Go to content Go to navigation and search

Home

Current SQL Server Blog Articles

    SQL Server Spatial: STFlipVectors
    SQL Server Spatial: Extract LineStrings in GeometryCollection to create LineString
    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.

Vectorising geometry objects in SQL Server 2008

Monday September 28 2009 at 11:27

Keywordsvectorize segmentize 2008 SQL Server
Summary

Vectorize or Segmentize line and polygon geometries Sql Server 2008

I demonstrated in this article the usefulness of having a vectorising function in one’s spatial database kitbag (there are GetVector functions in my CENTROID and GEOM packages – in particular the GetVector function is vital to the implementation of the algorithm in my CENTROID.SDO_CENTROID function).

So, I have also added one to my SQL Server 2008 kitbag.

This function, as against the PostGIS and Oracle implementation, does not use any object types due to limitations in the SQL Server 2008 TSQL system (my implementation deliberately does not use the CLR system).

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

Testing

Here’s a bunch of tests.

  1. SELECT v.*
  2.   FROM dbo.GetVector(geometry::STGeomFromText(
  3. 'LINESTRING(0 0 10 10, 1 1 10 20, 2 2 10 30, 3 3 10 40)',0)) AS v;

Result Is:

sx sy sz sm ex ey ez em
0 0 10 10 1 1 10 20
1 1 10 20 2 2 10 30
2 2 10 30 3 3 10 40
  1. SELECT v.*
  2.   FROM dbo.GetVector(geometry::STGeomFromText(
  3. 'MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))',0)) AS v;

Result Is:

sx sy sz sm ex ey ez em
0 0 NULL NULL 1 1 NULL NULL
1 1 NULL NULL 1 2 NULL NULL
2 3 NULL NULL 3 2 NULL NULL
3 2 NULL NULL 5 4 NULL NULL
  1. -- Ordinary polygon
  2. --
  3. SELECT v.*
  4.   FROM dbo.GetVector(geometry::STGeomFromText(
  5. 'POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0))',0)) AS v;

Result Is:

sx sy sz sm ex ey ez em
326000 5455000 NULL NULL 327000 5455000 NULL NULL
327000 5455000 NULL NULL 326500 5456000 NULL NULL
326500 5456000 NULL NULL 326000 5455000 NULL NULL
  1. -- Polygon with a hole
  2. --
  3. SELECT v.*
  4.   FROM dbo.GetVector(geometry::STGeomFromText(
  5. 'POLYGON((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0),
  6. (326500.0 5455500.0,326550.0 5455200.0,326450.0 5455200.0,326500.0 5455500.0))',0)) AS v;

Result is:

sx sy sz sm ex ey ez em
326000 5455000 NULL NULL 327000 5455000 NULL NULL
327000 5455000 NULL NULL 326500 5456000 NULL NULL
326500 5456000 NULL NULL 326000 5455000 NULL NULL
326500 5455500 NULL NULL 326550 5455200 NULL NULL
326550 5455200 NULL NULL 326450 5455200 NULL NULL
326450 5455200 NULL NULL 326500 5455500 NULL NULL
  1. -- MultiPolygon With a hole
  2. --
  3. SELECT v.*
  4.   FROM dbo.GetVector(geometry::STGeomFromText(
  5. 'MULTIPOLYGON(((326000.0 5455000.0,327000.0 5455000.0,326500.0 5456000.0,326000.0 5455000.0),
  6. (326500.0 5455500.0,326550.0 5455200.0,326450.0 5455200.0,326500.0 5455500.0)),
  7. ((321000.0 5450000.0,322000.0 5450000.0,321500.0 5451000.0,321000.0 5450000.0)))',0))  AS v;

Result is:

sx sy sz sm ex ey ez em
326000 5455000 NULL NULL 327000 5455000 NULL NULL
327000 5455000 NULL NULL 326500 5456000 NULL NULL
326500 5456000 NULL NULL 326000 5455000 NULL NULL
326500 5455500 NULL NULL 326550 5455200 NULL NULL
326550 5455200 NULL NULL 326450 5455200 NULL NULL
326450 5455200 NULL NULL 326500 5455500 NULL NULL
321000 5450000 NULL NULL 322000 5450000 NULL NULL
322000 5450000 NULL NULL 321500 5451000 NULL NULL
321500 5451000 NULL NULL 321000 5450000 NULL NULL



But what if you want the start and end of the vector to be geometry points?

  1. -- Get as points
  2. --
  3. SELECT geometry::Point(v.sx,v.sy,0).STAsText() AS startPoint,
  4.        geometry::Point(v.ex,v.ey,0).STAsText() AS endPoint
  5.   FROM dbo.GetVector(geometry::STGeomFromText(
  6. 'LINESTRING(0 0 10 10, 1 1 10 20, 2 2 10 30, 3 3 10 40)',0)) AS v;

Result is:

startPoint endPoint
POINT (0 0) POINT (1 1)
POINT (1 1) POINT (2 2)
POINT (2 2) POINT (3 3)

Finally, what if you want each vector to be represented as a single linestring and not a individual ordinates (sx,sy, …. ,ez,em)?

  1. -- Get as linestring
  2. --
  3. SELECT geometry::STGeomFromText(
  4. 'LINESTRING(' +
  5. CAST(v.sx AS VARCHAR(10)) + ' ' + CAST(v.sy AS VARCHAR(10)) + ',' +
  6. CAST(v.ex AS VARCHAR(10)) +' ' + CAST(v.ey AS VARCHAR(10)) + ')',0).STAsText() AS vLine
  7.   FROM dbo.GetVector(geometry::STGeomFromText(
  8. 'LINESTRING(0 0 10 10, 1 1 10 20, 2 2 10 30, 3 3 10 40)',0)) AS v;

Result is:

vLine
LINESTRING (0 0, 1 1)
LINESTRING (1 1, 2 2)
LINESTRING (2 2, 3 3)

I hope this is useful to someone.

The function is 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