确定匀称点是否在线串/多线串内

时间:2014-01-22 19:06:27

标签: python graph point shapely multilinestring

我试图使用shapely的'within'函数来做一个Linestring和一个点文件的'空间连接'(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)

1 个答案:

答案 0 :(得分:13)

在线上找到点时存在浮点精度错误。请改为使用具有适当阈值的距离。

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