我在Python中使用OGR Distance来确定点和线之间的最短距离。我的结果与使用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
答案 0 :(得分: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)