ST_DumpPoints

" /> STDumpPoints: A Function that Dumps the Vertices/Points in a geometry
Go to content Go to navigation and search

Home

Current SQL Server Blog Articles

    STGeometryTypes: Extracting all geometry type string values from complex geometry
    STDensify: Densify (m)LineString or (m)Polygon geometry objects
    STInsertN: Insert single vertex into a geometry
    STUpdateN: Update (replace) a single vertex within a geometry object.
    STUpdate: Replace all points equal to the supplied point with replacement point.
    STDeleteN: Delete single vertex from geometry
    STDelete: Deleting vertices in geometry objects
    STFlipVectors: Normalize direction of linestring vectors
    STConvertToLineString: Extract LineStrings in GeometryCollection to create LineString
    STLine2Cogo: Converting LineStrings to COGO XML
    STCogo2Line: Creating (Multi)LineStrings geometries from COGO XML instructions
    STVectorize: Break Linestring/Polygon elements into 2 point vectors (or 3 point circular curves)
    STScale: Function to Scale a geometry object
    TSQL String Tokenizer Function for SQL Server
    STGeometry2MBR/STGeography2MBR: Compute and return MBR ordinates
    generate_series for SQL Server 2008
    STExtractPolygon: Extract Polygons from result of STIntersection in SQL Server Spatial
    STRound: Function to round ordinates of a SQL Server Spatial geometry object
    STExtract: Extract elements of a geometry object
    STNumRings: Counting number of polygon rings
    STFilterRings: Removing rings from Polygon based on area.
    STMove: Function to Move a geometry object in SQL Server Spatial
    STCentroid*: Alternate Functions for Compute a Centroid
    STRotate: Function to rotate a geometry object in SQL Server Spatial
    STVertices: Wrapper over STDumpPoints
    STMorton: Creating a Morton number Space Key value for grid square
    Gridding a geometry or geography object (SQL Server Denali)
    On hinting spatial indexes
    RandomSearchByExtent: Random Search Procedure (2008 Spatial)
    COGO: Convert DMS String to decimal degrees floating point number.
    COGO: Converting (Google Earth) Formatted Longitude/Latitude points to decimal degrees (SQL Server)
    COGO: Convert Degrees, Minutes and Seconds values to Decimal Degrees
    COGO: DD2DMS Formatting a latitude/longitude decimal degree value
    COGO: Create point from bearing and distance
    COGO: Compute number of vertices required to stroke circle or circular arc
    COGO: Calculating the bearing between two points (SQL Server 2008 Spatial)
    COGO: Compute arc length subtended by angle centre of circle
    COGO: Compute chord length of segment of a circle (arc)
    COGO: Computing Arc To Chord (Arc2Chord) Separation
    COGO: Creating a stroked polygon from a circle's centre x,y and radius
    COGO: Finding centre and radius of a curve defined by three points: FindCircle function
    CheckRadii: Identifying Tight Radius Curves sections within LineString geometry data
    Generating random point data for SQL Server 2008 Spatial
    New Presentation on Active (Searchable) Spatial Metadata for SQL Server 2008 Spatial and FreeText
    STisGeo: Checking if a column in a table or a view is of type geometry or geography
    Vectorising geometry objects in SQL Server 2008
    STMBR2Geometry/STMBR2Geography functions
    Extracting geometry types from geometry/geography objects in SQL Server 2008
    Getting the number of coordinate dimensions of a geometry/geography object in SQL Server 2008
    STDumpPoints: A Function that Dumps the Vertices/Points in a geometry
    toGeography and toGeography Conversion Functions for SQL Server 2008 Spatial
    Write text file with spatial data from SQL Server 2008
    Loading Shapefiles into Geography type column in SQL Server 2008
    Tip #6: Correcting invalid geometries
    Tip #5: Where or where has my little column gone
    Tip #4: What Coordinate System is that?
    Tip #3: What object is that?
    Tip #2: Spatial Indexing and Primary Keys
    Tip #1: SQL Server 2008 "Katmai" - Setting SRIDs

