我正在尝试使用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编辑器具有getCentroid
下拉语法完成选项。
我也尝试通过将此功能用作特征对象的方法,feature.getCentroid()
并收到了类似的错误(“'QgsFeature'对象没有属性'getCentroid'“)。
类似地,尝试centroid(feature)
给我错误“ NameError:名称'centroid'未定义”,而feature.centroid()
给我“'QgsFeature'对象没有属性'centroid'”。
我应该使用另一种方法进行此操作吗?
centroid()是QgsGeometry类的方法。您可以使用geometry()方法检索QgsFeature的几何部分,因此可以通过链式链接这两种方法获得质心几何:
feature.geometry().centroid()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句