Raster Data I/O

Import a raster file

  • terra uses the function rast() to import raster data
  • Printing the object will give you some basic information about the raster
    • The number of rows and columns, as well as the number of layers
    • The resolution of the raster, meter per pixel (25 in our case)
    • The extent of the raster, in the coordinate reference system of the raster
    • The coordinate reference system (CRS) of the raster
    • The source (in memory, or a file path)
    • The name(s) of the band(s) (we only have one band in our case)
    • The min and max values of the raster
library(terra)

dhm25 <- rast("data/Spatial_Analysis_II/dhm25_lu.tif")
dhm25
class       : SpatRaster 
dimensions  : 2321, 2161, 1  (nrow, ncol, nlyr)
resolution  : 25, 25  (x, y)
extent      : 2628987, 2683012, 1179988, 1238013  (xmin, xmax, ymin, ymax)
coord. ref. : CH1903+ / LV95 (EPSG:2056) 
source      : dhm25_lu.tif 
name        : dhm25_lu 
min value   :    381.1 
max value   :   3228.3 

GeoTIFF

  • GeoTIFF is the most common raster format
  • GeoTIFF is an extension to the TIFF format, which includes additional metadata to establish the spatial reference of the file
  • This includes the CRS, the extent, the resolution, and the origin of the raster
  • The metadata is either stored in the header of the file, or in an accompanying file with the same name, but different extension (.tfw or .aux.xml)
  • Other important raster file formats include:
    • Cloud optimized GeoTIFF (COG): A GeoTIFF file that is optimized for cloud storage which allows for efficient, partial reading of the file over HTTP
    • JPG2000 (.jp2) is a compressed raster format that is often used for satellite imagery
    • NetCDF (.nc) is a format that is often used for climate data
    • HDF5 (.h5) is a format that is often used for remote sensing data

Writing a raster file

  • You can write a raster object to a file using the writeRaster() function
writeRaster(dhm25, "data-out/dhm25_lu.tif", overwrite = TRUE)