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.

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