2

I am extracting data from selected fields using .getFeatures() in PyQGIS (see code below). The result is correct, but for some reason python delivers some features multiple times (see below; Lamu power project three times and Vipingo twice...). Any ideas why?

vectorLayer = QgsVectorLayer('C:/AIRPOLIM-ES/buf2.gpkg', 'buf2', "ogr")     
QgsProject.instance().addMapLayer(vectorLayer)

rasterLayer = popKE
QgsProject.instance().addMapLayer(rasterLayer)

zoneStat = QgsZonalStatistics (vectorLayer, rasterLayer , 'buf2_pop_', 1,QgsZonalStatistics.Sum)
zoneStat.calculateStatistics(None)

##extract results

for feature in vectorLayer.getFeatures():
    print(feature['Plant'],',',feature['buf2_pop_sum'])

Results:

Kaloleni Cement Plant , 22221449.12682629
Lamu Power Project , 22570960.33722942
Lamu Power Project , 22570960.33722942
Lamu Power Project , 22570960.33722942
Pokot power station , 40265891.87767924
Vipingo power station , 4560350.673526992
Vipingo power station , 4560350.673526992
1
  • 1
    Do you have duplicate geometries or more than one layer in your geopackage?
    – Joseph
    Commented Jul 19, 2019 at 15:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.