# Detecting polygon shape with PostGIS

I've got a polygon table with a UTM 10x10 km grid in a PostgreSQL/PostGIS database and I need to identify those cells that are not a regular square (i.e. changes in UTM numbers):

Following the comments below, I've built this code to calculate the difference in lenght of two consecutive sides of each cell:

``````with tabla as (
select utmcode,
ST_Distance(ST_PointN(ST_ExteriorRing(geom), 1),
ST_PointN(ST_ExteriorRing(geom), 2)) AS d1,
ST_Distance(ST_PointN(ST_ExteriorRing(geom), 2),
ST_PointN(ST_ExteriorRing(geom), 3)) AS d2
from malla_p)
select utmcode, abs(round(d1-d2)) AS difference
from tabla
;
``````

But it returns, for example, `difference` = 0 for this cell:

or `difference` = 498 for this other:

How can it be solved?

• Check out this answer: gis.stackexchange.com/a/366837/120129 Dec 11 '21 at 13:49
• Do you know where to download your grid? Or did you create it yourself?
– BERA
Dec 11 '21 at 18:16
• why not just selecting geometries whose area is not 100km2?
– JGH
Dec 12 '21 at 13:29
• the issue with computing distances between points is that you must be sure that the polygon has only 4 points (+1 for the last point)
– JGH
Dec 12 '21 at 13:30
• Possible workflow - extract vertices > split exterior rings with vertices > count number of segments/flag polygons with more than 4 > flag polygons with less than 2 matching segment lengths. Dec 13 '21 at 21:53

``````SELECT * from the_table
• You should probably add some tolerance depending on the application - probably don't need full floating point accuracy - `where round(st_area(geom)::numeric,1) != round(st_area(st_orientedenvelope(geom))::numeric,1)` - adjust the rounding to suit your level of tolerance. Dec 16 '21 at 20:50