2

Im trying to find the area of each part in multipart polygons. I know some polygons have multiple parts, so why cant I access the individual polygon parts like this (why is parts list empty):

lyr = iface.activeLayer()
f = next(lyr.getSelectedFeatures())
parts = [p for p in f.geometry().parts()]

print(len(f.geometry().asMultiPolygon()))
print(parts)

2
[]

enter image description here

1

Try to use f.geometry().constParts() instead.

https://qgis.org/pyqgis/3.16/core/QgsGeometry.html?#qgis.core.QgsGeometry.constParts

0

After accessing the selected features, you need to loop on every geometry of the each feature first, then loop again on every part of the feature to calculate the individual acreage of each part of a multipart polygon.

Here is the code that worked for me:

lyr = iface.activeLayer()
features = lyr.getSelectedFeatures()
for feature in features:
    geoms = feature.geometry()
    print('Total area = {0} m2'.format(geoms.area()))
    
    for p in geoms.parts():
        print('Sub-area = {0} m2'.format(p.area()))

enter image description here

The output is:

Total area = 112077414.63030124 m2
Sub-area = 40200853.98864746 m2
Sub-area = 35989267.033569336 m2
Sub-area = 35887293.60803223 m2
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.