# 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.