2014-01-22 45 views
2

我試圖使用shapely的「內」功能來做一個線串和一個點文件的'空間連接'(fyi - 點文件是使用插值函數生成的線串)。問題是 - 什麼都沒有被返回。確定整形點是否在一個線串/多線串

我錯過了什麼?

    for x in range(1, numIntervals): 
        newPt = s.interpolate(interval * x) 
        ## this print statement confirms that the shapely point objects work 
        ## and are being made 
        print newPt 

        ## all things fail within the if/write statement 
        if newPt.within(shape(i['geometry'])): 
         newPt['properties'] = i['properties'] 
         e.write({'geometry':mapping(newPt),'properties':i['properties']}) 

NOTES: 

i = {'geometry': {'type': 'LineString', 'coordinates': [(-9765787.998118492, 5488940.974948905), (-9748582.801636808, 5488402.127570709)]}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'gid', 3)])} 

newPt = POINT (-9763788.9782693591000000 5488878.3678984242000000) 

回答

6

有浮點精度誤差發現線路上的點的時候。改爲使用具有適當閾值的距離。

from shapely.geometry import Point, LineString 

line = LineString([(-9765787.9981184918, 5488940.9749489054), (-9748582.8016368076, 5488402.1275707092)]) 
point = Point(-9763788.9782693591, 5488878.3678984242) 

line.within(point) # False 
line.distance(point) # 7.765244949417793e-11 
line.distance(point) < 1e-8 # True 
+0

嗨。我們如何才能選擇適當的門檻呢?我面臨同樣的問題,只有當我選擇1e-4時,它才適用於我。我們應該選擇什麼基礎?我正在使用wgs84緯度長點 –

+0

我的意思是對於某行,當我使用<1e-4和一些1e-3時,line.distance(point)返回true。這是否意味着,我必須計算點和數據集中所有行之間的距離,並採用最小距離的點?有沒有其他有效的方法呢? –

+0

@ds_user這實際上取決於你如何獲得數據,或者它代表了什麼樣的分辨率(例如見[十進制度](https://en.wikipedia.org/wiki/Decimal_degrees))。您需要檢查GIS中未投影的數據,以獲得您的案例中的想法。 –

相關問題