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.

SQL Server Spatial: Creating (Multi)LineStrings geometries from COGO XML instructions

Monday June 25 2018 at 15:39

KeywordsXML COGO LineString Import Create
Summary

Xml Cogo to LineString conversion function for Sql Server Spatial.

Previously I wrote about a function that converted the segments of a (Multi)LineString into a COGO XML document that contains all the instructions (bearing, distances, move deltaZ etc) to create a (Multi)LineString.

Today I present a function that does the reverse: takes the COGO XML and creates the (Multi)LineString.

The instructions are provided to the function in an XML document with the following structure:

  1. <Cogo srid={int}>
  2.   <Segments>
  3.     <Segment>
  4.       <MoveTo></MoveTo>  
  5.       <sDegMinSec></sDegMinSec>
  6.       <Bearing></Bearing>
  7.       <Distance></Distance>
  8.       <DeltaZ></DeltaZ>
  9.     <Segment>
  10.     <Segment>
  11.       ....
  12.     <Segment>
  13.   </Segments>
  14. </Cogo>
  15. -- <MoveTo> can be either a WKT point eg POINT(0 0 -1) or a coordinate string eg "0 0 -1".
  16. -- If the starting <moveTo> does not exist a 2D Line starting at 0,0 is created.
  17. -- If it does exist, then it determines if line is 2D or 3D.
  18. -- Measure ordinates are currently not supported.
  19. -- <DegMinSec> does not have to exist if <Bearing> (decimal degrees) exists.
  20. -- <DeltaZ> is optional. If <DeltaZ> does exist, a starting <MoveTo> must exist and have a Z ordinate.

Here is an example of Cogo XML:

  1. <Cogo srid="0">
  2.   <Segments>
  3.     <Segment>
  4.       <MoveTo>POINT (10 10 -1)</MoveTo>
  5.       <Bearing>345.364</Bearing>
  6.       <Distance>345.364</Distance>
  7.       <DeltaZ>1</DeltaZ>
  8.     </Segment>
  9.     <Segment>
  10.       <Bearing>83.0346</Bearing>
  11.       <Distance>83.0346</Distance>
  12.       <DeltaZ>2</DeltaZ>
  13.     </Segment>
  14.     <Segment>
  15.       <Bearing>186.575</Bearing>
  16.       <Distance>186.575</Distance>
  17.       <DeltaZ>3</DeltaZ>
  18.     </Segment>
  19.   </Segments>
  20. </Cogo>

The function looks like this. Its documentation can be seen see here.

  1. CREATE FUNCTION [$(cogoowner)].[STCogo2Line]
  2. (
  3.   @p_cogo        xml,
  4.   @p_start_point geometry,
  5.   @p_round_xy    INT = 3,
  6.   @p_round_z     INT = 2
  7. )
  8. RETURNS geometry
  9. .....

