通过python获取QGIS中的质心

2024-09-29 17:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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

。。。这给了我一个“name'getCentroid'is not defined”错误。在

我觉得这很奇怪,因为QGIS python编辑器将getCentroid作为一个下拉语法完成选项。在

我还尝试通过feature.getCentroid()将此函数用作feature对象的方法,并收到类似的错误(“QgsFeature”对象没有属性“getCentroid”)。在

类似地,尝试centroid(feature)会给我一个错误“NameError:name'centroid'is not defined”,而feature.centroid()会给我“'QgsFeature'对象没有属性‘centroid’”。在

我是否应该使用其他方法来执行此操作?


Tags: the对象nameis错误counternotfeature
1条回答
网友
1楼 · 发布于 2024-09-29 17:18:43

centroid()是QgsGeometry类的一个方法。 您可以使用geometry()方法检索QgsFeature的几何部分 因此,您可以通过链接两种方法获得质心几何体:

feature.geometry().centroid()

相关问题 更多 >

    热门问题