# How to check two 3D Geometry are equal in PostGIS?

I am using following query for checking the given two 3D geometry are equal. The following two input geometry XY are same and Z is different, but query return true. Is any other method available for checking geometry with Z?

SELECT ST_Equals(
ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525))'),
ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 200,500783.607714778 4598542.88397677 200,500777.145896174 4598538.58926913 200,500774.274532224 4598542.90952431 200,500780.736350828 4598547.20423196 200))'))

## 2 Answers

To check if two 3D geometries are equal you can check if

SELECT ST_ASTEXT (ST_3DDifference(geom1,geom2))
FROM (SELECT (ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525))')) AS geom1,
(ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 200,500783.607714778 4598542.88397677 200,500777.145896174 4598538.58926913 200,500774.274532224 4598542.90952431 200,500780.736350828 4598547.20423196 200))')) AS geom2 ) As t;

Result:

TIN Z (((500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525)),((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500780.736350828 4598547.20423196 330.212496351525)))

This means that they are not equal...

SELECT ST_ASTEXT (ST_3DDifference(geom1,geom2))
FROM (SELECT (ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525))')) AS geom1,
(ST_GeomFromText('POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525))')) AS geom2 ) As t;

Result: GEOMETRYCOLLECTION EMPTY

This means that they are equal to...

• Hi this query Query works for all the z of second geometries are same.But it shown error when the second geometry all the heights are matched with first geometry except one vertex height of second is different from first geometries.All the xy are same. – Raja Sep 14 at 15:26
• it's a little hard for me to understand what you mean by translation, but if you think the tool is working wrong, contact the PostGIS development team... or add a wrong example to your question... – Cyril Mikhalchenko Sep 14 at 15:57
• 'POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 330.212496351525,500780.736350828 4598547.20423196 330.212496351525))' 'POLYGON Z ((500780.736350828 4598547.20423196 330.212496351525,500783.607714778 4598542.88397677 330.212496351525,500777.145896174 4598538.58926913 330.212496351525,500774.274532224 4598542.90952431 341.287495989427,500780.736350828 4598547.20423196 330.212496351525))' – Raja Sep 15 at 7:15
• Hi ,I have tested your query with above two geometries the following error message shown. ERROR: Polygon is invalid : points don't lie in the same plane . (Only difference is second geometries 4th vertex height 341.287495989427.) – Raja Sep 15 at 7:17
• So the conclusion is: 1) I showed you a way to check two sets of geodata; 2) If you ask why the algorithm does not give you the difference in one height difference (4), then you should contact the developers... – Cyril Mikhalchenko Sep 15 at 8:32

To check if two 3D geometries are equal you can check if ST_3DMaxDistance(a,b) = 0.

EDIT: ST_OrderingEquals will check exact equality in X, Y and Z

• I have check the query with ST_3DMaxDistance with same two geometries.It return some double values instead of 0 – Raja Sep 14 at 4:18
• Right, I see that. Need to figure out why this is. Looks like probably this approach won't work then. – dr_jts Sep 14 at 6:44