Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...