1

I had previous functioning R code that took an interpolated dataset of class sf, and turned it into a raster. I then preformed an average of raster values over specific overlayed polygons. It seems that raster is now throwing errors. See the following reprex:

library(sp)
library(raster)
data("meuse.grid")
coordinates(meuse.grid) = ~x+y
mraster=raster(meuse.grid)

Note that the summary of meuse.grid (non-raster) appears as:

> meuse.grid
class       : SpatialPointsDataFrame 
features    : 3103 
extent      : 178460, 181540, 329620, 333740  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 5
names       : part.a, part.b,     dist, soil, ffreq 
min values  :      0,      0,        0,    1,     1 
max values  :      1,      1, 0.992607,    3,     3 

while the summary of the raster version of meuse grid appears as:

> mraster
class      : RasterLayer 
dimensions : 10, 10, 100  (nrow, ncol, ncell)
resolution : 308, 412  (x, y)
extent     : 178460, 181540, 329620, 333740  (xmin, xmax, ymin, ymax)
crs        : NA 

Am I missing something here? The dimensions of the raster are reduced to 100 cells. In my personal dataset (not shown here), the dataset reduces from 69759 cells to 100 as well. Recent error? Should I post as an issue on the raster github?

2 Answers 2

0

meuse.grid is a SpatialPointsDataFrame. It does not have rows, columns, or cells, so there is no basis for comparison. All that raster(meuse.grid) does (and can do) is create a RasterLayer with the same extent as meuse.grid. To create a Raster* object, see rasterize or, for the odd case like this one, where the points have a regular pattern, rasterFromXYZ.

data("meuse.grid")
b <- rasterFromXYZ(meuse.grid)
b
#class      : RasterBrick 
#dimensions : 104, 78, 8112, 5  (nrow, ncol, ncell, nlayers)
#resolution : 40, 40  (x, y)
#extent     : 178440, 181560, 329600, 333760  (xmin, xmax, ymin, ymax)
#crs        : NA 
#source     : memory
#names      :   part.a,   part.b,     dist,     soil,    ffreq 
#min values :        0,        0,        0,        1,        1 
#max values : 1.000000, 1.000000, 0.992607, 3.000000, 3.000000 

Or

coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) <- TRUE
r <- brick(meuse.grid)
r
#class      : RasterBrick 
#dimensions : 104, 78, 8112, 5  (nrow, ncol, ncell, nlayers)
#resolution : 40, 40  (x, y)
#extent     : 178440, 181560, 329600, 333760  (xmin, xmax, ymin, ymax)
#crs        : NA 
#source     : memory
#names      :   part.a,   part.b,     dist,     soil,    ffreq 
#min values :        0,        0,        0,        1,        1 
#max values : 1.000000, 1.000000, 0.992607, 3.000000, 3.000000 
1
  • As an extension to this, I had functioning code that turned a 'sf' object POINT geometry (69759 points) into a raster. I then extracted the raster values over a polygon to find the mean within polygons. Yesterday, the raster suddenly stopped containing values and the extract function returned Error in .readCells(x, cells, 1) : no data on disk or in memory. I'm totally lost on this. Jun 25, 2021 at 13:49
0

Hmm, seems weird. Maybe worth following up on github? In the meantime, here's a clunky workaround. You can use rasterFromXYZ() and explicitly call your x, y, and z values. A cursory check with the object and plots suggests it works as expected.

mraster2 = rasterFromXYZ(as.data.frame(meuse.grid)[,c("x","y","soil")])
mraster2

plot(meuse.grid, col = meuse.grid$soil)
plot(mraster2)

SpatialPointsDataframe: spdf plot Raster: raster plot

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.