Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Shapely-检查直线是否包含点_Python_Numpy_Intersection_Shapely - Fatal编程技术网

Python Shapely-检查直线是否包含点

Python Shapely-检查直线是否包含点,python,numpy,intersection,shapely,Python,Numpy,Intersection,Shapely,我在使用Shapely库检查线串是否包含由与多边形相交而产生的点时遇到问题 示例代码: l = LineString([(5.653154885795476, 6.418676641285647), (6.132921674075812, 5.995573963137367)]) o = LineString([(5, 7.5), (6, 6)]) p = l.intersection(o) # --> p = Point(5.817513045918756, 6.273730431121

我在使用Shapely库检查
线串
是否包含由与
多边形
相交而产生的
点时遇到问题

示例代码:

l = LineString([(5.653154885795476, 6.418676641285647), (6.132921674075812, 5.995573963137367)])
o = LineString([(5, 7.5), (6, 6)])
p = l.intersection(o)
# --> p = Point(5.817513045918756, 6.273730431121867)
但是当运行
l.intersects(p)
l.contains(p)
l.touch(p)
时,我总是得到
False
,这没有多大意义,因为
p
是交叉的结果

我阅读了有关浮点不精确性的内容,在进行上述检查之前添加了以下代码段:

ll = list(line.coords)
for i, v in enumerate(ll):
  ll[i] = Point(np.round(np.array(v), 4))
l = LineString(ll)
p = Point(np.round(np.array(p), 4))
但是没有用


我需要这个来检查
线串的哪个部分与点相交;这是错的吗?有更好的方法吗?

你是对的,这是一个浮点精度问题。您可以使用以下解决方法:

from shapely.geometry import LineString

l = LineString([(5.653154885795476, 6.418676641285647), (6.132921674075812, 5.995573963137367)])
o = LineString([(5, 7.5), (6, 6)])
p = l.intersection(o)

print(l.distance(p)<1e-8)
print(o.distance(p)<1e-8)
True
True