3

I have thousands of points in multiple shapefiles. I want to get the maximum value of an attribute of each shapefile. Then I want to merge all the maximum values to a new layer.

Is there a way to extract max values as a batch process from multiple layers?

EDIT:

This is the sample attribute table. The max values I need are from "DTM50austri" from each Shapefile.

Attribute Table

This is the query I've tried. "Zusammengeführt" is the merged shapefile.

This is what I've tried

1 Answer 1

3

This can be achieved doing the following steps:

  1. Merge all your shapefiles using Vector --> Data Management Tools --> Merge Vector Layers

  2. Create a new virtual layer Layer --> Add Layer --> Add/Edit Virtual Layer

  3. Enter the following Query: Select max("id") as "maxid", "geometry" from "Merged" Group by "path" (See explanation below)
  4. Make sure to select geometry as Geometry column and Point as Type, hit the Add button and set virtual layer's CRS to original/merged shapefiles CRS if needed (Right click layer --> Properties --> Source --> Geometry and Coordinate Reference System)
  5. You can now save the virtual layer as shapefile or any other supported format (Right click layer --> Export --> Save Features As...)

Explantation for the query:

  • id is the fieldname of the values you want the maximum's of (see hint below)
  • maxid is the new fieldname for your maximum values in the virtual layer
  • geometry selects the geometry of your points
  • Merged is the vector layer of the merged shapefiles
  • path is a field that will be generated by merging the shapefiles inside Merged vector layer and contains paths to your shapefiles as well as their names; so it is unique for each file --> this allows you to select the maximum id of each shapefile by grouping them by path
  • max() command selects the maximum value of your id field

My Example:

enter image description here

In my example I select the maximum id's.

Additional notes:

Of course this will only work when the maximum values you want to select all have the same column/attribute name in their original shapefiles! If not, you need to generate a new field in the merged shapefile and copy the values over to a new column via field calculator.

6
  • The problem is I get the name of the attribute written in the column "maxid" in the virtual layer and not the numerical values. So the value is always "height" (which is the name of the attribute). I get the correct amount of max values though. Everything's fine in the merged file. Seems like the query doesn't work for me.Any ideas?
    – KATRIN
    Commented Jan 7, 2019 at 9:13
  • can you edit your question and provide a screenshot of your attribute table(s) (just a sample) and post the code for the virtual layer you have tried? Is your attribute formatted as a numerical value? or is it a text field with numbers inside?
    – MrXsquared
    Commented Jan 7, 2019 at 10:21
  • I edited the question and added screenshots. The attribute is a numerical value, i just double checked.
    – KATRIN
    Commented Jan 7, 2019 at 11:50
  • Let me make a wild guess: your screenshot is from the original attribute table and not the merged one, right? Could it be, that the field "DTM50austri" does not exist on the merged one and is called "DTM50austr" instead (10chars limitation for fieldnames of shapefiles)? Otherwise everything looks fine so far...
    – MrXsquared
    Commented Jan 7, 2019 at 12:56
  • On a second look I'm a little confused... Based on your fist comment I think you should write max("height") instead of max("DTM50austri")
    – MrXsquared
    Commented Jan 7, 2019 at 13:02

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.