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.

Inconsistencies with respect to the Polygon `within` and `intersection` methods.

See original GitHub issue

Expected behavior and actual behavior.

I’ve been trying to use the intersection method to compute the intersection region of the 2 given shapely.geometry.Polygon instances. I was initially trying to verify the intersection results (let’s call it res) by checking if a point I know that lies in the result using the contains method.

I was then trying to dig into the source of this issue and I found that for a few cases the intersection result res when passed through poly.contains(res), the method returns False where ideally it is supposed to return a True value as the intersection of the polygon is supposed to be a part both the Polygons.

Steps to reproduce the problem.

  • Adding it in the comments below as I was mainly dealing with huge GeoJSON data.

Operating system

Mac OS X Mojave, Ubuntu 18.04

Shapely version and provenance

Shapely - 1.7.0

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mwtoewscommented, Aug 19, 2020

Fundamentally, this is a floating point precision issue, and is not entirely a shapely issue. Yes, with a perfect precision model I would expect the result of an intersection of two geometries to be contained by each geometry. However, this cannot always be safely represented with a double precision model used by Shapely. JTS fails with this specific example too, except when the precision model is changed to a fixed with scale 1000.0.

You can apply a negative buffer with a very small amount, such as a number near machine epsilon of the fraction of square root of area of the intersected area, which should return the desired result:

from math import sqrt
eps = 1e-14
res2 = res.buffer(-sqrt(res.area) * eps)
assert shapely_poly1.contains(res2)
assert shapely_poly2.contains(res2)

note that these two geometries are nearly the same:

res2.area  # 2742.7512489986843
res.area   # 2742.7512489986875
assert abs(res2.area - res.area) / res.area < 1e-12
0reactions
RavicharanNcommented, Aug 20, 2020

Thanks for clarifying!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Evaluation of Topological Consistency in CityGML - KIT
As an application, it is examined which matrices can occur as intersection matrices, and how matrices from topologically consistent data look.
Read more >
Problems intersecting large polygon shapefiles in QGIS (V2 ...
My method so far has been to intersect these two layers to split the distributions into the hexagonal grid cells. From this I...
Read more >
THE POLYGON CONTAINMENT PROBLEM - DTIC
lationships between polygons involves computing their intersection or testing for inclusion of one within the other. All these problems have been.
Read more >
Point in polygon - GIS Wiki | The GIS Encyclopedia
Another algorithm is to compute the given point's winding number with respect to the polygon. If the winding number is non-zero, the point...
Read more >
Intersection Polygon - an overview | ScienceDirect Topics
All methods have one thing in common—they must find points of intersection between pairs of edges. The edges on which the intersection points...
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