我正在尝试使用Shapely中的'.within'函数...只是一个简单的检查,我无法解决此语法错误。
我只是想重新创建我在docs.
中看到的示例代码是:
>>> from shapely.geometry import Point, LineString
>>> LineString([(-9765787.9981184918000000 5488940.9749489054000000, -9748582.8016368076000000 5488402.1275707092000000)]).within(Point(-9763788.9782693591000000 5488878.3678984242000000))
返回:
>> Traceback ( File "<interactive input>", line 1
LineString([(-9765787.9981184918000000 5488940.9749489054000000, -9748582.8016368076000000 5488402.1275707092000000)]).within(Point(-9763788.9782693591000000 5488878.3678984242000000))
^
SyntaxError: invalid syntax
答案 0 :(得分:1)
您有两种选择:
1)加载WKT字符串:
from shapely.wkt import loads as wkt_loads
line = wkt_loads('LINESTRING(-9765787.9981184918000000 5488940.9749489054000000, -9748582.8016368076000000 5488402.1275707092000000)')
point = wkt_loads('POINT(-9763788.9782693591000000 5488878.3678984242000000)')
2)将格式正确的坐标对列表格式化为常规Python浮点数:
from shapely.geometry import Point, LineString
line = LineString([(-9765787.9981184918000000, 5488940.9749489054000000), (-9748582.8016368076000000, 5488402.1275707092000000)])
point = Point(-9763788.9782693591000000, 5488878.3678984242000000)
接下来,您将在测试中遇到一些浮点精度错误:
line.within(point) # False
line.distance(point) # 7.765244949417793e-11
line.distance(point) < 1e-8 # True
我建议使用最后一种测试方法来确定该点是否在线上。
答案 1 :(得分:0)
得到了它。
>> LineString([(-9765787.9981184918000000,5488940.9749489054000000), (-9748582.8016368076000000,5488402.1275707092000000)]).within(Point(-9765787.9981184918000000,5488940.9749489054000000))