Top 5 Recent Articles
ARTICLES CATEGORIES
- Algorithms (22)
- All (399)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Conversion (2)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (13)
- GPS (1)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- LiDAR (1)
- Linear Referencing (4)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (5)
- Open Source (18)
- Oracle Spatial and Locator (194)
- Partitioning (1)
- PostGIS (36)
- Projections (1)
- Published Articles (1)
- qGIS (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (37)
- Space Curves (9)
- Spatial Database Functions (109)
- Spatial DB comparison (1)
- Spatial XML Processing (11)
- SQL Server Spatial (92)
- Standards (3)
- Stored Procedure (17)
- Tessellation or Gridding (10)
- Tools (2)
- Topological Relationships (1)
- Training (2)
- Triangulation (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
sx | sy | ex | ey | shareCount |
---|---|---|---|---|
10 | 0 | 20 | 0 | 1 |
10 | 20 | 10 | 0 | 1 |
20 | 0 | 20 | 20 | 1 |
20 | 0 | 30 | 0 | 1 |
20 | 20 | 10 | 20 | 1 |
20 | 20 | 20 | 0 | 1 |
30 | 0 | 30 | 20 | 1 |
30 | 20 | 20 | 20 | 1 |
-- 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
sx | sy | ex | ey | shareCount |
---|---|---|---|---|
10 | 0 | 10 | 20 | 1 |
10 | 0 | 20 | 0 | 1 |
10 | 20 | 20 | 20 | 1 |
20 | 0 | 20 | 20 | 2 |
20 | 0 | 30 | 0 | 1 |
20 | 20 | 30 | 20 | 1 |
30 | 0 | 30 | 20 | 1 |
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
Result.
polygon |
---|
POLYGON ((10 0, 20 0, 30 0, 30 20, 20 20, 10 20, 10 0)) |
I hope this function is of interest to someone.
Documentation
- MySQL Spatial General Functions
- Oracle LRS Objects
- Oracle Spatial Exporter (Java + pl/SQL)
- Oracle Spatial Object Functions
- Oracle Spatial Object Functions (Multi Page)
- PostGIS pl/pgSQL Functions
- SC4O Oracle Java Topology Suite (Java + pl/SQL)
- SQL Server Spatial General TSQL Functions
- SQL Server Spatial LRS TSQL Functions