【问题标题】:Get a centroid in QGIS via python通过python在QGIS中获取质心
【发布时间】:2017-02-15 06:29:03
【问题描述】:

我正在尝试使用 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: name 'centroid' is not defined”,而 feature.centroid() 会出现“'QgsFeature' object has no attribute 'centroid'”。

我应该使用其他方法进行此操作吗?

【问题讨论】:

标签: python geometry qgis centroid


【解决方案1】:

centroid() 是 QgsGeometry 类的一个方法。 您可以使用 geometry() 方法检索 QgsFeature 的几何部分 所以你可以简单地链接这两种方法来获得质心几何:

feature.geometry().centroid()

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2021-04-13
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多