4

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

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

  • 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 Mar 9 '16 at 13:15
  • 1
    Brilliant, this is exactly what I was looking for! Thank you very much for your effort, much appreciated! – Miron Mar 9 '16 at 13:21
  • @Miron - Most welcome buddy! Glad it was helpful :) – Joseph Mar 9 '16 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.

  • 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 Mar 9 '16 at 8:28
0

Great Josehp!!!

just a modification for my needs. How would the script be if what I need is a buffer over a certain number of maximum values? example: Select the 40 maximum values of a table and buffer them.

  • I've changed your code and did a botched like this: 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-20) processing.runalg("qgis:selectbyexpression", layer, expr, 0) processing.runalg("qgis:fixeddistancebuffer", layer, buffer_distance, 99, True, Result) How can I create a variable to set the range of maximum values? – Alejandro Chamizo de Castro Dec 15 '17 at 8:37
  • Done :) ##MaxFinderBuffer=name ##Layer=vector ##Fields=Field Layer ##value_KRK_max=output number 0 ##intervalo=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-intervalo) processing.runalg("qgis:selectbyexpression", layer, expr, 0) processing.runalg("qgis:fixeddistancebuffer", layer, buffer_distance, 99, True, Result) – Alejandro Chamizo de Castro Dec 15 '17 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.