2016-10-06 65 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()