2

I have a problem with the QGIS modeler:

The idea is to extract attributes ("DN" between 40 and 44) out of one layer.

I want to use the outcoming layer for further algorithms.

I calculated with the field calculator a new field named "selection2" using the following formula:

"DN" >= 40 AND "DN" <= 44

enter image description here

Then I extracted by attribute for selection2=1.

The attribute table is empty, as in the original layer exists no DN between 40 and 44.

The problem is that the fied calculator is not able to use empty attribute tables for further algorithms (here: "extract by location"). Therefore this error is displayed:

Error executing algorithm Extract by location float division by zero See log for more details

enter image description here

It would be good to avoid the error and have a layer algorithms can work with.

Is there a algorithm in the modeler which functions like an if/else-function?

If there is none I have the following approach: Fill in the attribute table any value, e.g. 9999, in order to not have an empty one. The question is then how to create this command?

As I do not have any experience with python, I cannot use it.

  • Welcome to GIS:SE @Guest! What are you expecting the output to be if you did add a feature to the attribute table? If no features are available due to the Extract by attribute tool then the result of the Extract by location would be empty even if there was no error. – Joseph Jan 19 '16 at 11:23
  • @ Joseph Thank you for your quick answer! I want to exclude several attributes, e.g. "DN" between 40 and 44 or "DN" is between 1 and 11. Furthermore I want it to work for different input layers of the same type. As I don't know in advance which attributes exist and which not, it is ok for me to have empty and not empty layers as a result. – user65535 Jan 19 '16 at 14:28
1

The error occurs because the Extract by location requires atleast one feature for it to run. If the output of the Extract by attribute tool contains no features then we will need to create one (ideally with no geometry) and we can do this by adding a custom script to the model.

To do this, go to Processing Toolbox > Scripts > Tools > Create new script and copy the following:

##Add feature without geometry=name
##Layer=vector

from qgis.core import QgsFeature

layer = processing.getObject(Layer)
num = layer.featureCount()
if num == 0:
    layer.startEditing()
    attr = layer.dataProvider()
    feat = QgsFeature()
    feat.setAttributes(['1'])
    attr.addFeatures([feat])

layer.commitChanges()

Save the script into C:/Users/You/.qgis2/processing/scripts.

Then add the script from the list of algorithms to your model (I made a simple test):

Model example

Add the script and use the output of the Extract by attribute tool in the Layer option:

Script settings

Then in the settings of the Extract by location tool, add the script as a Parent Algorithm:

Extract by location

I created a simple shapefile with one feature that has a value of 1. I set my Extract by attribute tool to extract values equal to 0 (i.e. the output of this will be blank):

Example shapefile

The model seems to run successfully with no errors and outputs a shapefile with no features:

Model log

Result

  • @PieterB - What is the error? My guess is that it requires atleast one feature which contains geometry. – Joseph Feb 16 '17 at 10:07
  • It was just a common error. I made a workaround by adding a fictive feature based on the layers extent. But I forgot to remove my comment here, because it is not relevant anymore – PieterB Feb 16 '17 at 10:11
  • @PieterB - Ahh no worries, glad you found a workaround atleast. And thank you for letting me know about this error, will have to keep this in mind ;) – Joseph Feb 16 '17 at 10:15
  • @Joseph I'm trying to run this script between two extract by location algorithms. The first one occasionally outputs a blank file depending on the data input into the model. The script is outputting a shape file like it's designed to but I'm still getting an error like there is no data. – brink Nov 29 '17 at 0:47
  • @brink - I think you should ask this as a new question and include your script and a screenshot of your model :) – Joseph Nov 29 '17 at 10:04

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.