Python Shapely - 检查线是否包含一个点

问题描述

我在检查 Linestring 是否包含 Point 时遇到问题,该 polygon 是使用 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.touches(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)) 是十字路口。

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

Linestring

但无济于事。

我需要这个来检查 if (command === 'inv') { const getInv = async () => { const response = await axios.get( 'https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100',); return response.data; }; const invValue = await getInv(); let total = 0; invValue.data.forEach((item) => { message.channel.send(`${item.name} \n ${item.recentAveragePrice}`); total += item.recentAveragePrice; }); message.channel.send(`Total average price: ${total}`); } 的哪一部分与点相交;这是错误的吗?有没有更好的方法来做到这一点?

解决方法

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

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