1

I would like to use grass7:i.image.mosaic algorithm in R using RQGIS packages But I don't understand how I can define it

I have 14 rasters named r1:r14. I do

params <- get_args_man(alg = "grass7:i.image.mosaic")
params$input <- paste0("r",1:length(my.raster.list))
params$output <- file.path(tempdir(), "ger_coords.tif")
params$GRASS_REGION_PARAMETER <- c("277743.101135","712479.268554","1590501.95667","1913668.6652")

out <- run_qgis(alg = "grass7:i.image.mosaic",
                params = params,
                load_output = TRUE)

But the GRASS_REGION_PARAMETER seems to be wrong

Error in run_qgis(alg = "grass7:i.image.mosaic", params = params, load_output = TRUE) : 
  Error: Wrong parameter value: 277743.101135712479.2685541590501.956671913668.6652

I have taken a look at this answer but it is still dark. I need a hand ! I'm using R version 3.4.0, QGIS 2.18 and Grass 7.2

  • From ?run_qgis() (v0.2.0.9000): "GRASS users do not have to specify manually the GRASS region extent (function argument GRASS_REGION_PARAMETER). If "None" (the QGIS default), run_qgis (see pass_args() for more details) will automatically determine the region extent based on the user-specified input layers." – pat-s May 15 '17 at 11:56
2

We have now implemented a support for ParameterMultipleInput-parameters in RQGIS. When providing a list as argument for such a parameter, RQGIS will now also determine the GRASS_REGION_PARAMETER. So coming back to our example from above, let's create three rasters, and since i.image.mosaic so far is not working correctly in QGIS, let's use r.patch:

# use the RQGIS developer version
devtools::install_github("jannes-m/RQGIS")
# attach packages
library("RQGIS")
library("raster")
# create three rasters (see also ?raster::mosaic)
r <- raster(ncol = 100, nrow = 100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(9, 30, 9, 30))
r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)
# plot the three rasters
plot(r1); plot(r2, add = TRUE); plot(r3, add = TRUE)
# patch them together using "grass7:r.patch
alg <- "grass7:r.patch"
# have a look at the online documentation
open_help(alg)
# find out how to use the geoalgorithm
get_usage(alg)
# run QGIS
out <- run_qgis(alg, input = list(r1, r2, r3), output = "out.tif", 
                load_output = TRUE)
# plot the output
plot(out)
| improve this answer | |
2

If you would like to specify the GRASS_REGION_PARAMETER yourself, you would have to use the notation accepted by the QGIS API (see https://docs.qgis.org/2.8/en/docs/user_manual/processing/console.html), in your case this would be:

params$GRASS_REGION_PARAMETER <- "277743.101135, 712479.268554, 1590501.95667, 1913668.6652"

Make sure that these coordinates correspond to xmin, xmax, ymin and ymax!

In your case, you would have to specify the GRASS_REGION_PARAMETER yourself since you would like to use multiple rasters (supposedly with different extents - why else would you want to mosaic). Normally, RQGIS would do so automatically. However, this is the first time that we realize that one input parameter can contain multiple files. Until now we always suspected there was only one file per input parameter (ok, we will take care of this in a future RQGIS release). Therefore, you would need to save your raster files to disk and use the corresponding paths as input argument. In the case of multiple input, you have to separate the files by a semi-colon (;) (see again https://docs.qgis.org/2.8/en/docs/user_manual/processing/console.html). Here, is a toy example:

library("raster")
# use the RQGIS developer version
devtools::install_github("jannes-m/RQGIS")
library("RQGIS")
set_env(dev = TRUE)
find_algorithms("grass7:i.image")

Interesting enough (because I cannot find this geoalgorithm in the QGIS GUI), this gives us:

"i.image.mosaic - Mosaics several images and extends colormap.--->grass7:i.image.mosaic"

Next, let's create a raster, extract three parts of it (with different extents), save the rasters as tif-files and retrieve manually the GRASS_REGION_PARAMETER:

# copied from the raster::mosaic help
r <- raster(ncol = 100, nrow = 100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(9, 30, 9, 30))
r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)
ls_1 <- list(r1, r2, r3)
names(ls_1) <- paste0("r", 4:6, ".tif")
 # retrieve the GRASS_REGION_PARAMETER for the three raster files
ext <- lapply(seq_along(ls_1), function(i) {
  # write the rasters to a temporary folder
  writeRaster(x = ls_1[[i]], filename = file.path(tempdir(), names(ls_1[i])), prj = TRUE, overwrite = TRUE)
  # retrieve the extent
  extent(ls_1[[i]])
})
ext <- Reduce(raster::merge, ext)
# final bounding box in GRASS notation
ext <- paste(ext[c(1, 2, 3, 4)], collapse = ",")
# construct the paths to the raster files
rasters <- grep(".tif$", dir(file.path(tempdir())), value = TRUE)
rasters <- paste(file.path(tempdir(), rasters), collapse = ";")

Finally, we can run QGIS:

run_qgis("grass7:i.image.mosaic", input = rasters,  GRASS_REGION_PARAMETER = ext)

Allegedly, this created following output:

$output
[1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing0185a5ca2d6b479aa8fc8f21953174b9\\bd06822188844e9589bde5b6c9e85b46\\output.tif"

However, the corresponding folder is empty. But this is hard to debug since we supposedly did everything correctly. I would have liked to test it in the QGIS GUI but there the algorithm is not available. So maybe we have to wait for the QGIS developers to actually integrate this algorithm (after all, we are using here the developer version). But I have to admit this is unsatisfactory. So why don't you use the raster::mosaic-function in the meantime since your rasters already reside in R's global environment. To continue with our toy example (please note it is only copied from the raster::mosaic helpfile):

m1 <- mosaic(r1, r2, r3, fun = mean)
plot(m1)
| improve this answer | |
  • Thank you Jannes, my region issue work now, but the now it's a input issue. I can deal with R rasterLayer class? – delaye May 16 '17 at 18:12
  • What do you mean? Most of my answer addresses the input-issue... In the end I suggested to use the raster::mosaic-function. – Jannes May 17 '17 at 9:16

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.