1

我正在尝试使用 Shapely 的'.within'函数......只是一个简单的检查,我无法解决这个语法错误。

我只是想重新创建我在文档中看到的示例。

代码是:

>>> 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
4

2 回答 2

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

我建议使用最后一种测试方法来确定该点是否在线。

于 2014-01-22T22:13:36.717 回答
0

知道了。

>> LineString([(-9765787.9981184918000000,5488940.9749489054000000), (-9748582.8016368076000000,5488402.1275707092000000)]).within(Point(-9765787.9981184918000000,5488940.9749489054000000))
于 2014-01-22T21:08:00.043 回答