10

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")
    NDVI@data@values
            ## 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.

  • 4
    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
  • 1
    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
  • 1
    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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.