Wednesday 6 March 2019

python - shapely within and contain methods don't work the way I think they should


I am using Python 3.6 with the latest version of shapely from Anaconda.


from shapely.geometry import Point,LineString
g = LineString(coordinates=[(0, 0), (6.656423206909781, 4.437570291332059)])
p =wkt.loads('POINT (4.160264504318614 2.773481432082537)')
g.contains(p)
>>> False


The point p is clearly within geometry g, Since:


g.interpolate(5).xy
>>> (array('d', [4.160264504318614]), array('d', [2.773481432082537]))

Why is the above statement evaluating to false when I think it should be true?



Answer



Simply look at the answer of Mike T in Determine if shapely point is within a linestring/multilinestring



There are floating point precision errors when finding a point on a line. Use the distance with an appropriate threshold instead.




 g.distance(p) < 1e-8  # True
True

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...