我怎样才能制作一个程序来计算 qgis 中的特征属性?

How can I make a program that count features' attributes in qgis?

我制作了计算特征的程序。 代码如下。

from osgeo import ogr
import os
path = 'C:/~'
datasource = driver.open(tree,0)
layer = datasource.Getlayer()
featureCount = layer.GetFeatureCount()
print("path = ", featureCount)

如何添加计算每个属性的代码? 我需要帮助,不是医生

我不是很清楚你所说的计算属性是什么意思。但是一旦你有了一层,它就是 easy to iterate through the features and access the attributes of each feature:

for feature in layer.getFeatures():
     print(feature['name'])
     print(feature[0])

您还可以查询图层以获取每个要素将具有的属性的详细信息:

for field in layer.fields():
    print(field.name(), field.typeName())