Polygon intersection with line | Python Shapely

I am trying to use shapely to find the intersection of a line and a polygon, but I am having problems with some floating point numbers.

Code example:

polygon = [(4.0, -2.0), (5.0, -2.0), (4.0, -3.0), (3.0, -3.0), (4.0, -2.0)] shapely_poly = shapely.geometry.Polygon(polygon) line = [(4.0, -2.0000000000000004), (2.0, -1.1102230246251565e-15)] shapely_line = shapely.geometry.LineString(line) intersection_line = list(shapely_poly.intersection(shapely_line).coords) print intersection_line 

What I expect is a list of two peaks.

Point 1: the point that will be inside the polygon, or (4.0, -2.0000000000000004) in this case.

Point 2: the point that is the intersection of [(4.0, -2.0000000000000004), (2.0, -1.1102230246251565e-15)] and [(3.0, -3.0), (4.0, -2.0)].

However, I get the result:

 [(4.0, -2.0000000000000004)] 

I also checked if there is any intersection with the edge I am looking at:

 >>> edge = shapely.geometry.LineString([(3.0, -3.0), (4.0, -2.0)]) >>> edge.intersects(shapely_line) False 

If I replaced (4.0, -2.0000000000000004) with (4.0, -2.000000000000000), then the intersection of the edges will be evaluated as True.

Does anyone have any ideas on what is happening or what I don't see? Thanks!

enter image description here

Edit:

I tested using the beautiful version 1.12 and geography 3.3.1, 3.3.5, 3.3.6, 3.3.7.

If anyone is interested in how I updated the version of geoinformation in Windows:

Download the geos- [version] .tar.bz2 file from the GEOS website. Extract the files and run CMake on it using the Visual Studio 10 Win64 generator. After opening the .sln file and building it, he moved the created geos_c.dll file and pasted it on top of where geos_c.dll was installed correctly in the Python directory.

+6
source share
1 answer

Shapely is built on top of the C shell around the C ++ GEOS library. Somewhere deep inside this C ++ library are Precision classes that handle rounding errors. I think we can conclude that your version of Shapely and the geos libraries handle this case differently. Unfortunately, code that accesses the precision model is not available in C api, and therefore not in Shapely. See http://lists.gispython.org/pipermail/community/2011-February/002898.html

Switching to a higher version of the geo-network may solve your problem. It works fine on my machine with beautiful 1.2.16 and libgeos 3.3.5-CAPI-1.7.5.

+3
source

All Articles