# How to compute equality of polygons in PostGIS independently of their orientation?

I tried some functions in postgis. Scope of work, get the equality of two shapes, independently of their orientation in the space. I detect the shapes from autocad and of course in lifetime of a project, can happen that two shapes in two different draws are oriented int different way. so i tried the very first relationship, get difference of mirrored shapes(this was my very first trouble):

``````select st_equals(
ST_GeomFromEWKT('TRIANGLE ((0 0, 5 10, 30 0, 0 0))'),
ST_GeomFromEWKT('TRIANGLE ((0 0, 5 -10, 30 0, 0 0))'))
``````

And i get False. Perfect. But I was not more happy, when i just tried to rotate the geometry:

``````select st_equals(
ST_GeomFromEWKT('TRIANGLE ((0 0, 5 10, 30 0, 0 0))'),
ST_GeomFromEWKT('TRIANGLE ((0 0, -10 5, 0 30, 0 0))'))
``````

These are the same triangle, just rotate of 90°,I get also False. Is postgis able to define the equality of shapes, also if are rotate in the space?

• You could try comparing the area and the angles. Since angles won't necessarily be in the same order, you'll need to sort them with array functions before comparison. Jul 5 '15 at 2:18
• Also do you consider traslated geometries as equals?, that is a geometry with the same angles and area but in different positions in space. To measure angles probably you can use something like this gis.stackexchange.com/questions/78665/… Jul 5 '15 at 8:58
• I would need to define an unique reference point. And this becomes hard. Currently, I wrote a routine that detect the most significantly side. But if you have a square you have 4 significantly sides. And if you have an hole inside, no way to know where is going to fall. Jul 5 '15 at 9:55
• @Amenadiel : these are mechanical parts, think shape H or C-shape, or more complex, all the angles are equal and a lot of sides are also equal. The best way is to define diagonals. But the comparison is ok. The problem is the mirrored shape. Until now the only thing, is a make analysis of the new shape, identify his 0-point, and than runa comparison point by point. But for square or something with high symmetry which is 0 point? Becomes something pervert... Jul 5 '15 at 10:48

There are a couple of questions about equality in this site

What is the PostGIS 2.x equivalent of the pre-PostGIS 1.5 ~= operator?

ST_Equals Postgis problems

The geometries of your example are not "spatially equals", not share the same space, so st_equals will return always false. What output do you expect?

Code used to generate the image:

``````from matplotlib import pyplot as plt
from shapely.geometry.polygon import LinearRing, Polygon

poly = Polygon ([(0, 0), (5, 10), (30, 0), (0, 0)])
x,y = poly.exterior.xy
fig = plt.figure(1, figsize=(5,5), dpi=90)