Identifying Quadrilateral Polygon Geometries in SQL Server Spatial

Introduction

This article introduces a a function to identify quadrilaterally shaped polygon objects in SQL Server Spatial

Discussion

Some geometric processing cannot, or should not, be carried out on quadrilateral polygon geometries.

An situation where one does not want to process quadrilateral polygons is where one uses the STLineToCurve function to convert a LineString/Polygon to a geometry with CircularStrings and the passed geometry is a 5 point polygon that is a square not a stroked square (impossible).

It seems strange to create a function that is mainly to be used in the negative, but at the moment the current use case is just that.

What are Quadrilateral Polygon Geometries

Here is a list of quadrilateral objects.

The quadrilateral polygons this function detects are:

1. Square,
2. Rectangle,
3. Rhomboid,
4. Isosceles Trapezium,
4. Parallelogram.

STIsQuadrilateral

The identification of quadrilaterial polygons is encapsulated in the STisQuadrilateral function.

This is defined as follows.

Function STIsQuadrilateral (
  @p_polygon   geometry,
  @p_precision integer = 3
)
Returns bit /* True if is a Quadrilateral geometry, 0 otherwise. */

The parameters are: 

@p_polygon  (geometry) - A 5 point polygon
@p_precision (integer) - Precision of length of a side or diagonal

The function detects any of the above quadrilateral polygon objects at any angle, but does not detect Irregular Quadrilaterals.

Detection is done via comparing sides or diagonals (to desired precision).

Examples.

with data as (
  select 'Square' as qType, geometry::STGeomFromText('POLYGON((50 0,65 0,65 15,50 15,50 0))',0) as geom
  union all
  select 'Rectangle' as qType, geometry::STGeomFromText('POLYGON((80 0,110 0,110 10,80 10,80 0))',0)
  union all
  select 'Isosceles Trapezium' as qType, geometry::STGeomFromText('POLYGON ((60 20, 70 20, 68 30, 62 30, 60 20))',0)
  union all
  select 'Rhombus' as qType, geometry::STGeomFromText('POLYGON((20 0,40 10, 20 20,0 10, 20 0))',0)
  union all
  select 'Rectangle at Angle' as qType, geometry::STGeomFromText('POLYGON ((72.929 -15.355, 83.536 -4.749, 72.929 5.858, 62.322 -4.749, 72.929 -15.355))',0)
  union all
  select 'Complicated Polygon' as qType, geometry::STGeomFromText('POLYGON ((24.23 -10.83, 32.17 -12.89, 22.13 -29.44, 47.39 -35.11, 65.19 -21.21, 46.66 -27.6, 53.1 -12.5, 37.91 -17.61, 44.3 -4.65, 32.04 1.44, 24.23 -10.83))',0)
)   
select qType, [dbo].[STIsQuadrilateral](geom,1) as isQuadrilateral
  from data as a;
GO

qType               isQuadrilateral
Square              1
Rectangle           1
Isosceles Trapezium 1
Rhombus             1
Rectangle at Angle  1
Complicated Polygon 0

And here’s what each of these look like.

Objects Processed by STIsQuadrilateral
Geometry objects to be processed by STIsQuadrilaterial

Dependency

This function is used by STLineToCurve.