Go to content Go to navigation and search

Home

Current SQL Server Blog Articles


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