question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour using difference with Polygons

See original GitHub issue

Expected behavior and actual behavior.

Removing the intersection (difference) of two Polygons from both Polygons should result in two MultiPolygons which consist of two Polygons. However, the actual returned objects are a Polygon and a MultiPolygon.

The images below show the problem (especially the last one where two “visually” disconnected polygons are treated as one)

Intersecting Polygons: alt text alt text

Intersection: alt text

Polygon (which should be a MultiPolygon): alt text

Edit1: Enabling edges shows the connection between both polygons: alt text

Edit2: Interesting enough, a.symmetric:_difference(b) returns a MultiPolygon with 4 Polygons as expected

Steps to reproduce the problem.

from shapely.geometry import Polygon as plg

a = plg([(489.73, 0), (498.96, 0), (662.21, 282.74), (652.97, 282.74)])
b = plg([(494.06, 282.74), (484.83, 282.74), (648.07, 0), (657.31, 0)])

inter = a.intersection(b) # is a Polygon

a_wo_inter = a.difference(inter) # should be a MultiPolygon but its not! :(
b_wo_inter = b.difference(inter) # is a Multipolygon

print("Intersection:", inter)
print("Polygon A without intersection:", a_wo_inter)
print("Polygon B without intersection:", b_wo_inter)

Operating system

Windows 10 Pro (1803) Build: 17134.112 Debian 8.8 Kernel Version: 4.13.0-1011-gcp

Shapely version and provenance

Installed using conda (also tried using the windows-wheel) shapely 1.6.4 py36h2a969d5_0 (Python 3.6.4)

Installed using repl.it repl-link Shapely-1.6.4.post1-cp36-cp36m-manylinux1_x86_64.whl (Python 3.6.1)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jorisvandenbosschecommented, Oct 17, 2022

@Korred another two years, and just to confirm that those changes have landed in GEOS ànd are exposed in Shapely 2.0 (beta version released to test). Using your example from above:

>>> a.difference(inter)
<POLYGON ((652.97 282.74, 662.21 282.74, 498.96 0, 489.73 0, 568.9 137.126, ...>

>>> a.difference(inter, grid_size=0.01)
<MULTIPOLYGON (((498.96 0, 489.73 0, 568.9 137.13, 573.52 129.13, 498.96 0))...>

(and visually inspecting this resulting MultiPolygon shows that it is now the expected result)

1reaction
dr-jtscommented, Jun 11, 2020

@dr-jts Incredible to get a response to something I posted 2 years ago 😮

I’m going through all the tickets I can find which report problems with JTS/GEOS overlay, so I can verify how the new code works on them. So far am batting 100… 😃

Since Shapely is a wrapper around GEOS, which in turn is a port of JTS, I will have to wait for both the JTS and GEOS changes in order to use the new code, am I right?

Correct. Hopefully the GEOS changes will land by Q3 2020.

Is there another Python wrapper that uses JTS directly?

Not that I’m aware of.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected behaviour using difference with Polygons #605
This isn't a bug. It's expected behaviour when computing overlay operations using finite-precision floating point arithmetic. The results of ...
Read more >
python - Unexpected intersects behavior between GeoSeries ...
One way to accomplish what I desired is through the following method: from shapely.geometry import MultiPoint single_shapely_compare ...
Read more >
Polygons and arc curves - unexpected behavior
Hi, While digitizing in ArcPro 2.4.1, I've noticed some unexpected behavior while creating polygons using straight lines and arc curves.
Read more >
Unexpected behaviour when trying to delete all polygons of ...
Hi, Create a cube in Maya 2015, or 2016, go into component mode and delete all polygons. Result is there is always 1...
Read more >
#1064 (Topology preserve simplify: unexpected behavior on similar ...
The following WKT polygons are essentially the same polygon, each time with a different first/last vertex: Polygon((0 42, 0 100, 42 100, 100...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found