Search

    Browse

RSS / Atom

Email me

textpattern

Creative Commons License
All Blog Articles, Data Models and Free Source Code by Simon Greener, The SpatialDB Advisor is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

STDumpPoints: A Function that Dumps the Vertices/Points in a geometry

Monday September 28 2009 at 06:03

KeywordsSTDumpPoints ST_DumpPoints dumping points get vertices
Summary

A function that dumps (or extracts) the vertices / points of a geometry/geography object is very useful for the database spatial practitioner. Here is one whose name is drawn from PostGIS’s ST_DumpPoints

I often find I need to manipulate the points of a geometry/geography object directly in SQL. Oracle has the neat TABLE function mdsys.sdo_util.GetVertices( sdo_geometry ) for this. Since SQL Server 2008 does not have such a function I found I had to write it myself. I prefer to program in TSQL using the OGC geometry/geography API rather than in a CLR language like C# so I did it entirely in TSQL.

Note that the function is written for only the geometry data type. See my article on the conversion functions toGeography/toGeometry , and the tests below, to see how the function can be used for the geography data type.

  1. USE [GISDB]  -- Change this to your database
  2. GO
  3. .
  4. CREATE FUNCTION [dbo].[STDumpPoints]( @p_geom geometry )
  5.   RETURNS @Points TABLE
  6.    (x FLOAT,
  7.     y FLOAT,
  8.     z FLOAT,
  9.     m FLOAT )
  10. AS
  11. BEGIN
  12.   DECLARE
  13.     @v_GeometryType VARCHAR(1000),
  14.     @ringn          INT,
  15.     @geomn          INT,
  16.     @pointn         INT,
  17.     @geom           geometry;
  18.   BEGIN
  19.     IF ( @p_geom IS NULL )
  20.       RETURN;
  21. .
  22.     SET @v_GeometryType = @p_geom.STGeometryType();
  23. .
  24.     IF ( @v_GeometryType = 'Point' )
  25.     BEGIN
  26.       INSERT INTO @Points ( [x],[y],[z],[m] )
  27.            VALUES ( @p_geom.STX,
  28.                     @p_geom.STY,
  29.                     @p_geom.Z,
  30.                     @p_geom.M );
  31.       RETURN;
  32.     END;
  33. .
  34.     IF ( @v_GeometryType = 'MultiPoint' )
  35.     BEGIN
  36.       SET @geomn  = 1;
  37.       WHILE ( @geomn <= @p_geom.STNumGeometries() )
  38.       BEGIN
  39.         INSERT INTO @Points ( [x],[y],[z],[m] )
  40.              VALUES ( @p_geom.STGeometryN(@geomn).STX,
  41.                       @p_geom.STGeometryN(@geomn).STY,
  42.                       @p_geom.STGeometryN(@geomn).Z,
  43.                       @p_geom.STGeometryN(@geomn).M );
  44.         SET @geomn = @geomn + 1;
  45.       END;
  46.       RETURN;
  47.     END;
  48. .
  49.     IF ( @v_GeometryType = 'LineString' )
  50.     BEGIN
  51.       SET @pointn = 1;
  52.       WHILE ( @pointn <= @p_geom.STNumPoints() )
  53.       BEGIN
  54.         INSERT INTO @Points ( [x],[y],[z],[m] )
  55.            VALUES ( @p_geom.STPointN(@pointn).STX,
  56.                     @p_geom.STPointN(@pointn).STY,
  57.                     @p_geom.STPointN(@pointn).Z,
  58.                     @p_geom.STPointN(@pointn).M );
  59.         SET @pointn = @pointn + 1;
  60.       END;  
  61.       RETURN;
  62.     END;
  63. .
  64.     IF ( @v_GeometryType = 'MultiLineString' )
  65.     BEGIN
  66.       SET @geomn  = 1;
  67.       WHILE ( @geomn <= @p_geom.STNumGeometries() )
  68.       BEGIN
  69.         SET @pointn = 1;
  70.         WHILE ( @pointn <= @p_geom.STGeometryN(@geomn).STNumPoints() )
  71.         BEGIN
  72.           INSERT INTO @Points ( [x],[y],[z],[m] )
  73.                VALUES ( @p_geom.STGeometryN(@geomn).STPointN(@pointn).STX,
  74.                         @p_geom.STGeometryN(@geomn).STPointN(@pointn).STY,
  75.                         @p_geom.STGeometryN(@geomn).STPointN(@pointn).Z,
  76.                         @p_geom.STGeometryN(@geomn).STPointN(@pointn).M );
  77.           SET @pointn = @pointn + 1;
  78.         END;  
  79.         SET @geomn = @geomn + 1;
  80.       END;
  81.       RETURN;
  82.     END;
  83. .
  84.     IF ( @v_GeometryType = 'Polygon' )
  85.     BEGIN
  86.       SET @ringn  = 0;
  87.       WHILE ( @ringn < ( 1 + @p_geom.STNumInteriorRing() ) )
  88.       BEGIN
  89.         IF ( @ringn = 0 )
  90.           SET @geom = @p_geom.STExteriorRing()
  91.         ELSE
  92.           SET @geom = @p_geom.STInteriorRingN(@ringn);
  93.         SET @pointn = 1;
  94.         WHILE ( @pointn <= @geom.STNumPoints() )
  95.         BEGIN
  96.           INSERT INTO @Points ( [x],[y],[z],[m] )
  97.                VALUES ( @geom.STPointN(@pointn).STX,
  98.                         @geom.STPointN(@pointn).STY,
  99.                         @geom.STPointN(@pointn).Z,
  100.                         @geom.STPointN(@pointn).M);
  101.           SET @pointn = @pointn + 1;
  102.         END;
  103.         SET @ringn = @ringn + 1;
  104.       END;
  105.       RETURN;
  106.     END;
  107. .
  108.     IF ( @v_GeometryType = 'MultiPolygon' )
  109.     BEGIN
  110.       SET @geomn  = 1;
  111.       WHILE ( @geomn <= @p_geom.STNumGeometries() )
  112.       BEGIN
  113.         SET @ringn  = 0;
  114.         WHILE ( @ringn < ( 1 + @p_geom.STGeometryN(@geomn).STNumInteriorRing() ) )
  115.         BEGIN
  116.           IF ( @ringn = 0 )
  117.             SET @geom = @p_geom.STGeometryN(@geomn).STExteriorRing()
  118.           ELSE
  119.             SET @geom = @p_geom.STGeometryN(@geomn).STInteriorRingN(@ringn);
  120.           SET @pointn = 1;
  121.           WHILE ( @pointn <= @geom.STNumPoints() )
  122.           BEGIN
  123.             INSERT INTO @Points ( [x],[y],[z],[m] )
  124.                  VALUES ( @geom.STPointN(@pointn).STX,
  125.                           @geom.STPointN(@pointn).STY,
  126.                           @geom.STPointN(@pointn).Z,
  127.                           @geom.STPointN(@pointn).M );
  128.             SET @pointn = @pointn + 1;
  129.           END;
  130.           SET @ringn = @ringn + 1;
  131.         END;
  132.         SET @geomn = @geomn + 1;
  133.       END;
  134.       RETURN;
  135.     END;
  136. .
  137.     IF ( @v_GeometryType = 'GeometryCollection' )
  138.     BEGIN
  139.       SET @geomn  = 1;
  140.       WHILE ( @geomn <= @p_geom.STNumGeometries() )
  141.       BEGIN
  142.          INSERT INTO @Points ( [x],[y],[z],[m] )
  143.               SELECT [x],[y],[z],[m]
  144.                 FROM [dbo].[STDumpPoints](@p_geom.STGeometryN(@geomn));
  145.         SET @geomn = @geomn + 1;
  146.       END;
  147.       RETURN;
  148.     END;
  149.   END;
  150.   RETURN;
  151. END
  152. GO

