Top 5 Recent Articles
- Algorithms (13)
- All (401)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (12)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- Linear Referencing (3)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (3)
- Open Source (18)
- Oracle Spatial and Locator (187)
- PostGIS (33)
- Published Articles (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (35)
- Space Curves (9)
- Spatial Database Functions (102)
- Spatial DB comparison (1)
- Spatial XML Processing (10)
- SQL Server Spatial (General) (83)
- SQL Server Spatial (LRS) (38)
- Standards (1)
- Stored Procedure (15)
- Tessellation or Gridding (9)
- Tools (2)
- Training (2)
STFlipVectors: Normalize direction of linestring vectors
In the past I have found use for the ability to “flip” vectors in a polygon in order to provide an independent check on the topological integrity of planar enforced polygons.
What I mean by topological integrity is that there are no gaps or overlaps between adjacent polygons.
A simple method for doing this relies on the fact that if a linestring is part of a shared boundary there will be two versions of it: one in one polygon and one in the other.
If they are exactly the same then there cannot be a gap or an overlap.
With normal ring ordering (anticlockwise/clockwise vertex order for their outer/inner rings), the segments that define a specific part of a the polygon boundary will have different direction.
This can be seen in the following diagram.
Two segments with different direction will not be metrically equal because their start and end points are different due to their different directions.
To make them the same requires a process that will normalize the direction.
I call this Flipping.
This flipping is different from simply reversing the coordinate order of a linestring segment.
After all, if we flip each of the two segments with different direction, we will still end up with two segments with different direction!
Hence we need a process that provides that all segments are flipped to the same direction.
This is what STFlipVectors() does, whose call signature is:
CREATE FUNCTION [dbo].[STFlipVectors] ( @p_line_collection geometry ) RETURNS geometry AS BEGIN ... END
Its documentation can be seen see here.
Here are some examples of how to use it:
-- Process geometry extracting segments with no flipping WITH gc AS ( SELECT geometry::STGeomFromText( 'GEOMETRYCOLLECTION( POLYGON((10 0,20 0,20 20,10 20,10 0)), POLYGON((20 0,30 0,30 20,20 20,20 0)), POINT(0 0))',0) AS geom ) SELECT v.sx,v.sy,v.ex,v.ey,COUNT(*) AS shareCount FROM gc AS a CROSS apply [dbo].[STVectorize](a.geom) AS v GROUP BY v.sx,v.sy,v.ex,v.ey GO
Note: 8 Rows Produced
-- Process geometry extracting segments with flipping -- WITH gc AS ( SELECT geometry::STGeomFromText( 'GEOMETRYCOLLECTION( POLYGON((10 0,20 0,20 20,10 20,10 0)), POLYGON((20 0,30 0,30 20,20 20,20 0)), POINT(0 0))',0) AS geom ) SELECT v.sx,v.sy,v.ex,v.ey,COUNT(*) AS shareCount FROM gc AS a CROSS apply [dbo].[STVectorize] ( [dbo].[STFlipVectors] ( a.geom ) ) AS v GROUP BY v.sx,v.sy,v.ex,v.ey GO
Note: 7 Rows Produced
Note that one segment, the shared boundary, is recorded as existing twice.
What can we do with such information?
There are many things one can do, for example provide an independent check that data editing has not created gaps or overlaps at particular tolerances.
But we will concentrate here on a simple implementation of a boundary “dissolve” operation.
(Yes this can be done via STUnion() but here we are simple demonstrating a use for flipped vector processing.)
WITH gc AS ( SELECT geometry::STGeomFromText( 'GEOMETRYCOLLECTION( POLYGON((10 0,20 0,20 20,10 20,10 0)), POLYGON((20 0,30 0,30 20,20 20,20 0)), POINT(0 0))',0) AS geom ) SELECT geometry::STGeomFromText( REPLACE( geometry::UnionAggregate(vector).STAsText(), 'LINESTRING (' COLLATE DATABASE_DEFAULT, 'POLYGON ((' COLLATE DATABASE_DEFAULT ) + ')' COLLATE DATABASE_DEFAULT, 0).STAsText() AS polygon FROM (SELECT [dbo].[STMakeLine] ( geometry::Point(v.sx,v.sy,0), geometry::Point(v.ex,v.ey,0), 3,2 ) AS vector FROM gc AS a CROSS apply [dbo].[STVectorize] ( [dbo].[STFlipVectors] ( a.geom ) ) AS v GROUP BY v.sx,v.sy,v.ex,v.ey HAVING COUNT(*) = 1 /* Get rid of duplicate lines */ ) AS f GO
|POLYGON ((10 0, 20 0, 30 0, 30 20, 20 20, 10 20, 10 0))|
I hope this function is of interest to someone.