Rasters are a form of data that is georeferenced and (somewhat) continuous. Raster data is perhaps best envisioned as a matrix of values, whose entries represent spatially referenced data points. The raster itself can be visualized as you could for normal matrix output. What makes a raster different, however, is that it is (or should be) georeferenced. This means that each element of the matrix represents some measurement on the ground having a specific location and spread. This is analogous to an image, where if you zoom in on it enough, you will be able to differentiate between individual pixels, it is just that for rasters, each pixel has a spatial location and size associated with it that we can map onto the earth.

You can either create raster objects de novo or you can acquire them from some external source. To create one from scratch, you start with a matrix of values and then construct the raster object using the raster() function as:

## class      : RasterLayer 
## dimensions : 100, 100, 10000  (nrow, ncol, ncell)
## resolution : 0.01, 0.01  (x, y)
## extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
## crs        : NA 
## source     : memory
## names      : layer 
## values     : 5.646143e-07, 0.9998708  (min, max)

which can be visualized using the normal plot command. The raster library has overridden several of the plotting functions and you can plot raster objects and decorate the images in the same way you do for normal plotting materials (@ref(graphics)).

Loading Rasters

Raster data can be loaded from a variety of sources in a variety of formats. For most cases, GeoTIFF and BIL formats are the most common. Here is an example of an elevation model for Baja California that I pulled down from WorldClim.

## class      : RasterLayer 
## dimensions : 3600, 3600, 12960000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : -120, -90, 0, 30  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : /Users/rodney/Desktop/DLab-Spatial/data/alt_22.tif 
## names      : alt_22 
## values     : -202, 5469  (min, max)

Notice it is 30; spread (in latitude & longitude). Lets crop it first before we go printing it out.

## class      : RasterLayer 
## dimensions : 960, 720, 691200  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : -115, -109, 22, 30  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : /private/var/folders/ty/1tdv5mw56p33qx86hnk5h8l80000gn/T/Rtmp72DZ9d/raster/r_tmp_2019-11-21_150956_13287_99404.grd 
## names      : alt_22 
## values     : -202, 2263  (min, max)

Much better. This is what it looks like in the raw.

Manipulating Rasters

Since rasters are basically just big matrices, you can manipulate them as you would a matrix. Here Iā€™m going to remove all the data whose elevation is < 500.

Saving Rasters

You can save any object in R as an *Rda object by using save. You can also write rasters out to files in many formats that other applications are able to use. For this, use the writeRaster function. The formats supported by the raster package are extensive and include:

##       name        long_name                                
##  [1,] "raster"    "R-raster"                               
##  [2,] "SAGA"      "SAGA GIS"                               
##  [3,] "IDRISI"    "IDRISI"                                 
##  [4,] "IDRISIold" "IDRISI (img/doc)"                       
##  [5,] "BIL"       "Band by Line"                           
##  [6,] "BSQ"       "Band Sequential"                        
##  [7,] "BIP"       "Band by Pixel"                          
##  [8,] "ascii"     "Arc ASCII"                              
##  [9,] "CDF"       "NetCDF"                                 
## [10,] "big"       "big.matrix"                             
## [11,] "ADRG"      "ARC Digitized Raster Graphics"          
## [12,] "BMP"       "MS Windows Device Independent Bitmap"   
## [13,] "BT"        "VTP .bt (Binary Terrain) 1.3 Format"    
## [14,] "BYN"       "Natural Resources Canada's Geoid"       
## [15,] "CTable2"   "CTable2 Datum Grid Shift"               
## [16,] "EHdr"      "ESRI .hdr Labelled"                     
## [17,] "ELAS"      "ELAS"                                   
## [18,] "ENVI"      "ENVI .hdr Labelled"                     
## [19,] "ERS"       "ERMapper .ers Labelled"                 
## [20,] "GPKG"      "GeoPackage"                             
## [21,] "GS7BG"     "Golden Software 7 Binary Grid (.grd)"   
## [22,] "GSBG"      "Golden Software Binary Grid (.grd)"     
## [23,] "GTiff"     "GeoTIFF"                                
## [24,] "GTX"       "NOAA Vertical Datum .GTX"               
## [25,] "HFA"       "Erdas Imagine Images (.img)"            
## [26,] "IDA"       "Image Data and Analysis"                
## [27,] "ILWIS"     "ILWIS Raster Map"                       
## [28,] "INGR"      "Intergraph Raster"                      
## [29,] "ISCE"      "ISCE raster"                            
## [30,] "ISIS2"     "USGS Astrogeology ISIS cube (Version 2)"
## [31,] "ISIS3"     "USGS Astrogeology ISIS cube (Version 3)"
## [32,] "KRO"       "KOLOR Raw"                              
## [33,] "LAN"       "Erdas .LAN/.GIS"                        
## [34,] "Leveller"  "Leveller heightfield"                   
## [35,] "MBTiles"   "MBTiles"                                
## [36,] "MRF"       "Meta Raster Format"                     
## [37,] "netCDF"    "Network Common Data Format"             
## [38,] "NGW"       "NextGIS Web"                            
## [39,] "NITF"      "National Imagery Transmission Format"   
## [40,] "NTv2"      "NTv2 Datum Grid Shift"                  
## [41,] "NWT_GRD"   "Northwood Numeric Grid Format .grd/.tab"
## [42,] "PAux"      "PCI .aux Labelled"                      
## [43,] "PCIDSK"    "PCIDSK Database File"                   
## [44,] "PCRaster"  "PCRaster Raster File"                   
## [45,] "PDF"       "Geospatial PDF"                         
## [46,] "PDS4"      "NASA Planetary Data System 4"           
## [47,] "PNM"       "Portable Pixmap Format (netpbm)"        
## [48,] "RMF"       "Raster Matrix Format"                   
## [49,] "ROI_PAC"   "ROI_PAC raster"                         
## [50,] "RRASTER"   "R Raster"                               
## [51,] "RST"       "Idrisi Raster A.1"                      
## [52,] "SAGA"      "SAGA GIS Binary Grid (.sdat, .sg-grd-z)"
## [53,] "SGI"       "SGI Image File Format 1.0"              
## [54,] "Terragen"  "Terragen heightfield"