I am building a Processing Model working with large vector Datasets with classification values. My vector file contains a lot of features (40.000) with one column containing classification values ranking from min (e.g. 1) to max (e.g. 15). Note that the min and max values of the classification can vary from Model-run to Model-run. I need to find a way to select all the features with the highest classification value to create a Buffer around them in a next step.
I already have a python script that writes the maximum value of a selected column in a number that than can be used in Processing:
##MaxFinder=name
##Layer=vector
##Fields=Field Layer
##value_KRK_max=output number 0
layer = processing.getObject( Layer )
idx = layer.fieldNameIndex( Fields )
KRK_max = layer.maximumValue(idx)
My idea was to define a function in the "Select by expression" tool, but I cannot make it work. Any suggestions?
2 Answers 2
You're half way there! As you and @spatialthoughts mentioned, the next steps would be to select the features with the maximum values and then buffer those. Fortunately, those tools already exist in the Processing plugin so we can just call these up instead of defining a new function:
Here's your modified code with a couple of added parameters:
##MaxFinder=name
##Layer=vector
##Fields=Field Layer
##value_KRK_max=output number 0
##buffer_distance=number 0
##Result=output vector
import processing
layer = processing.getObject( Layer )
idx = layer.fieldNameIndex( Fields )
KRK_max = layer.maximumValue(idx)
# Set expression for features in Fields to equal max value
expr = '"%s" %s %s' % (Fields, '=', KRK_max)
processing.runalg("qgis:selectbyexpression", layer, expr, 0)
processing.runalg("qgis:fixeddistancebuffer", layer, buffer_distance, 99, False, Result)
Testing the script with a point example. The labels show the attribute values we will test with:
Set buffer distance in script:
Result:
-
I just noticed that perhaps you want to use the modeler to buffer the selected features instead of inside the script. In which case, you can just remove the last line in the script and the
buffer_distance
parameter =)Joseph– Joseph2016年03月09日 13:15:45 +00:00Commented Mar 9, 2016 at 13:15 -
1Brilliant, this is exactly what I was looking for! Thank you very much for your effort, much appreciated!Miron– Miron2016年03月09日 13:21:52 +00:00Commented Mar 9, 2016 at 13:21
-
@Miron - Most welcome buddy! Glad it was helpful :)Joseph– Joseph2016年03月09日 13:22:50 +00:00Commented Mar 9, 2016 at 13:22
If your goal is to create a buffer around the features with the highest value, your script can select the features with the maximum value and create a new output layer with those features. The next step in your model can use this layer.
-
The thing is the script does not select the features with the maximum values, it just gives you the maximum value as a number...I am looking exactly for that, a script that selects the features with the maximum values ;)Miron– Miron2016年03月09日 08:28:26 +00:00Commented Mar 9, 2016 at 8:28
Explore related questions
See similar questions with these tags.