2

The question is loosely related to Using Calculator in QGIS Graphical Modeler since it is the same model I'm talking about. I have an algorithm ("Create Grid", in that case, but it might be any other) that needs a CRS as an input parameter, which I want to be set by the use of the model.

.

But from the list of available model inputs I suspect that this is currently not supported.

I tried to use "Vector Grid", which does not need the CRS, and assign the projection later, but I run into the same problem. Also tried to extract the CRS from another layer, but I just can't connect the extracted CRS with the algorithm where it is needed.


To be a bit more specific: I want the CRS as a model parameter rather than setting it within the model (which would then mean I have to edit the model every time I need another CRS)

  • try clicking on the three points right to GRID CRS or do I miss something? – LaughU Feb 20 '18 at 7:11
  • The algorithm is used within the model and I want to assign the CRS either from some other input or to be set by the user when the model is called, not within the algorithm itself. See updated question – s6hebern Feb 20 '18 at 7:13
2

There is a CRS parameter, unfortunately it is available for custom scripts and not exposed to the modeler (it is however available in the modeler for QGIS 3).

If you want the CRS to be defined at the beginning of your model, one method could be to use a String parameter and have the user type in the CRS they want to use. We can create a custom script which takes this string input, calls the Create grid tool and inserts the CRS, along with other variables as shown in your image, as parameters.


  1. You can create one from:

    Processing Toolbox > Scripts > Tools > Create new script
    

    Then use something like the following:

    ##Example=name
    ##CRS=string
    ##Extent=extent
    ##Horizontal_spacing=number
    ##Vertical_spacing=number
    ##Grid=output vector
    
    processing.runalg("qgis:creategrid", 1, Extent, Horizontal_spacing, Vertical_spacing, CRS, Grid)
    

    Save the script into your /.qgis2/processing/scripts directory.


  1. Load your model and insert a String parameter (you could set a default string if you wish):

    String parameter


  1. Add the custom script from the list of algorithms to your model, choose the relevant parameters and save:

    Script parameters


  1. Now when the model is executed, the user can see the default CRS or they can type in a specific one which would be carried over to the script:

    Running model



Edit:

If you want to set the CRS directly by taking it from the raster instead of letting the user set it, remove the script from your model then save it (you can also remove the String parameter for the CRS). Then edit your script using something like:

##Example=name
##Raster_layer=raster
##Extent=extent
##Horizontal_spacing=number
##Vertical_spacing=number
##Grid=output vector

raster = processing.getObject(Raster_layer)
CRS = raster.crs().authid()

processing.runalg("qgis:creategrid", 1, Extent, Horizontal_spacing, Vertical_spacing, CRS, Grid)

Save this script and then add it back into your model, selecting the relevant options again. This time you are selecing the raster layer as an input parameter which the script uses to obtain its CRS, which will subsequently be used in the Create grid algorithm.

  • Nice workaround! Is there any way to read the ESPG or CRS string from the raster to implement it directly instead of letting it set by the user? – s6hebern Feb 20 '18 at 13:57
  • @s6hebern - Yup, see the edited post :) – Joseph Feb 20 '18 at 14:06
  • Amazing, exactly what I needed! – s6hebern Feb 20 '18 at 14:16
  • @s6hebern - Glad it works :) – Joseph Feb 20 '18 at 14:21

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.