Testing it we get:

  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('POINT(0 1 2 3)',0)) AS e;

Result

x y z m
0 1 2 3
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('LINESTRING(2 3 4,3 4 5)',0)) AS e;

Result

x y z m
2 3 4 NULL
3 4 5 NULL
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))',0)) AS e;

Result

x y z m
0 0 NULL NULL
100 0 NULL NULL
100 100 NULL NULL
0 100 NULL NULL
0 0 NULL NULL
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('MULTILINESTRING((2 3 4,3 4 5),(1 1,2 2))',0)) AS e;

Result

x y z m
2 3 4 NULL
3 4 5 NULL
1 1 NULL NULL
2 2 NULL NULL
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('MULTIPOINT((1 1),(2 2),(3 3))',0)) AS e;

Result

x y z m
1 1 NULL NULL
2 2 NULL NULL
3 3 NULL NULL
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('MULTIPOLYGON (((200 200, 400 200, 400 400, 200 400, 200 200)), ((0 0, 100 0, 100 100, 0 100, 0 0)))',0)) AS e;

Result

x y z m
200 200 NULL NULL
400 200 NULL NULL
400 400 NULL NULL
200 400 NULL NULL
200 200 NULL NULL
0 0 NULL NULL
100 0 NULL NULL
100 100 NULL NULL
0 100 NULL NULL
0 0 NULL NULL
  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](geometry::STGeomFromText('GEOMETRYCOLLECTION(POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0)),POINT(2 3 4),MULTIPOINT((1 1),(2 2),(3 3)),LINESTRING(2 3 4,3 4 5),MULTILINESTRING((2 3 4,3 4 5),(1 1,2 2)),POINT(4 5),MULTIPOINT((1 1),(2 2)),POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0)),MULTIPOLYGON (((200 200, 400 200, 400 400, 200 400, 200 200)), ((0 0, 100 0, 100 100, 0 100, 0 0))))',0)) AS e;

