7

I have MultiPolygon geometries in my file, and I want to take only the largest polygon within the MultiPolygon geometry to further work with it.

I tried using the "Multipart to singleparts" tool, which allowed me to have single polygons. Now I suppose I could filter for non-unique "ID"s and have QGIS calculate the largest out of its geometries. I am not sure how to do this, and any alternative methods could be beneficial as well!.

5
  • Does this answer your question? Selecting largest polygon in QGIS
    – Erik
    Commented Jan 6, 2021 at 9:46
  • 3
    After you generated the single parts from mutli parts, you can now calculate the area per polygon. See here gis.stackexchange.com/questions/23355/… After you have the areas, then you can filter the largest one.
    – Nil
    Commented Jan 6, 2021 at 9:47
  • 2
    If you have to select the largest polygon with non-unique IDs, use this function as QGIS expression : aggregate(@layer, 'max', $area, filter:="id"=attribute(@parent, 'id')) = $area, it gets the max area of the layer filter by ID and returns True (or 1) if it equals the current feature area. Commented Jan 6, 2021 at 10:33
  • 2
    You could also use the following QGIS expression (using select by expression from the toolbox for expample): coalesce( order_parts( $geometry, area($geometry), False)), see: gis.stackexchange.com/a/335879/88814 - no need to use Multipart to Single part
    – Babel
    Commented Jan 6, 2021 at 10:41
  • Please, do not forget about "What should I do when someone answers my question?"
    – Taras
    Commented Nov 18, 2021 at 20:17

4 Answers 4

6

You can achieve this in a single step using a virtual layer.

The idea is to call QGIS function to order the multi part, as suggested by @Babel, and to select the 1st (biggest) one. The difference between the two approaches is how the biggest part is retained, and that a virtual layer is dynamic, i.e. if you modify the source shapes, it will be automatically recomputed.

Go to the menu Layer > Add Layer > Add/Edit Virtual Layer... and enter the following query. Replace the layer name for yours and feel free to add any field you want from the source.

SELECT id, -- any field you want from the source layer
       geometry_n(
         order_parts(geometry, area(geometry), False)
         ,1) as geometry
FROM myMultiPartLayer

enter image description here

6

Let's assume there is a multi-polygon layer called 'polygons' with its attribute table, see image below

input

Step 1. Apply the "Multipart to singleparts" geoalgorithm

step_1

Step 2. Use a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... with the following query:

SELECT
    *
FROM
    "Single parts"
GROUP BY
    "id"
HAVING
    MAX(area(geometry))

And get the output

result

1
  • What happens with this solution if there is a tie for the max? Commented Jan 7, 2021 at 16:05
4

Here's another alternative using a virtual layer

Go to Layer > Add Layer > Add/Edit Virtual Layer... and enter the following prompt:

WITH
tbla AS (SELECT id, st_area(geometry) area, geometry FROM "Single parts")
SELECT id, MAX(area) max_area, geometry FROM tbla

I hope that your result meets your expectations...

3

As an alternative, you can get the biggest polygons using exclusively QGIS expressions. Similar to @JGH's proposal, but you don't have to create a virtual layer, but it will still adapt dynamically to changing geometries if using geometry generator. In this case, you need no additional layer. This is if you need it for to visualize or for styling only.

However, if satisfied, you can transform it to real geometries with the same expression.

This is the expression to use:

geometry_n(  
    order_parts( 
        $geometry,
        'area($geometry)', 
        'false'
    )
    ,1
)

You have two possibilities:

  1. For visualization purpose only, without creating actual geometries: add a new symbol layer, set it to Geometry generator / Geometry type: line and paste the expression (this is what you see on the screenshot).

  2. Create actual geometries you can use for further processing: Menu Processing / Toolbar / Geoemtry by expression, than set the multipolygon layer as input, set polygon as output type and paste the expression from above.

enter image description here

0

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.