Skip to main content
1 of 7
MickyT
  • 12.2k
  • 2
  • 26
  • 48

SQL (POSTGIS), 286

This is a query for the PostGIS extension to PostgreSQL. I haven't counted the input values in the total.

WITH I AS (SELECT *FROM(VALUES
-- Coordinate input
(2, 1, 3, 11)
,(1, 10, 5, 19)
,(6, 10, 11, 3)
,(8, 8, 15, 15)
,(13, 13, 9, 5)
,(15, 1, 19, 7)
,(17, 19, 19, 17)
)i(a,b,c,d))SELECT COUNT(*)FROM(SELECT(ST_Dump(ST_polygonize(g))).geom d FROM(SELECT ST_Union(ST_ExteriorRing(ST_MakeBox2D(ST_Point(a,b),ST_Point(c,d))))g FROM i)a)b WHERE ST_XMax(d)-ST_XMin(d)=ST_YMax(d)-ST_YMin(d) AND ST_Area(d)=(ST_XMax(d)-ST_XMin(d))^2

Explanation

The query builds geometries for each coordinate pair. Unions the exterior rings to properly node the lines. Turns the results into polygons and tests the width against height and the area against a side squared.

It will run as a standalone query on any PostgreSQL database with the PostGIS Extension.

MickyT
  • 12.2k
  • 2
  • 26
  • 48