Shapely: MultiPoint.difference(Point) doesn't keep original order

Created on 1 Apr 2019  ·  3Comments  ·  Source: Toblerity/Shapely

Simple example:

>>> from shapely.geometry import MultiPoint, Point
>>> points = MultiPoint([Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])
>>> points.wkt
'MULTIPOINT (0 0, 1 0, 1 1, 0 1)'
>>> bad_point = Point(1, 0)
>>> points.difference(bad_point).wkt
'MULTIPOINT (0 0, 0 1, 1 1)'

As you can see, removing a Point from a MultiPoint by using difference breaks the initial ordering. Expected output would be:

'MULTIPOINT (0 0, 1 1, 0 1)'

Is this a bug or by design? Docs don't say anything on this behaviour.

Shapely version: 1.6.4.post1, installed from conda.

documentation geos wontfix

Most helpful comment

See OGC 06-103r4 for simple feature access, §6.1.5 for MultiPoint:

The Points are not connected or ordered in any semantically important way (see the discussion at GeometryCollection).

So this indicates the behavior is by design. It's also the same behavior in JTS.

A similar object that preserves order is a [Multi]LineString, but the example would need to be processed differently.

All 3 comments

This is a behaviour that is inherited from GEOS, the library that Shapely is a wrapper around.

I don't think this is a bug per se. It just isn't the way the algorithm that calculates the difference works. The two outputs are geometrically equivalent. I don't think the simple feature model places any significance on the order of parts in a collection.

Something explaining this could be added to the documentation. I'm not sure where the best place would be to add it though as this isn't something specific to the difference method.

See OGC 06-103r4 for simple feature access, §6.1.5 for MultiPoint:

The Points are not connected or ordered in any semantically important way (see the discussion at GeometryCollection).

So this indicates the behavior is by design. It's also the same behavior in JTS.

A similar object that preserves order is a [Multi]LineString, but the example would need to be processed differently.

Ok, I see. I think a word of warning that one shouldn't rely on preserving the order could be added in the end of the _Collections_ section.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benediktbrandt picture benediktbrandt  ·  3Comments

sgillies picture sgillies  ·  5Comments

LostFan123 picture LostFan123  ·  3Comments

akadouri picture akadouri  ·  4Comments

MarkWieczorek picture MarkWieczorek  ·  4Comments