2014-01-22 51 views
1

我正在嘗試使用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

得到它。

>> LineString([(-9765787.9981184918000000,5488940.9749489054000000), (-9748582.8016368076000000,5488402.1275707092000000)]).within(Point(-9765787.9981184918000000,5488940.9749489054000000)) 
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 

我建議最後一個測試方法來確定點是否在線上。