Result

x y z m
0 0 NULL NULL
100 0 NULL NULL
100 100 NULL NULL
0 100 NULL NULL
0 0 NULL NULL
2 3 4 NULL
1 1 NULL NULL
2 2 NULL NULL
3 3 NULL NULL
2 3 4 NULL
3 4 5 NULL
2 3 4 NULL
3 4 5 NULL
1 1 NULL NULL
2 2 NULL NULL
4 5 NULL NULL
1 1 NULL NULL
2 2 NULL NULL
326000 5455000 NULL NULL
327000 5455000 NULL NULL
326500 5456000 NULL NULL
326000 5455000 NULL NULL
200 200 NULL NULL
400 200 NULL NULL
400 400 NULL NULL
200 400 NULL NULL
200 200 NULL NULL
0 0 NULL NULL
100 0 NULL NULL
100 100 NULL NULL
0 100 NULL NULL
0 0 NULL NULL

And to show that it will work for geography data type based data:

  1. SELECT e.[x], e.[y], e.[z], e.[m]
  2.   FROM [dbo].[STDumpPoints](
  3.          dbo.toGeometry(geography::STGeomFromText('POLYGON ((148.0 -44.0, 148.0 -43.0, 147.0 -43.0, 147.0 -44.0, 148.0 -44.0), (147.4 -43.6, 147.2 -43.6, 147.2 -43.2, 147.4 -43.2, 147.4 -43.6))',4326),
  4.                         0)) AS e;

Result

x y z m
148 -44 NULL NULL
148 -43 NULL NULL
147 -43 NULL NULL
147 -44 NULL NULL
148 -44 NULL NULL
147.4 -43.6 NULL NULL
147.2 -43.6 NULL NULL
147.2 -43.2 NULL NULL
147.4 -43.2 NULL NULL
147.4 -43.6 NULL NULL

I hope this function is useful to someone.

Creative Commons License

post this at del.icio.uspost this at Diggpost this at Technoratipost this at Redditpost this at Farkpost this at Yahoo! my webpost this at Windows Livepost this at Google Bookmarkspost this to Twitter

Comment