2013-03-06 111 views
0

我在Python中使用OGR距離來確定點和線之間的最短距離。我的結果與使用QGIS的結果完全不同。我假設OGR使用的單位取決於座標系? 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 
+1

我期望的單位是輸入的單位。 – askewchan 2013-03-06 19:55:58

+0

如何找出輸入的單位是什麼? – ustroetz 2013-03-06 19:58:37

+0

你必須看看輸入來自哪裏。如果你給一個點作爲元組,這些數字代表什麼? – askewchan 2013-03-06 20:01:54

回答

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)