0

I have a raster in R like

> str(my.raster)
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:80936] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num 0.338
  .. .. ..@ max       : num 0.948
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr ""
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num -15.6
  .. .. ..@ xmax: num -15
  .. .. ..@ ymin: num 15.7
  .. .. ..@ ymax: num 16.3
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 268
  ..@ nrows   : int 302
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
  ..@ history : list()
  ..@ z       : list()

I want to write it in GRASS7 data base. I have, of course, loads rgrass7 and I tried.

> writeRAST(my.raster, "oct_dec")

but

Error in x@data[[zcol]] : this S4 class is not subsettable

I don't understand how to deal with this error in raster context.

Edit ----------------------------------------------------------------------

A reproductible exemple

library(raster)
library(rgrass7)
# initialisation GRASS
initGRASS(gisBase = "/usr/lib/grass72", home = tempdir(),
          gisDbase = "/home/user/Documents/grassDB/",
          location = "Modis_ndvi", mapset = "PERMANENT")
r1 <- raster(ncol = 5, nrow = 10, xmn = 0, xmx = 5, ymn = 0, ymx = 10)
values(r1) <- 1:50  # Note 50 is the total number of cells in the grid. 
r1
# class       : RasterLayer 
# dimensions  : 10, 5, 50  (nrow, ncol, ncell)
# resolution  : 1, 1  (x, y)
# extent      : 0, 5, 0, 10  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
# data source : in memory
# names       : layer 
# values      : 1, 50  (min, max)
proj4string(r1) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 

writeRAST(r1, "test_r1")
# Error in x@data[[zcol]] : this S4 class is not subsettable

I'm using grass72, GDAL 2.1.3 andsessionInfo() give me

R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8   
 [6] LC_MESSAGES=fr_FR.UTF-8    LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rgrass7_0.1-9       XML_3.98-1.6        rasterVis_0.41      latticeExtra_0.6-28 RColorBrewer_1.1-2  lattice_0.20-35     magrittr_1.5       
 [8] rgdal_1.2-7         doParallel_1.0.10   iterators_1.0.8     foreach_1.4.3       raster_2.5-8        sp_1.2-4           

loaded via a namespace (and not attached):
[1] Rcpp_0.12.10      codetools_0.2-15  zoo_1.8-0         viridisLite_0.2.0 grid_3.4.0        hexbin_1.27.1     tools_3.4.0       compiler_3.4.0 

This error seems to be knowed on old sp package version ...

  • Would be easier to help if you provide a reproductible example with R utils::sessionInfo() and GRASS 7 version, it can interesting to know also if you are using the gdal-grass pluging – dickoa Apr 26 '17 at 12:15
  • I have updated my question. – delaye Apr 26 '17 at 13:30
3

From the help of rgrass7::writeRAST

 writeRAST(x, vname, zcol = 1, NODATA=NULL,
  ignore.stderr = get.ignore.stderrOption(), useGDAL=get.useGDALOption(),
  overwrite=FALSE, flags=NULL, drivername="GTiff")

....

   x: A SpatialGridDataFrame object for export to GRASS as a raster
      layer

You need to coerce your Raster object to SpatialGridDataFrame and then upload. This should hopefully work

writeRAST(as(r1, "SpatialGridDataFrame"), "test_r1")

May be a this point we can write a small wrapper to save few keystrokes.

Hope it helps

|improve this answer|||||

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.