Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if Shapely point is within a LineString/MultiLineString

I am trying to use Shapely's within function to do a 'spatial join' of a LineString and a Point file (FYI, the point file was generated using the interpolate function on the LineString). Problem is - nothing is being returned.

# this condition is never satisfied
if point.within(line):
    # here I write stuff to a file

where:

point = POINT (-9763788.9782693591000000 5488878.3678984242000000)
line = LINESTRING (-9765787.998118492 5488940.974948905, -9748582.801636808 5488402.127570709)

What am I missing?

like image 935
user14696 Avatar asked Jan 22 '14 19:01

user14696


People also ask

Is Point in polygon shapely?

There are basically two ways of conducting Point in Polygon queries in Shapely: using a function called . within() that checks if a point is within a polygon. using a function called .

What is LineString in Python?

Geometry types Point (*args) A geometry type that represents a single coordinate with x,y and possibly z values. LineString ([coordinates]) A geometry type composed of one or more line segments. LinearRing ([coordinates])

Is Point in polygon Python?

How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.

What is Unary_union?

unary_union. Returns a geometry containing the union of all geometries in the GeoSeries .


1 Answers

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

from shapely.geometry import Point, LineString

line = LineString([(-9765787.9981184918, 5488940.9749489054), (-9748582.8016368076, 5488402.1275707092)])
point = Point(-9763788.9782693591, 5488878.3678984242)

line.within(point)  # False
line.distance(point)  # 7.765244949417793e-11
line.distance(point) < 1e-8  # True
like image 196
Mike T Avatar answered Sep 28 '22 02:09

Mike T