Trying to get to the bottom of why, when I read in a raster of NDVI, the @data@values slot does not contain the actual values until I set them manually. For example:

    NDVI <- raster("./filename.tif", crs="+proj=longlat +datum=WGS84")
            ## returns: logical(0)

This did not happen with other rasters I had loaded in by the same method, so I'm confused. I wish I could be more specific but I do not remember doing anything differently before. It's easy enough to get the values manually, using:

    NDVI1@data@values <- getValues(NDVI19east)

But it's still a pain to have to do so for every file. So, two-part question:

  1. Why did this happen in the first place? I understand that it could have something to do with how the raster file is being stored (i.e. whether it's in memory or not) but I can't really understand how that changes the methods I should use for accessing the data...

  2. Is there a way to automate this process (perhaps using a method similar to lapply) for reading files as RasterLayers and accessing values for those files? My current project involves reading 6-10 files at a time for NDVI, Rainfall and other environmental variables, in order to combine them and perform some weighted overlays. It would be helpful to automate the process of importing the data.

  • 6
    Don't use @ unless you are developing internal code - use readAll(NDVI). It happens as a memory-efficiency technique, you can open very large grids as kind of a promise - raster promises to pull in the data (via rgdal, via GDAL in this case) when you actually need the numbers. If you need to save the object as a standalone R object not tied to a file readAll is the way to do it. See ?raster "In many cases . . . does (initially) not contain any cell (pixel) values in (RAM)" – mdsumner Jun 12 '15 at 20:44
  • 2
    the logical(0) is in fact the value for any Raster* object created from a file. Either way, as @mdsumner says, do not directly read these values, and certainly do not set them! (although your NDVI1@data@values <- getValues(NDVI19east) won't affect anything, these values are ignored). It is probably further down your script where you do not understand how to effectively use these objects. You can use getValues, but even that is rarely necessary. Provide a simple, self-contained, example of what you are trying to achieve. – Robert Hijmans Jun 14 '15 at 23:04
  • 2
    Thanks all very much. I ended up accomplishing what I needed to with readAll() as mdsumner said, so thanks for that - it was good advice! I had been new to the raster package as of recently so I honestly wasn't yet aware of that function and the need to use it for accessing the actual values of large files. – Henry Hawkins Wells Jun 15 '15 at 14:20

This question has been answered in the comments (by mdsummer). This is just a way to put those ideas in order and get this question out of unanswered queue.

Here you can download world wide jpg's NVDI from nasa.

Here you have the code and a raster file to tryout.

As shown in question, loading the raster into R with raster() function does not load the actual values into memory.

enter image description here

As you see, NVDI@data@values has no values while the plot can be rendered showing those "hiden" values. You can see, that, if you load the file into QGIS, the values are actually read.

enter image description here

So, you have to use readAll() function from raster package (as mdsummer said in the comments). Here is the code:


NDVI <- raster("./RenderData.tif", crs="+proj=longlat +datum=WGS84")

NDVI.all <- readAll(NDVI)

Using this function you can now access to the raster values within the file.

enter image description here

| 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.