# 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. – amenadiel 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/… – Francisco Puga 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. – jurhas 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... – jurhas Jul 5 '15 at 10:48

## 1 Answer

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)
ax = fig.add_subplot(111)
ax.plot(x, y)
poly2 = Polygon ([(0, 0), (5, -10), (30, 0), (0, 0)])
x2,y2 = poly2.exterior.xy
ax.plot(x2, y2)
plt.show()
``````
• My target is to define if two shapes are equal. Doesn't not matter the absolute position, or rotation. I achived it with my code, but I cannot find an easy way to detect if a shape is " mirrored" such in your picture. For my code these two shapes are the same because all the sides and angles are equal. It should be if I could turn it on the other site. But not ever it is possible. – jurhas Jul 4 '15 at 11:11
• ST_NPoints(), ST_Area() and ST_Perimeter() taken together will provide a bit of a "signature" of particular shapes, though if you also allow scale to vary you're down to having to analyze angles, etc, which you'll have to write your own routines for. – Paul Ramsey Jul 8 '15 at 11:49