0

I am writing a script in R for running in QGIS processing. The code is a oil spill classification algorithm and depending on the number of polygons which user inputs in the shapefile, output raster layers are to be produced, each containing the oil/water classification plot corresponding to each polygon in the shapefile. Since I do not know in advance how many polygons user is going to input, I cannot specify in the processing header like:

##out_plot_raster_1 = output raster
##out_plot_raster_2 = output raster
##out_plot_raster_3 = output raster
.
.
.

QGIS processing scripts support multiple raster input like:

## input_many_rasters = multiple raster

so it is logically inferred that same must be true for output also, but on trying:

## out_many_rasters = output multiple raster

I get an error and the script won't run at all.

Now, it is really important for me to be able to output multiple rasters at once so that user can get all the friendly options like "saving them to desired location" and "opening them in QGIS after the script run" which are automatically provided by the processing framework of QGIS. How do I do this? i.e. I want user to be able to enter, before the script run, the location to store all generated raster outputs and if he wants, the rasters must be opened in QGIS automatically after the execution is complete. Please suggest me a way to do this.

12
  • 1
    This question has come up before (possible to specify variable number of output layers?) and has no answer. I had a quick look at the 2.18 source code and can not see anything that suggests setting multiple outputs as a single param is possible. It is however possible in QGIS 3x, but note the 3x "script" syntax is now completely different and much more complex (though much more flexible and powerful).
    – user2856
    Commented Jul 9, 2018 at 1:32
  • @Luke But QGIS 3.x, does not have a support for running R scripts in the processing >> toolbox menu. That was the reason I shifted to 2.x. Would you please explain how is it possible in QGIS 3.x Commented Jul 9, 2018 at 4:06
  • I don't use r. I only mention QGIS 3 as it does support multiple outputs.
    – user2856
    Commented Jul 9, 2018 at 4:08
  • @Luke Do you mean w.r.t python scripts? I mean supporting multiple outputs can only be w.r.t. some script, what else is the meaning of 'supporting multiple outputs'. I am really new to QGIS so can't get you. Commented Jul 9, 2018 at 4:15
  • Yes, setting multiple outputs as a single parameter w.r.t QGIS Processing python scripts.
    – user2856
    Commented Jul 9, 2018 at 4:17

1 Answer 1

1

I would set a common extent (ie., maximum extent of all polygons) and structure your R script so it creates a stack or brick object and adds each polygon result to the stack. In this way you only have to worry about outputting a single object representing all of the polygons.

4
  • Would you please elaborate? I do not know what an extent is and also how does making a single object solve the problem. How do I output that? Do you mean to say that I should make a single raster (of smallest rectangular size such that it covers all polygons, where in place of selected polygons, I classify oil/water while the other non-polygon area is left blank) and just output that raster? Commented Jul 8, 2018 at 21:31
  • For anyone who happens to come here for an answer: This is the only workaround if you use 2.x (which you have to if you are using R since it is not yet supported in 3.x). I ended up doing what this answer suggests i.e. considering the smallest rectangle encompassing all the polygons as a single output raster. Commented Jul 9, 2018 at 12:06
  • 1
    @MadhurPanwar, if you need a raster for each polygon you can write a series of rasters and store them as a stack or brick object. This object can then be written out to a format such as tiff or img, which can support multiband rasters. In this way each polygon can have its own raster. The one caveat is that the dimensions of the rasters have to match including: origin and bounding coordinates, cell resolution, number of row and columns (which defines the extent). With a multiband stack/brick, you then have a single raster object to write to disk. Commented Jul 9, 2018 at 16:57
  • Thanks. Actually it turned out that the solution mentioned in the answer was more apt for what I wanted. Your input about 'brick' being written as a multiband raster is really great though. That's a really nice alternative. Commented Jul 10, 2018 at 17:36

Your Answer

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

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