0

我在 Python 中使用 OGR 距离来确定点和线之间的最短距离。我的结果与我使用 QGIS 得到的结果完全不同。我假设 OGR 使用的单位取决于坐标系?会不会是 OGR 使用度数?如果是这样,我怎么能将这些转换为米?我的代码如下所示:

import ogr

driver = ogr.GetDriverByName('ESRI Shapefile')

roads = driver.Open('G:/Basedata/OR/infra/TigerRoads2010/OR_TIGERroads_2010_merge.shp', 0)
point = driver.Open('U:/My Documents/Tool/shp/testareacentro.shp', 0)

roadslayer = roads.GetLayer()
pointl = point.GetLayer()

roadsfeature = roadslayer.GetNextFeature()
pointf = pointl.GetNextFeature()

roadgeom = roadsfeature.GetGeometryRef()
pointgeom = pointf.GetGeometryRef()

dist = pointgeom.Distance(roadgeom)

print dist
4

1 回答 1

0

我的距离之所以关闭,是因为我只比较了第一个特征。这将产生与 QGIS 相同的结果:

import ogr

driver = ogr.GetDriverByName('ESRI Shapefile')

lineshp = driver.Open('U:/My Documents/Tool/shp/line.shp', 0)
linelyr = lineshp.GetLayer()

pointshp = driver.Open('U:/My Documents/Tool/shp/point.shp', 0)
pointlyr = pointshp.GetLayer()

linefeat = linelyr.GetNextFeature()
pointfeat = pointlyr.GetNextFeature()

point_geom = pointfeat.GetGeometryRef()

distlist = []
while linefeat:
    line_geom = linefeat.GetGeometryRef()
    dist = point_geom.Distance(line_geom)
    distlist.append(dist)
    linefeat.Destroy()
    linefeat = linelyr.GetNextFeature()

print min(distlist)
于 2013-03-07T18:15:16.070 回答