3

I am aiming to replicate an ArcMap Model in QGIS Graphical Modeler.

ArcMap model functions give a much wider range of options (see fig 1) for the type of field to be created. Furthermore, the field calculator function when accessed through the GUI offers different options than those offered in the Model Builder function. I am creating a model in which these field types are of particular importance. I need to ensure the field I am creating using the QGIS Modeler field calculator function is Double.

Primary Question: How can I ensure I replicate a "Double" field type in the QGIS Model Builder Field Calculator function?

ArcMap Model Field Type

ArcMap Model Field Type QGIS Model Field Type for Field Calculator Function

QGIS Model Field Type

QGIS Field Calculator Types

QGIS Field Calculator Types

4

Create a custom script and add this to your modeler.

You can write some code in the script which adds a Double field-type along with an expression as you would type into the Expression window of the GUI. You can create a script by going to:

Processing Toolbox > Scripts > Tools > Create new script

Then use the following code, making necessary edits such as field name and expression:

##Example=name
##Input=vector

import qgis
from qgis.core import QgsField, QgsExpression
from PyQt4.QtCore import QVariant

layer = processing.getObject(Input)
layer.dataProvider().addAttributes( [ QgsField("Name", QVariant.Double) ] )
layer.updateFields()

layer.startEditing()
idx = layer.fieldNameIndex( "Name" )
e = QgsExpression( """ 1 """ )
e.prepare( layer.pendingFields() )
for f in layer.getFeatures():
    f[idx] = e.evaluate(f)
    layer.updateFeature(f)

layer.commitChanges()

Make sure the script is saved in /.qgis2/processing/scripts/ directory. Now you should be able to add this script in your modeler, it will ask for an input vector layer which you can choose from your modeler.


Edit:

If you want the user to input an expression, you can add a String input in your model and give it a name (e.g. "Expression"). Then in your script, add a Formula=string parameter in the header and call this as the expression e = QgsExpression(Formula):

##Example=name
##Input=vector
##Formula=string

import qgis
from qgis.core import QgsField, QgsExpression
from PyQt4.QtCore import QVariant

layer = processing.getObject(Input)
layer.dataProvider().addAttributes( [ QgsField("Name", QVariant.Double) ] )
layer.updateFields()

layer.startEditing()
idx = layer.fieldNameIndex( "Name" )
e = QgsExpression(Formula)
e.prepare( layer.pendingFields() )
for f in layer.getFeatures():
    f[idx] = e.evaluate(f)
    layer.updateFeature(f)

layer.commitChanges()

When you add your script to your model, you can then choose to specify the Formula to read from the "Expression" input:

Script model

  • Hi there Joseph Thanks very much for this. How would I change the number from 1 to a number of the users choosing? (Ideally prompted for at the start of the model / script being run). Cheers – Jamie2483 Aug 17 '16 at 10:20
  • @Jamie2483 - Edited the post to include how you would allow the user to enter an expression when running the model =) – Joseph Aug 17 '16 at 10:40
  • @Jamie2483 - Most welcome, glad it helped :) – Joseph Aug 17 '16 at 10:51

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.