locked
Combine LINESTRINGs to POLYGONs to MULTIPOLYGONs RRS feed

  • Question

  • As subject says. And all this in T-SQL. Having a bunch of LINESTRINGS (either WKT or WKB), would like to make this to POLYGONs and finally combine the POLYGONs to a MULTIPOLYGON.

    Could anybody please give me a pointer?

    Regards

    neil
    Wednesday, January 14, 2009 11:14 AM

Answers

  • Do you mean that your linestrings are currently closed rings representing the outside perimeter of the polygon you want to create? If so, you can extract the coordinate list from WKB and convert them into polygons using something like this:
    DECLARE @LineString geometry  
    SET @LineString = geometry::STLineFromText('LINESTRING(0 0, 20 0, 30 30, 0 10, 0 0)', 0)  
     
    DECLARE @Polygon geometry  
    SET @Polygon =   
    geometry::STGeomFromWKB(  
     0x01 + 0x03000000 + 0x01000000  
     + SUBSTRING(@LineString.STAsBinary(),6,datalength(@LineString.STAsBinary())  
    ),@LineString.STSrid)  
     
    SELECT @Polygon, @Polygon.STAsText() 

    The result of this is to convert a shape from LINESTRING(0 0, 20 0, 30 30, 0 10, 0 0) to POLYGON ((0 0, 20 0, 30 30, 0 10, 0 0))

    If instead, you meant that you want to create the polygon that encompasses a series of separate linestrings, you might be looking for the STConvexHull() method instead.
    • Marked as answer by neil.young Wednesday, January 14, 2009 12:13 PM
    Wednesday, January 14, 2009 11:40 AM
    Answerer

All replies

  • Do you mean that your linestrings are currently closed rings representing the outside perimeter of the polygon you want to create? If so, you can extract the coordinate list from WKB and convert them into polygons using something like this:
    DECLARE @LineString geometry  
    SET @LineString = geometry::STLineFromText('LINESTRING(0 0, 20 0, 30 30, 0 10, 0 0)', 0)  
     
    DECLARE @Polygon geometry  
    SET @Polygon =   
    geometry::STGeomFromWKB(  
     0x01 + 0x03000000 + 0x01000000  
     + SUBSTRING(@LineString.STAsBinary(),6,datalength(@LineString.STAsBinary())  
    ),@LineString.STSrid)  
     
    SELECT @Polygon, @Polygon.STAsText() 

    The result of this is to convert a shape from LINESTRING(0 0, 20 0, 30 30, 0 10, 0 0) to POLYGON ((0 0, 20 0, 30 30, 0 10, 0 0))

    If instead, you meant that you want to create the polygon that encompasses a series of separate linestrings, you might be looking for the STConvexHull() method instead.
    • Marked as answer by neil.young Wednesday, January 14, 2009 12:13 PM
    Wednesday, January 14, 2009 11:40 AM
    Answerer
  • Very good. The first step has been done. I got my POLYGON. This works fine, although I don't know anything about what I've done there :)

    Is there also a way to combine these POLYGONs to a MULTIPOLYGON?

    Regards

    neil
    Wednesday, January 14, 2009 11:50 AM
  • Ah, found the WKB description. Able to roll it by myself.
    Thanks!
    neil
    Wednesday, January 14, 2009 12:13 PM