Wednesday 22 January 2020

r - Difference between raster() and readGDAL()?


I am new in working with rasters and after having trouble working with them i'd like to know the difference between those two import functions.


for example i did this


  library(raster)
s<-raster("random.TIF")

when trying to plot it, returns the correct image, but trying this:



s<-readGDAL("random.TIF")

returns a black image.



Answer



None of these functions return a colored image, black or otherwise. This


library(raster)
s <-raster("random.TIF")
s

Returns a RasterLayer object (or perhaps a RasterBrick if there are multiple layers in "random.TIF"). This class (type of object) is defined in the raster package. You can visualize it with



plot(s)

This


library(rgdal)   
x <- readGDAL("random.TIF")
class(x)

Returns a SpatialGridDataFrame. This class (type of object) is defined in the sp package. You probably did plot(s) but that does not work well as that would probably show a 'black image'. An appropriate way to plot a SpatialGridDataFrame is


spplot(x)


sp and raster objects play well together, but they are different. A possible starting point is reading this: cran.r-project.org/web/packages/raster/vignettes/Raster.pdf


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...