I have a layer with MultiPolygons. Some of them have parts less than five m2 which I want to delete.
The QgsGeometry
class has methods: parts()
and deletePart(self, partNum: int)
. On parts()
I can apply area()
to get the area. https://qgis.org/pyqgis/master/core/QgsGeometry.html
What I am missing, is a method to get the partNum
.
Can someone point me in the right direction?
Here is the code I came up so far. But I doubt that the index of the list asGeometryCollection
is the same as the numPart
index. Alternatively, I could create a new geometry, and loop over the parts, and if a part is > 5, add the part to the new geometry.
# Minimum area threshold
minimum_area = 5
# Loop through all the features in the layer
for feature in layer.getFeatures():
# Retrieve the multipolygon geometry
geometry = feature.geometry()
geom = geometry.asGeometryCollection()
numParts = len(geom)
# loop over parts and numParts ( same index ??)
for part, numPart in zip(geometry.parts(), range(numParts)):
if part.area() < minimum_area:
geometry.deletePart(numPart)