The equals operation in Shapely

Posted on

Shapely provides two ways of testing the equivalence of geometries:

  • Using the == operator, e.g. a == b
  • Using the .equals method, e.g. a.equals(b)

The result of the two methods are not identical, although it may appear that way at first.

For example, take two points:

>>> A = Point([1, 2])
>>> B = Point([1, 2])
>>> A == B
True
>>> A.equals(B)
True

So far so good, but what about a more complex example?

>>> A = LineString([(1, 2), (3, 4)])
>>> B = LineString([(3, 4), (1, 2)])
>>> A == B
False
>>> A.equals(B)
True

The difference is that the == operator does a comparison of the coordinate sequences of the geometries (in addition to checking their type), while the .equals method is a test of geometric equivalence. Another way to think of this is as a test for when the symmetric difference of two geometries is empty, i.e. neither geometry has a part that the other doesn't.

Another example of this is two geometries that have different coordinate sequences but represent the same geometry.

>>> A = LineString([(0, 0), (5, 0)])
>>> B = LineString([(0, 0), (2, 0), (5, 0)])
>>> A == B
False
>>> A.equals(B)
True

This also means that two geometries are considered equivalent by .equals even if they have different types, so long as they are geometrically equivalent.

>>> A = Point(1, 2)
>>> B = MultiPoint([(1, 2)])
>>> A == B
False
>>> A.equals(B)
True

Shapely uses GEOS’s GEOSEquals method internally. There is also a GEOSEqualsExact method, exposed as .equals_exact, which allows a tolerance in the comparison.

>>> A = Point([1, 2])
>>> B = Point([1, 2.5])
>>> A.equals_exact(B, tolerance=0.0)
False
>>> A.equals_exact(B, tolerance=1.0)
True

So which method should you use? This depends on what your data represents. Both methods are useful in different situations.