Search code examples
rextractr-raster

Raster extraction returns NA's only


I am trying to create a rasterstack of 7 rasters named stack. this seems to work well, as when I use freq(stack) it returns frequencies for the rasters that are part of it, for example for one of the rasters:

$Slope_Final
     value  count
[1,]     0 414933
[2,]     1  94763
[3,]     2   6622
[4,]     3    917
[5,]     4    195
[6,]     5     22
[7,]    NA 487146

Yet when trying summary(stack) it returns

Error in .local(object, ...) : no cell values associated with this RasterBrick

This seems to contradict each other? Strangely enough, print(stack) does work. As I continue, and use the raster::extract function,

extraction <- extract(stack,points)

where points is a spatialpoints dataframe with a few columns like date and time, the returned matrix is 3145 rows of NA only;

> summary(extraction)
 Elevation_Final Landcover_Final Primroad_Final Secroad_Final  GPW_Final      Slope_Final    Water_Final   
 Mode:logical    Mode:logical    Mode:logical   Mode:logical   Mode:logical   Mode:logical   Mode:logical  
 NA's:3145       NA's:3145       NA's:3145      NA's:3145      NA's:3145      NA's:3145      NA's:3145  
points$Lat 

and

 points$Lon 

do return coordinates! They are of class integer. In case it is relevant; the extens of stack and points

 extent(stack)
class      : Extent 
xmin       : -265959.9 
xmax       : 873040.1 
ymin       : 4619541 
ymax       : 5501541 

extent(points)
class      : Extent 
xmin       : -97.1336 
xmax       : -83.54935 
ymin       : 43.6795 
ymax       : 49.0001 

Solution

  • It would be helpful if you showed more code, including all the messages you get. For example, you say you have a RasterStack, but the error message says you have a RasterBrick. Now that should not matter too much in most cases, but, who knows, it does matter here. summary takes a sample if the files are very large, and since you have many NA's perhaps it was an unlucky sample (seems unlikely) --- there would be a message in that case.

    Ideally, you would recreate the problem by creating some similar data with code. That may be difficult here, but you could still show the workflow and show(stack) and show(points).

    From the extents, one can assume that points has a longitude/latitude coordinate reference system, but stack clearly does not have that. The also do not overlap and hence you get all NAs . That is a good thing here, if they had, by coincidence, overlapped you might have gotten values, but the wrong values.