There are two methods of determining equality in PostGIS.
- The equals operator
x.geom = y.geom
ST_Equals(x.geom,y.geom)
Obviously these two methods work different, but the docs on ST_Equals
are not clear why,
Returns TRUE if the given Geometries are "spatially equal". Use this for a 'better' answer than '='. Note by spatially equal we mean ST_Within(A,B) = true and ST_Within(B,A) = true and also mean ordering of points can be different but represent the same geometry structure. To verify the order of points is consistent, use ST_OrderingEquals (it must be noted ST_OrderingEquals is a little more stringent than simply verifying order of points are the same).
This seems to mean there is two criterion for ST_Equals
, and that is that
ST_Within(A,B) = true
ST_Within(B,A) = true
However, that ordering of points does not matter with ST_Equals
. Presumably, that would matter with the equal operator then =
? But, that doesn't match up with my observation.
So as three in the same questions..
- What does the equal operator (
=
) do? - What does the
ST_Equals()
do? - What criteria does spatially equal include that
=
does not include?
Answer
= operator means bounding box equality. So you could for example compare a linestring and a polygon and if they have the same bounding box, = will return true. So it's not a true equality that people think of. We've had debates about changing it, but since it's used in group by , order by etc internally by PostgreSQL, and for historical reasons was mapped to that per performance and has been like that since the beginning of PostGIS, it would require a great deal of testing to change it.
ST_Equals is the SQL-MM standard idea of equality, which is based on the DEI9M intersection matrix. Which means if two geometries share exactly the same space, they are equal. So you could have a MULTILINESTRING with just one linestring that is ST_Equal to a LINESTRING. You could have one LINESTRING going right to left and anotehr going left to right that are equal because they share the same space.
A side affect of ST_Equals depending on the intersection matrix is that if you compare two invalid geometries, even if they are exactly the same, they will probably not come back as equal.
ST_OrderingEquals is a bit of a bastard. It's not quite the same as binary equality, but it's close. The internal details always baffled me. You can use it for invalid geometries, and in cases where ST_Equals returns true, it may not return true (like if the direction of linestrings is different, or you are comparing a multi with single). As I recall (don't hold me too it), you can for example represent the same linestring with different points and as long as the direction is the same, it will consider them ordering equal.
No comments:
Post a Comment