Find/Extract Features by largest size (aggregate) 🌎

dudeastronaut

Peter Perkins

Posted on February 14, 2022

Find/Extract Features by largest size (aggregate) 🌎

Say you have multiple shapes within parent shapes; in this case, buildings that belong on parcels of land. We can use the maximum() expression in QGIS to find the largest building on each parcel.

Prerequisites:

  • Keys: Each building shape needs a unique ID and a foreign key that associates it with the parcel (its parent). In this case I found the Pole Of Inaccessibility for each building and used that point in a Join by Location against the parcel to create a foreign key to create a relationship between the buildings and parcels.

  • Size: Each buildings needs a field that defines its area

The Expression:

*We use Extract by Expression tool with the parcels layer selected as the input layer.

"SHAPE_AREA" = maximum("SHAPE_AREA",group_by:="PARCEL_ID")

Image description

Notes:

  • Expression Breakdown: All that needs to be replaced is SHAPE_AREA and PARCEL_ID. Keep the remaining syntax the same. I was not familiar with the group_by:= syntax and thought this was a memo in documentation I read but this is literally part of the function. Image showing the function below.

Image description

  • Processing Time: In my tests I found that the processing time was not linear. What I mean by this is comparing 1000 features does not always take 10 times as long as 100 features. I believe the calculation is ~exponential meaning that the more features you add to compare, the longer the processing time by a factor of x (not sure exactly). If you have a large dataset that you are running this expression against (>50,000 features), I would recommend splitting up the layer you are processing into chunks, run the expression on each, and then merge. You should be able to split the layer without duplicates but if you want to be sure you can always run a Remove Duplicates expression after the merge at the end.

~56,000 features took about 110 minutes to process.

💖 💪 🙅 🚩
dudeastronaut
Peter Perkins

Posted on February 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related