Here are some examples:

  1. Print 'Generate XYZ linestring using ordinate string moveTo.';
  2. DECLARE @v_cogo xml;
  3. SET @v_cogo =
  4. '<Cogo srid="28356"><Segments>
  5. <Segment><MoveTo>10 10 -1</MoveTo><DegMinSec> 345°21''48.75"</DegMinSec><Distance>7.26992</Distance><DeltaZ>1</DeltaZ></Segment>
  6. <Segment><DegMinSec>  83° 2''4.652"</DegMinSec><Distance>151.712</Distance><DeltaZ>2</DeltaZ></Segment>
  7. <Segment><DegMinSec> 186°34''30.73"</DegMinSec><Distance>10.3924</Distance><DeltaZ>3</DeltaZ></Segment>
  8. </Segments></Cogo>';
  9. SELECT [cogo].[STCogo2Line] (@v_cogo, 3, 2).AsTextZM() AS cogoLine
  10. GO
  11. -- Result
  12. -- LINESTRING(10 10 -1,8.163 17.034 0,158.755 35.432 2,157.565 25.108 5)
  13. --
  14. Print 'Generate XYZ linestring using POINT() WKT moveTo.';
  15. DECLARE @v_cogo xml;
  16. SET @v_cogo =
  17. '<Cogo srid="28356"><Segments>
  18. <Segment><MoveTo>POINT(10 10 -1)</MoveTo><DegMinSec> 345°21''48.75"</DegMinSec><Distance>7.26992</Distance><DeltaZ>1</DeltaZ></Segment>
  19. <Segment><DegMinSec>  83° 2''4.652"</DegMinSec><Distance>151.712</Distance><DeltaZ>2</DeltaZ></Segment>
  20. <Segment><DegMinSec> 186°34''30.73"</DegMinSec><Distance>10.3924</Distance><DeltaZ>3</DeltaZ></Segment>
  21. </Segments></Cogo>';
  22. SELECT [cogo].[STCogo2Line] (@v_cogo, 3, 2).AsTextZM() AS cogoLine
  23. GO
  24. -- Result
  25. -- LINESTRING (10 10 -1, 8.163 17.034 0, 158.755 35.432 2, 157.565 25.108 5)
  26. --
  27. Print 'Create 2D MultiLineString using single MoveTo instruction';
  28. DECLARE @v_cogo xml;
  29. SET @v_cogo =
  30. '<Cogo srid="28356"><Segments>
  31. <Segment><DegMinSec> 345°21''48.75"</DegMinSec><Distance>7.26992</Distance><DeltaZ>1</DeltaZ></Segment>
  32. <Segment><DegMinSec>  83° 2''4.652"</DegMinSec><Distance>151.712</Distance><DeltaZ>2</DeltaZ></Segment>
  33. <Segment><MoveTo>POINT(10 10 -1)</MoveTo><DegMinSec> 186°34''30.73"</DegMinSec><Distance>10.3924</Distance><DeltaZ>3</DeltaZ></Segment>
  34. </Segments></Cogo>';
  35. SELECT [cogo].[STCogo2Line] (@v_cogo, 3, 2).AsTextZM() AS cogoLine
  36. GO
  37. -- Result
  38. -- MULTILINESTRING ((0 0, -1.837 7.034, 148.755 25.432), (10 10, 8.81 -0.324))
  39. --
  40. Print 'Create 2D MultiLineString with 3 elements using two styles of MoveTo';
  41. DECLARE @v_cogo xml;
  42. SET @v_cogo =
  43. '<Cogo srid="28356"><Segments>
  44. <Segment><MoveTo>POINT(10 10)</MoveTo><DegMinSec> 345°21''48.75"</DegMinSec><Distance>7.26992</Distance><DeltaZ>1</DeltaZ></Segment>
  45. <Segment><DegMinSec>  83° 2''4.652"</DegMinSec><Distance>151.712</Distance><DeltaZ>2</DeltaZ></Segment>
  46. <Segment><DegMinSec> 186°34''30.73"</DegMinSec><Distance>10.3924</Distance><DeltaZ>3</DeltaZ></Segment>
  47. <Segment><DegMinSec>  86° 2''48.18"</DegMinSec><Distance>13.3879</Distance><DeltaZ>4</DeltaZ></Segment>
  48. <Segment><MoveTo>POINT(100 100)</MoveTo><DegMinSec> 187° 5''20.73"</DegMinSec><Distance>9.09956</Distance><DeltaZ>5</DeltaZ></Segment>
  49. <Segment><DegMinSec>  89°20''48.85"</DegMinSec><Distance>61.061</Distance><DeltaZ>6</DeltaZ></Segment>
  50. <Segment><DegMinSec> 359°49''6.930"</DegMinSec><Distance>11.6861</Distance><DeltaZ>7</DeltaZ></Segment>
  51. <Segment><MoveTo>200 200 -15</MoveTo><DegMinSec>  96°21''55.47"</DegMinSec><Distance>11.0401</Distance><DeltaZ>8</DeltaZ></Segment>
  52. <Segment><DegMinSec> 355° 3''18.45"</DegMinSec><Distance>28.1448</Distance><DeltaZ>9</DeltaZ></Segment>
  53. <Segment><DegMinSec>  82°59''21.42"</DegMinSec><Distance>5.59584</Distance><DeltaZ>10</DeltaZ></Segment>
  54. </Segments></Cogo>';
  55. SELECT [cogo].[STCogo2Line] (@v_cogo, 3, 2).AsTextZM() AS cogoLine
  56. GO
  57. -- Result
  58. -- MULTILINESTRING (
  59. -- (10 10, 8.163 17.034, 158.755 35.432, 157.565 25.108, 170.921 26.031),
  60. -- (100 100, 98.877 90.97, 159.934 91.666, 159.897 103.352),
  61. -- (200 200, 210.972 198.776, 208.546 226.816, 214.1 227.499))
  62. --
  63. DECLARE @v_cogo xml;
  64. SET @v_cogo =
  65. '<Cogo srid="0">
  66. <Segments>
  67. <Segment><sDegMinSec> 345°21''48.75"</sDegMinSec><Distance>7.26992</Distance><DeltaZ>1</DeltaZ></Segment>
  68. <Segment><sDegMinSec>  83° 2''4.652"</sDegMinSec><Distance>151.712</Distance><DeltaZ>2</DeltaZ></Segment>
  69. <Segment><sDegMinSec> 186°34''30.73"</sDegMinSec><Distance>10.3924</Distance><DeltaZ>3</DeltaZ></Segment>
  70. <Segment><sDegMinSec>  86° 2''48.18"</sDegMinSec><Distance>13.3879</Distance><DeltaZ>4</DeltaZ></Segment>
  71. <Segment><sDegMinSec> 187° 5''20.73"</sDegMinSec><Distance>9.09956</Distance><DeltaZ>5</DeltaZ></Segment>
  72. <Segment><sDegMinSec>  89°20''48.85"</sDegMinSec><Distance>61.061</Distance><DeltaZ>6</DeltaZ></Segment>
  73. <Segment><sDegMinSec> 359°49''6.930"</sDegMinSec><Distance>11.6861</Distance><DeltaZ>7</DeltaZ></Segment>
  74. <Segment><sDegMinSec>  96°21''55.47"</sDegMinSec><Distance>11.0401</Distance><DeltaZ>8</DeltaZ></Segment>
  75. <Segment><sDegMinSec> 355° 3''18.45"</sDegMinSec><Distance>28.1448</Distance><DeltaZ>9</DeltaZ></Segment>
  76. <Segment><sDegMinSec>  82°59''21.42"</sDegMinSec><Distance>5.59584</Distance><DeltaZ>10</DeltaZ></Segment>
  77. </Segments>
  78. </Cogo>';
  79. SELECT cogoLine.AsTextZM() AS cogoLineWKT
  80.   FROM (SELECT [$(cogoowner)].[STCogo2Line] (@v_cogo, geometry::STPointFromText('POINT(10 10 -10)',28355), 3, 2) AS cogoLine) AS f;
  81. GO
  82. -- Rsult
  83. -- LINESTRING (10 10 -10, 8.163 17.034 -9, 158.755 35.432 -7, 157.565 25.108 -4, 170.921 26.031 0, 169.798 17.001 5, 230.855 17.697 11, 230.818 29.383 18, 241.79 28.159 26, 239.364 56.199 35, 244.918 56.882 45)

Visually, the result looks like this:

P. Which should be the same as STLine2Cogo.

I hope this function is of interest 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