none
Create ConvexHull polygon of polygons in table

    Question

  • Hi,

    I have table that represent polygons and I have to create ConvexHull polygon. The problem is how to pick up all polygons from table into one geometry and then create a ConvexHull polygon!

    Thanks!

    Sunday, April 08, 2012 10:27 AM

Answers

All replies

  • Hi,

    Here is the code sample.Read  each polygon from table and union themup into another variable, finally get the conexhull from the varibale. 

    GO
    /*
     Example SET @GeomCol = geometry::STGeomFromText('POLYGON((0 0, 0 2, 1 1, 2 2, 2 0, 0 0))', 0);
    */
    DECLARE @GeomCol   geometry,@CombinedGeomCol geometry,@ConvexHull geometry;


    DECLARE Cur  CURSOR
    FOR SELECT GeomCol  FROM tblGeom

    OPEN Cur
    FETCH NEXT FROM Cur INTO @GeomCol

    WHILE @@FETCH_STATUS =0
    BEGIN
     SET @CombinedGeomCol  =  @CombinedGeomCol.STUnion(@GeomCol)
     FETCH NEXT FROM Cur INTO @GeomCol
    END

    CLOSE Cur
    DEALLOCATE Cur

    SELECT @CombinedGeomCol.STConvexHull()

    • Proposed as answer by Sarma N Evani Wednesday, April 11, 2012 2:51 PM
    Tuesday, April 10, 2012 12:12 PM
  • In SQL Server 2012, you can simply use the ConvexHullAggregate():

    SELECT geometry::ConvexHullAggregate(yourgeomcol)
    FROM yourtable;


    twitter: @alastaira blog: http://alastaira.wordpress.com/

    Tuesday, April 10, 2012 9:22 PM