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.
This is the query I've tried. "Zusammengeführt" is the merged shapefile.
1 Answer 1
This can be achieved doing the following steps:
Merge all your shapefiles using
Vector --> Data Management Tools --> Merge Vector Layers
Create a new virtual layer
Layer --> Add Layer --> Add/Edit Virtual Layer
- Enter the following Query:
Select max("id") as "maxid", "geometry" from "Merged" Group by "path"
(See explanation below) - Make sure to select
geometry
as Geometry column andPoint
as Type, hit theAdd
button and set virtual layer's CRS to original/merged shapefiles CRS if needed (Right click layer --> Properties --> Source --> Geometry and Coordinate Reference System
) - 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 layergeometry
selects the geometry of your pointsMerged
is the vector layer of the merged shapefilespath
is a field that will be generated by merging the shapefiles insideMerged
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 maximumid
of each shapefile by grouping them bypath
max()
command selects the maximum value of yourid
field
My Example:
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.
-
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– KATRIN2019年01月07日 09:13:42 +00:00Commented 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– MrXsquared2019年01月07日 10:21:20 +00:00Commented Jan 7, 2019 at 10:21
-
I edited the question and added screenshots. The attribute is a numerical value, i just double checked.KATRIN– KATRIN2019年01月07日 11:50:26 +00:00Commented 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– MrXsquared2019年01月07日 12:56:36 +00:00Commented 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 ofmax("DTM50austri")
MrXsquared– MrXsquared2019年01月07日 13:02:55 +00:00Commented Jan 7, 2019 at 13:02