5

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?

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Mar 8, 2016 at 16:55

2 Answers 2

3

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:

Point example

Set buffer distance in script:

Running script

Result:

Result

answered Mar 9, 2016 at 13:03
3
  • 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 =) Commented Mar 9, 2016 at 13:15
  • 1
    Brilliant, this is exactly what I was looking for! Thank you very much for your effort, much appreciated! Commented Mar 9, 2016 at 13:21
  • @Miron - Most welcome buddy! Glad it was helpful :) Commented Mar 9, 2016 at 13:22
0

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.

answered Mar 8, 2016 at 17:38
1
  • 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 ;) Commented Mar 9, 2016 at 8:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.