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.

STDeleteN: Delete single vertex from geometry

Thursday April 25 2019 at 18:41

KeywordsSTDeleteN delete single vertex coordinate geometry SQL Server Spatial
Summary

STDeleteN Delete a single vertex from a geometry

SQL Server Spatial, unlike spatial database type systems such as PostGIS, does not provide coordinate editing functions:

  • STAddPoint
  • STUpdatePoint
  • STDeletePoint

These functions are not a part of either the SQL or OGC standards. In fact neither provide an API for the SQL editing of geometry objects. If one wishes to do so, one must program replacement functions. For SQL based solutions (eg based on TSQL) one must manipulate a geometry’s Well Know Text (WKT) description. This is pretty ugly (unless you are an Oracle Locator/Spatial user and so can access the array holding the ordinate values), so to wrap such access in specific SQL accessible functions is of use.

This article introduces the ST_DeleteN function, which deletes a single coordinate from the supplied geometry.

  1. CREATE FUNCTION [dbo].[STDeleteN]
  2. (
  3.   @p_geometry geometry,
  4.   @p_position INT = 1,
  5.   @p_round_xy INT = 3,
  6.   @p_round_zm INT = 2
  7. )
  8. RETURNS geometry
  9. AS
  10. BEGIN
  11.   RETURN [dbo].[STDelete] (
  12.             @p_geometry,
  13.             CAST(ISNULL(@p_position,1) AS VARCHAR(10)),
  14.             @p_round_xy,
  15.             @p_round_zm
  16.          );
  17. END
  18. GO

Description

Function that removes a single, nominated, coordinates from the supplied geometry.
The function does not process POINT or GEOMETRYCOLLECTION geometries.
The point to be deleted is supplied as a single integer.
The point number can be supplied as -1 (last number), or 1 to the total number of points in a WKT representation of the object.
A point number does not refer to a specific point within a specific sub-geometry eg point number 1 in the 2nd interiorRing in a polygon object.

Arguments

  1. @p_geometry     (geometry) - supplied geometry OF any TYPE.
  2. @p_position   (INT) - Valid point NUMBER IN geometry.
  3. @p_round_xy          (INT) - Rounding VALUE FOR XY ordinates.
  4. @p_round_zm          (INT) - Rounding VALUE FOR ZM ordinates.

