0

I would like to retrieve a polygon's geometry from a layer to use it in a cql_filter. Right now, I'm using the code below. It is working, however, it's creating a rectangular geometry out of the polygon.

    # Get the polygon layer
    polygon_layer = QgsProject.instance().mapLayersByName(layer_name)[0]

    # Get the extent of the polygon layer
    extent = polygon_layer.extent()

    # Create a polygon geometry from the extent
    polygon = QgsGeometry.fromRect(extent)

I've tried searching online and reading the QGIS API documentation but I couldn't find what I'm looking for...

5
  • 1
    If it's not doing what you want, then it seems ill-advised to describe it as "working" ("not failing" might be more apt). It's clear that this code should produce a rectangle covering the extent of the geometry. What have you attempted in the way of conveting the geometry into a filter?
    – Vince
    Mar 24, 2023 at 0:50
  • layer.extent() method gives you a rectangle bounding the layer. Your question is unclear. Mar 24, 2023 at 0:53
  • Yes. I would like to get the polygon geometry and not the rectangumar shape of it. However i dont know how to modify my code to achieve that.
    – emanas
    Mar 24, 2023 at 8:02
  • Could you add an image showing exactly what you achieve? Mar 24, 2023 at 8:24
  • My goal is to get the polygon's geometry to use it in a cql_filter. I've tried using the code shared by @Tom Brennan, but i'm getting this error : TypeError: MultiPolygon geometry cannot be converted to a polygon. Only single polygon or curve polygon types are permitted.
    – emanas
    Mar 25, 2023 at 19:40

1 Answer 1

5

A given layer will have multiple features (could be one, but could be many).

So once you have the layer, you need to iterate over the features, and then get the geometry for each feature eg

# Get the polygon layer
for feature in polygon_layer.getFeatures():
    if feature.geometry().isMultipart(): # multipolygon
        for geom in feature.geometry().asGeometryCollection():
            polygon = geom
            <insert further code>
    else:
        polygon = feature.geometry()
        <insert further code>

A cleaner approach to avoid dealing with the difficulties of single/multi part features is to use the parts() or constParts() iterators. constParts() is probably sufficient for your requirements eg:

for feature in polygon_layer.getFeatures():
    for part in feature.geometry().constParts():
        polygon = part
        <insert further code>
2
  • My goal is to get the polygon's geometry to use it in a cql_filter. The code you shared is a huge leap toward what i'm looking for. However im getting this issue : TypeError: MultiPolygon geometry cannot be converted to a polygon. Only single polygon or curve polygon types are permitted.
    – emanas
    Mar 25, 2023 at 19:38
  • I have added some logic for multi-geometry features. Keep in mind though that even single polygons can have multiple parts - an outer ring and multiple inner rings. If your data has these, it's possible your cql_filter will not like them. Mar 25, 2023 at 21:30

Your Answer

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

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