5

I am trying to get a raster into GRASS from R and back out again using the rgrass7 package. Getting the raster in seems to be no problem. However, when I try to get it out with the readRAST function it seems that the raster dimensions have been reduced to [1,1].

Am I missing a function argument? All the examples I have seen simply use the GRASS object name.

Is this a mapset issue? I didn't think so because it was my understanding that the philosophy of rgrass7 is to use temporary mapsets and layers.

See reproducible example:


library(raster)
#> Loading required package: sp

raster_image <- raster::raster(system.file("external/test.grd", package = "raster"))
raster_image <- as(raster_image, "SpatialGridDataFrame")

loc <- rgrass7::initGRASS("/usr/lib/grass70", home = tempdir(), override = TRUE)
rgrass7::writeRAST(raster_image, "raster_image", flags = c("overwrite"))
#>    0%   3%   6%   9%  12%  15%  18%  21%  24%  27%  30%  33%  36%  39%  42%  45%  48%  51%  54%  57%  60%  63%  66%  69%  72%  75%  78%  81%  84%  87%  90%  93%  96%  99% 100%
rgrass7::execGRASS("r.info", map = "raster_image")
#>  +----------------------------------------------------------------------------+
#>  | Map:      raster_image                   Date: Wed Jan 18 08:36:35 2017    |
#>  | Mapset:   file17053b43fb4e               Login of Creator: jose            |
#>  | Location: file170531b5b35a                                                 |
#>  | DataBase: /tmp/Rtmpn8cS5A                                                  |
#>  | Title:     ( raster_image )                                                |
#>  | Timestamp: none                                                            |
#>  |----------------------------------------------------------------------------|
#>  |                                                                            |
#>  |   Type of Map:  raster               Number of Categories: 0               |
#>  |   Data Type:    DCELL                                                      |
#>  |   Rows:         115                                                        |
#>  |   Columns:      80                                                         |
#>  |   Total Cells:  9200                                                       |
#>  |        Projection: x,y                                                     |
#>  |            N:     334000    S:     329400   Res:    40                     |
#>  |            E:     181600    W:     178400   Res:    40                     |
#>  |   Range of data:    min = 128.434005737305  max = 1805.78002929688         |
#>  |                                                                            |
#>  |   Data Description:                                                        |
#>  |    generated by r.in.bin                                                   |
#>  |                                                                            |
#>  |   Comments:                                                                |
#>  |    r.in.bin --overwrite -d input="/tmp/Rtmpn8cS5A/file170531b5b35a/file\   |
#>  |    17053b43fb4e/.tmp/josecomp/X530" output="raster_image" bytes=8 heade\   |
#>  |    r=0 bands=1 order="native" north=334000 south=329400 east=181600 wes\   |
#>  |    t=178400 rows=115 cols=80 anull=127                                     |
#>  |                                                                            |
#>  +----------------------------------------------------------------------------+

out_raster <- rgrass7::readRAST("raster_image")
#> Creating BIL support files...
#> Exporting raster as floating values (bytes=8)
#>    0% 100%
dim(raster_image)
#> [1] 9200    1
plot(raster_image)

dim(out_raster)
#> [1] 1 1
plot(out_raster)
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Error in seq.default(zlim[1], zlim[2], length.out = length(col) + 1): 'from' cannot be NA, NaN or infinite
5

Your GRASS mapset region settings are at fault (even a temp dir needs to be set up). Try something like

region_check <- capture.output(execGRASS('g.region', flags = c('p'))

edit: (nah, just use `gmeta()` )

or cheat: open your mapset directly in GRASS and run g.region -p. You can do this even with a temp location so long as you don't exit R, you just have to navigate to the correct R temp folder in GRASS's start screen.

You will be able to set the region to match the file you imported by e.g. g.region raster=raster_image'.

edit2: you may find that after this, the location still has no genuine CRS. g.region won't set that, instead you need to run g.proj -t -c georef='raster_image' or similar. The catch is that this has to be done in the PERMANENT mapset and then g.region -d has to be run in all other mapsets.

Suggest you simplify your life by altering your initGRASS call to start in PERMANENT e.g.

rgrass7::initGRASS("/usr/lib/grass70", home = tempdir(), 
                   mapset = 'PERMANENT', override = TRUE)

I've never understood why that isn't default behaviour in GRASS. Anyway, you can then set projection with g.proj. Notably, if you point it at a raster on disk using the georef= parameter (e.g. georef=<filepath> or georef=raster@file@name), you get all your region settings matching the file, not just the projection.

  • 1
    Thanks! Can't believe I missed this. I needed the line rgrass7::execGRASS("g.region", raster = "raster_image") – jsta Jan 18 '17 at 14:40
  • cool. gmeta() is actually the better way to report location/mapset info, too. Are you having any trouble with missing projection info? – obrl_soil Jan 18 '17 at 14:52

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.