Examples

  1. SELECT 'Single Point - No Action' AS message,
  2.        [dbo].[STDeleteN](geometry::STGeomFromText('POINT(0 0 1 1)',0),1,3,2).AsTextZM() AS WKT
  3. GO
  4. message WKT
  5. Single Point - No Action  POINT (0 0 1 1)
  6. SELECT 'MultiPoint - No Action'   AS message,
  7.        [dbo].[STDeleteN](geometry::STGeomFromText('MULTIPOINT((0 0 1 1))',0),1,3,2).AsTextZM() AS WKT
  8. GO
  9. message WKT
  10. MultiPoint - No Action  MULTIPOINT ((0 0 1 1))
  11. SELECT 'MultiPoint - All Points'  AS message,
  12.        t.IntValue,
  13.        [dbo].[STDeleteN](geometry::STGeomFromText('MULTIPOINT((1 1 1 1),(2 2 2 2),(3 3 3 3))',0),t.IntValue,3,2).AsTextZM() AS WKT
  14.   FROM [dbo].[generate_series](-1,4,1) AS t
  15. GO
  16. message IntValue  WKT
  17. MultiPoint - ALL Points -1  MULTIPOINT ((1 1 1 1), (2 2 2 2))
  18. MultiPoint - ALL Points 0 MULTIPOINT ((1 1 1 1), (2 2 2 2), (3 3 3 3))
  19. MultiPoint - ALL Points 1 MULTIPOINT ((2 2 2 2), (3 3 3 3))
  20. MultiPoint - ALL Points 2 MULTIPOINT ((1 1 1 1), (3 3 3 3))
  21. MultiPoint - ALL Points 3 MULTIPOINT ((1 1 1 1), (2 2 2 2))
  22. MultiPoint - ALL Points 4 MULTIPOINT ((1 1 1 1), (2 2 2 2), (3 3 3 3))
  23. SELECT 'LineString - All Points' AS message,
  24.        t.IntValue,
  25.        [dbo].[STDeleteN](geometry::STGeomFromText('LINESTRING(1 1, 2 2, 3 3, 4 4)',0),t.IntValue,3,2).AsTextZM() AS WKT
  26.   FROM [dbo].[generate_series](-1,5,1) AS t
  27. GO
  28. message IntValue  WKT
  29. LineString - ALL Points -1  LINESTRING (1 1, 2 2, 3 3)
  30. LineString - ALL Points 0 LINESTRING (1 1, 2 2, 3 3, 4 4)
  31. LineString - ALL Points 1 LINESTRING (2 2, 3 3, 4 4)
  32. LineString - ALL Points 2 LINESTRING (1 1, 3 3, 4 4)
  33. LineString - ALL Points 3 LINESTRING (1 1, 2 2, 4 4)
  34. LineString - ALL Points 4 LINESTRING (1 1, 2 2, 3 3)
  35. LineString - ALL Points 5 LINESTRING (1 1, 2 2, 3 3, 4 4)
  36. SELECT 'MultiLineString - All Points' AS message,
  37.        t.IntValue,
  38.        [dbo].[STDeleteN](geometry::STGeomFromText('MULTILINESTRING((1 1,2 2,3 3),(4 4,5 5,6 6))',0),t.IntValue,3,2).AsTextZM() AS WKT
  39.   FROM [dbo].[generate_series](-1,7,1) AS t
  40. GO
  41. message IntValue  WKT
  42. MultiLineString - ALL Points  -1  MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 5 5))
  43. MultiLineString - ALL Points  0 MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 5 5, 6 6))
  44. MultiLineString - ALL Points  1 MULTILINESTRING ((2 2, 3 3), (4 4, 5 5, 6 6))
  45. MultiLineString - ALL Points  2 MULTILINESTRING ((1 1, 3 3), (4 4, 5 5, 6 6))
  46. MultiLineString - ALL Points  3 MULTILINESTRING ((1 1, 2 2), (4 4, 5 5, 6 6))
  47. MultiLineString - ALL Points  4 MULTILINESTRING ((1 1, 2 2, 3 3), (5 5, 6 6))
  48. MultiLineString - ALL Points  5 MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 6 6))
  49. MultiLineString - ALL Points  6 MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 5 5))
  50. MultiLineString - ALL Points  7 MULTILINESTRING ((1 1, 2 2, 3 3), (4 4, 5 5, 6 6))
  51. WITH poly AS (
  52.   SELECT geometry::STGeomFromText('POLYGON((326000.0 5455000.0,327000.0 5455000.0,326820 5455440,326500.0 5456000.0,326000.0 5455000.0))',0) AS poly
  53. )
  54. SELECT 'ExteriorRing -- Sufficient Points - Note first and last point avoided.' AS message,
  55.        t.IntValue,
  56.        [dbo].[STDeleteN](a.poly,t.intValue,3,2).AsTextZM() AS t
  57.   FROM poly AS a CROSS apply [dbo].[generate_series](2,a.poly.STNumPoints()-1,1) AS t
  58. GO
  59. message IntValue  t
  60. ExteriorRing -- Sufficient Points - Note first and last point avoided.  2 POLYGON ((326000 5455000, 326820 5455440, 326500 5456000, 326000 5455000))
  61. ExteriorRing -- Sufficient Points - Note first and last point avoided.  3 POLYGON ((326000 5455000, 327000 5455000, 326500 5456000, 326000 5455000))
  62. ExteriorRing -- Sufficient Points - Note first and last point avoided.  4 POLYGON ((326000 5455000, 327000 5455000, 326820 5455440, 326000 5455000))
  63. SELECT 'SAME OUT' AS message,
  64.        [dbo].[STDeleteN](geometry::STGeomFromText('GEOMETRYCOLLECTION(POINT(2 3 4),LINESTRING(2 3 4,3 4 5),POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0)))',0),
  65.                         3,3,2).AsTextZM() AS WKT
  66. GO
  67. message WKT
  68. SAME OUT  GEOMETRYCOLLECTION (POINT (2 3 4), LINESTRING (2 3 4, 3 4 5), POLYGON ((326000 5455000, 327000 5455000, 326500 5456000, 326000 5455000)))
  69. SELECT 'LineString - Points' AS message,
  70.        [dbo].[STDelete](geometry::STGeomFromText('LINESTRING(1 1, 2 2, 3 3, 4 4)',0),'1,2',3,2).AsTextZM() AS WKT
  71. GO
  72. message WKT
  73. LineString - Points LINESTRING (3 3, 4 4)
  74. SELECT 'LineString - Last Point' AS message,
  75.        [dbo].[STDeleteN](geometry::STGeomFromText('LINESTRING(1 1, 2 2, 3 3, 4 4)',0),-1,3,2).AsTextZM() AS WKT
  76. GO
  77. message WKT
  78. LineString - LAST Point LINESTRING (1 1, 2 2, 3 3)

