Go to content Go to navigation and search

Home

Current SQL Server Blog Articles

    SQL Server Spatial: Converting a LineString to COGO XML
    SQL Server Spatial: Creating (Multi)LineStrings geometries from COGO XML instructions
    New Version of Package of TSQL Spatial Functions
    Vectorize/Segmentize SQL Server 2012
    Coordinate Editing Functions for SQL Server Spatial
    Function to Scale a geometry object for SQL Server Spatial
    TSQL String Tokenizer Function for SQL Server
    geography/geometry to MBR helper functions
    generate_series for SQL Server 2008
    Extract Polygons from result of STIntersection in SQL Server Spatial
    Function to round ordinates of a SQL Server Spatial geometry object
    Extract elements of SQL Server Spatial geometry object
    Counting number of polygon rings SQL Server Spatial
    Filtering Polygon Rings in SQL Server Spatial
    Function to Move a geometry object in SQL Server Spatial
    Alternate Centroid Functions for SQL Server Spatial
    Function to Rotate geometry objects in SQL Server Spatial
    A GetVertices wrapper for DumpPoints in SQL Server 2008 Spatial
    Creating a Morton number Space Key generator for SQL Server
    Gridding a geometry or geography object (SQL Server Denali)
    On hinting spatial indexes
    Random Search Procedure (SQL Server 2008 Spatial)
    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
    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
    Checking is a column in a table or a view is of type geometry or geography
    Extracting geometry type string values from geometry/geography objects in SQL Server 2008 Spatial
    Vectorising geometry objects in SQL Server 2008
    MBR to geography/geometry helper 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
    A Dump Points Function for SQL Server 2008 Spatial
    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.

Coordinate Editing Functions for SQL Server Spatial

Thursday October 15 2015 at 17:59

Keywordsinsert update delete coordinates geometry
Summary

TSql functions for inserting, updating, and deleting coordinates in geometries.

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.

I have programmed 4 TSQL functions:

  1.   /** ----------------------------------------------------------------------------------------
  2.   * @function   : STAddPoint
  3.   * @precis     : Adds a point to a MultiPoint, LineString or MultiLineString geometry before point <p_position> (1-based index: Set to -1/NULL for appending.
  4.   * @version    : 1.0
  5.   * @param      : p_geometry  : geometry : Original geometry
  6.   * @param      : p_point     : geometry  : Replacement point to be inserted
  7.   * @param      : p_position  : integer : Position before which point is inserted. If NULL or -1 the point is appended to whole geometry.
  8.   * @param      : p_precision : integer : ordinate precision (decimal digits)
  9.   * @return     : geometry : input geometry with new point added.
  10.   * @history    : Simon Greener - October 2015 - Original coding.
  11.   * @copyright  : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
  12.   *               http://creativecommons.org/licenses/by-sa/2.5/au/
  13.   **/
  14. CREATE FUNCTION [STADDPOINT](
  15.        @p_geometry  geometry,
  16.        @p_point     geometry,
  17.        @p_position  INTEGER,
  18.        @p_precision INTEGER = 3)
  19.   RETURNS GEOMETRY
  20. ....
  21.   /** ----------------------------------------------------------------------------------------
  22.   * @function   : STDeletePoint
  23.   * @precis     : Deletes a point from a geometry.
  24.   * @version    : 1.0
  25.   * @param      : p_geometry  : geometry : Original geometry
  26.   * @param      : p_position     : integer : Coordinate position to be deleted.
  27.   * @param      : p_precision : integer : ordinate precision (decimal digits)
  28.   * @return     : geometry : input geometry with point deleted.
  29.   * @history    : Simon Greener - October 2015 - Original coding.
  30.   * @copyright  : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
  31.   *               http://creativecommons.org/licenses/by-sa/2.5/au/
  32.   **/
  33. CREATE FUNCTION [STDELETEPOINT](
  34.        @p_geometry geometry,
  35.        @p_position INT = 1,
  36.        @p_round    INT = 1)
  37.   RETURNS GEOMETRY
  38. ....
  39.   /** ----------------------------------------------------------------------------------------
  40.   * @function   : STUpdatePoint
  41.   * @precis     : Updates every point in a geometry that has the same value as the specified point.
  42.   * @version    : 1.0
  43.   * @param      : p_geometry  : geometry : Original geometry
  44.   * @param      : p_original_pt : geometry  : Point to be updated. All points with this value are updated. Useful for updating first/last point in a polygon ring
  45.   * @param      : p_replacement_pt : geometry : Point that replaces original when matched.
  46.   * @param      : p_precision : integer : ordinate precision (decimal digits)
  47.   * @return     : geometry : input geometry with new point added.
  48.   * @history    : Simon Greener - October 2015 - Original coding.
  49.   * @copyright  : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
  50.   *               http://creativecommons.org/licenses/by-sa/2.5/au/
  51.   **/
  52. CREATE FUNCTION [STUPDATEPOINT](
  53.        @p_geometry    geometry,
  54.        @p_original_pt geometry,
  55.        @p_replace_pt  geometry,
  56.        @p_round       INT = 1)
  57.   RETURNS GEOMETRY
  58. ....
  59.   /** ----------------------------------------------------------------------------------------
  60.   * @function   : STUpdatePointN
  61.   * @precis     : Updates point at specific position within geometry
  62.   * @version    : 1.0
  63.   * @param      : p_geometry  : geometry : Original geometry
  64.   * @param      : p_position : int : Point to be updated. Only this point is updated.
  65.   * @param      : p_replace_pt : geometry : Point that replaces original at position p_position.
  66.   * @param      : p_precision : integer : ordinate precision (decimal digits)
  67.   * @return     : geometry : input geometry with new point added.
  68.   * @history    : Simon Greener - October 2015 - Original coding.
  69.   * @copyright  : Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.
  70.   *               http://creativecommons.org/licenses/by-sa/2.5/au/
  71.   **/
  72. CREATE FUNCTION [STUPDATEPOINTN](
  73.        @p_geometry   geometry,
  74.        @p_position INT,
  75.        @p_replace_pt geometry,
  76.        @p_round      INT = 1)
  77.   RETURNS GEOMETRY
  78. ...

The functions are available via a new download of all my SQL Server TSQL function. I hope these are of use to someone out there.

The functions in this article are available in the package of SQL Server functions downloadable from my website’s download page’s SQL Server Spatial TSQL Functions link

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