2016-10-06 125 views
1

我想要使用python獲取QGIS中多邊形的質心。這是我的代碼通過python獲取QGIS中的質心

layerPluto = iface.addVectorLayer("/path/to/mn_mappluto_16v1/MNMapPLUTO.shp", "PLUTO", "ogr") 
features = layerPluto.getFeatures() 
counter = 0 
for feature in features: 
    # Get the first feature from the layer 
    if counter < 3: 
     print feature['Address'] 
     print getCentroid(feature) 
     counter += 1 

......這給我一個「名稱」getCentroid'未定義「錯誤。

我覺得這很令人驚訝,因爲QGIS python編輯器有​​作爲下拉語法完成選項。

我也試過使用這個函數作爲特徵對象的方法,通過feature.getCentroid()並且收到了類似的錯誤(「'QgsFeature'對象沒有屬性'getCentroid'」)。

同樣,嘗試centroid(feature)給我錯誤「NameError:name'centroid'is defined」,而feature.centroid()給我「'QgsFeature'對象沒有屬性'centroid'」。

是否有另一種方法我應該使用此操作?

+1

檢查這個職位。 http://gis.stackexchange.com/questions/209292/getting-x-y-of-polygon-in-pyqgis –

回答

1

centroid()是一個QgsGeometry類的方法。 您可以geometry()方法 檢索QgsFeature的幾何部分,這樣你就可以得到質心幾何siply鏈接的兩種方法:

feature.geometry().centroid()