The following examples all fail.

  1. SELECT 'Polygon - All Points' AS message,
  2.        t.IntValue,
  3.        [dbo].[STDeleteN](geometry::STGeomFromText('POLYGON((1 1,10 1,10 10,1 10,1 1),(2 2,9 2,9 9,2 9,2 2))',0),t.intValue,3,2) AS t
  4.   FROM [dbo].[generate_series](-1,11,1) AS t
  5. GO
  6. Msg 6522, Level 16, State 1, Line 1
  7. A .NET Framework error occurred during execution OF user-defined ROUTINE OR aggregate "geometry":
  8. System.FormatException: 24121: The Polygon INPUT IS NOT valid because the START AND END points OF the interior ring NUMBER 1 are NOT the same. Each ring OF a polygon must have the same START AND END points.
  9. System.FormatException:
  10.    at Microsoft.SqlServer.Types.GeometryValidator.ValidatePolygonRing(Int32 iRing, Int32 cPoints, DOUBLE firstX, DOUBLE firstY, DOUBLE lastX, DOUBLE lastY)
  11.    at Microsoft.SqlServer.Types.Validator.EXECUTE(Transition transition)
  12.    at Microsoft.SqlServer.Types.ForwardingGeoDataSink.EndFigure()
  13.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseLineStringText()
  14.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePolygonText()
  15.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType TYPE)
  16.    at Microsoft.SqlServer.Types.WellKnownTextReader.READ(OpenGisType TYPE, Int32 srid)
  17.    at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType TYPE, SqlChars text, Int32 srid).
  18. SELECT 'ExteriorRing -- Insufficient Points' AS message,
  19.        t.IntValue,
  20.        [dbo].[STDeleteN](geometry::STGeomFromText('POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0))',0),t.intValue,3,2) AS t
  21.   FROM [dbo].[generate_series](2,5,1) AS t
  22. GO
  23. Msg 6522, Level 16, State 1, Line 20
  24. A .NET Framework error occurred during execution OF user-defined ROUTINE OR aggregate "geometry":
  25. System.FormatException: 24118: The Polygon INPUT IS NOT valid because the exterior ring does NOT have enough points. Each ring OF a polygon must contain at least four points.
  26. System.FormatException:
  27.    at Microsoft.SqlServer.Types.GeometryValidator.ValidatePolygonRing(Int32 iRing, Int32 cPoints, DOUBLE firstX, DOUBLE firstY, DOUBLE lastX, DOUBLE lastY)
  28.    at Microsoft.SqlServer.Types.Validator.EXECUTE(Transition transition)
  29.    at Microsoft.SqlServer.Types.ForwardingGeoDataSink.EndFigure()
  30.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseLineStringText()
  31.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePolygonText()
  32.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType TYPE)
  33.    at Microsoft.SqlServer.Types.WellKnownTextReader.READ(OpenGisType TYPE, Int32 srid)
  34.    at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType TYPE, SqlChars text, Int32 srid).
  35. SELECT 'Polygon with InteriorRing (end up with insufficient points)' AS message,
  36.        [dbo].[STDeleteN](geometry::STGeomFromText('POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0),
  37.                                                           (326500.0 5455500.0,326550.0 5455200.0,326450.0 5455200.0,326500.0 5455500.0))',0),
  38.                              t.intValue,3,2) AS t
  39.   FROM [dbo].[generate_series](-1,11,1) AS t
  40. GO
  41. Msg 6522, Level 16, State 1, Line 38
  42. A .NET Framework error occurred during execution OF user-defined ROUTINE OR aggregate "geometry":
  43. System.FormatException: 24120: The Polygon INPUT IS NOT valid because the interior ring NUMBER 1 does NOT have enough points. Each ring OF a polygon must contain at least four points.
  44. System.FormatException:
  45.    at Microsoft.SqlServer.Types.GeometryValidator.ValidatePolygonRing(Int32 iRing, Int32 cPoints, DOUBLE firstX, DOUBLE firstY, DOUBLE lastX, DOUBLE lastY)
  46.    at Microsoft.SqlServer.Types.Validator.EXECUTE(Transition transition)
  47.    at Microsoft.SqlServer.Types.ForwardingGeoDataSink.EndFigure()
  48.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseLineStringText()
  49.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePolygonText()
  50.    at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType TYPE)
  51.    at Microsoft.SqlServer.Types.WellKnownTextReader.READ(OpenGisType TYPE, Int32 srid)
  52.    at Microsoft.SqlServer.Types.SqlGeometry.GeometryFromText(OpenGisType TYPE, SqlChars text, Int32 srid).

The function can be downloaded from here

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