This is how you would look if you just started studying spatial tools for R in 2022-2026.
This is because many of the guides and tutorials on the internet are still referring to the old school tools like maptools
, rgdal
, rgeos
, and raster
. The worst thing is that the authors of those guides and tutorials may not be able to update those materials anymore. The reasons why I considered the aforementioned packages as old school are because the maptools
, rgdal
and rgeos
were announced as retired packages as of 2023 and that the raster
package have slower performance than that of its successor the terra
package. To think, raster
and terra
are similar packages authored by the same person.
On the other hand, you are lucky if you started studying spatial tools for R in 2027 and onwards. It would be more easy for you since during this time, there will be less tutorials and guidelines on the internet about using the old school tools and would probably be more of using sf
, stars
, and terra
already.
- Raster — data consisted of a matrix of cells (or pixels) organized into rows and columns (or a grid) where each cell contains a value representing information. It is also known as RasterLayer of the
raster
package and SpatRaster of theterra
package. - Extent — Bounding coordinates — of a raster/vector data. It is also known as Extent of the
raster
package and SpatExtent of theterra
package. - CRS — Coordinate Reference System — defines how the two-dimensional, projected map is related to real locations on the earth.
- Westernmost — refers to the x-axis or longitude value that represents the farthest point to the west of a raster/vector data.
- Easternmost — refers to the x-axis or longitude value that represents the farthest point to the east of a raster/vector data.
Chapters | ❌ Old school | ✅ Next generation |
---|---|---|
Creating a raster | raster::raster() |
terra::rast() |
Setting raster values | raster::setValues() |
terra::setValues() |
Storing raster as a file | raster::writeRaster() |
terra::writeRaster() |
Reading raster from a file | raster::raster() |
terra::rast() |
Getting raster values | raster::getValues() |
terra::values() |
Getting the extent of a raster | raster::extent() |
terra::ext() |
Getting the raw bounding coordinates of a raster | raster::xmin() ,raster::xmax() , raster::ymin() ,raster::ymax() |
terra::xmin() ,terra::xmax() , terra::ymin() ,terra::ymax() |
Getting the dimensions of a raster | raster::nrow() ,raster::ncol() |
terra::nrow() ,terra::ncol() |
Getting the resolution of a raster | raster::xres() ,raster::yres() |
terra::xres() ,terra::yres() |
Getting the CRS of a raster | raster::crs() |
terra::crs() |
Getting the cell index from x and y coordinates | raster::cellFromXY() |
terra::cellFromXY() |
Getting the x and y coordinates from cell index | raster::xyFromCell() |
terra::xyFromCell() |
Cropping a raster | raster::crop() |
terra::crop() |
raster_data <- raster::raster(ncol=3, nrow=3, xmn=0, xmx=3, ymn=0, ymx=3)
print( raster_data )
raster_data <- terra::rast(ncol=3, nrow=3, xmin=0, xmax=3, ymin=0, ymax=3)
print( raster_data )
Aspect | raster |
terra |
---|---|---|
Function name | raster() |
rast() |
Westernmost extent argument | xmn |
xmin |
Easternmost extent argument | xmx |
xmax |
Northernmost extent argument | ymx |
ymax |
Southernmost extent argument | ymn |
ymin |
# Creating a blank raster
raster_data <- raster::raster(ncol=3, nrow=3, xmn=0, xmx=3, ymn=0, ymx=3)
# Setting the values of each cell (pixel)
raster_data <- raster::setValues( raster_data, c(1,2,3,4,5,6,7,8,9) )
# Plot to graphical rendering of the raster
raster::plot( raster_data )
# Creating a blank raster
raster_data <- terra::rast(ncol=3, nrow=3, xmin=0, xmax=3, ymin=0, ymax=3)
# Setting the values of each cell (pixel)
raster_data <- terra::setValues( raster_data, c(1,2,3,4,5,6,7,8,9) )
# Plot to graphical rendering of the raster
terra::plot( raster_data )
# Creating a blank raster
raster_data <- raster::raster(ncol=3, nrow=3, xmn=0, xmx=3, ymn=0, ymx=3)
# Setting the values of each cell (pixel)
raster_data <- raster::setValues( raster_data, c(1,2,3,4,5,6,7,8,9) )
# Storing raster to as a file
raster::writeRaster( raster_data, '/path/to/raster.tif' )
# Creating a blank raster
raster_data <- terra::rast(ncol=3, nrow=3, xmin=0, xmax=3, ymin=0, ymax=3)
# Setting the values of each cell (pixel)
raster_data <- terra::setValues( raster_data, c(1,2,3,4,5,6,7,8,9) )
# Storing raster to as a file
terra::writeRaster( raster_data, '/path/to/raster.tif' )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Getting the values of each cell (pixel)
raster_data_values <- raster::getValues( raster_data )
print( raster_data_values )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Getting the values of each cell (pixel)
raster_data_values <- terra::values( raster_data )
print( raster_data_values )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Getting the extent
raster::extent( raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Getting the extent
terra::ext( raster_data )
Aspect | raster |
terra |
---|---|---|
Function name | extent() |
ext() |
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Westernmost
raster::xmin( raster_data )
# Easternmost
raster::xmax( raster_data )
# Northernmost
raster::ymax( raster_data )
# Southernmost
raster::ymin( raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Westernmost
terra::xmin( raster_data )
# Easternmost
terra::xmax( raster_data )
# Northernmost
terra::ymax( raster_data )
# Southernmost
terra::ymin( raster_data )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Number of rows
number_of_rows <- raster::nrow( raster_data )
# Number of columns
number_of_columns <- raster::ncol( raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Number of rows
number_of_rows <- terra::nrow( raster_data )
# Number of columns
number_of_columns <- terra::ncol( raster_data )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# X resolution
x_resolution <- raster::xres( raster_data )
# Y resolution
y_resolution <- raster::yres( raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# X resolution
x_resolution <- terra::xres( raster_data )
# Y resolution
y_resolution <- terra::yres( raster_data )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Getting the coordinate reference system
coordinate_reference_system <- raster::crs( raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Getting the coordinate reference system
coordinate_reference_system <- terra::crs( raster_data )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Getting the cell index from x and y coordinates
index <- raster::cellFromXY( raster_data, c(161.82, 45) )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Create a matrix
matrix_data <- matrix( c(161.82, 45), nrow = 1, ncol = 2, byrow = TRUE)
colnames(matrix_data) <- c("x","y")
# Getting the cell index from x and y coordinates
index <- terra::cellFromXY( raster_data, matrix_data )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Getting the x and y coordinates from cell index
coordinates <- raster::xyFromCell( raster_data, 1 )
print( coordinates )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Getting the x and y coordinates from cell index
coordinates <- terra::xyFromCell( raster_data, 1 )
print( coordinates )
# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )
# Generating a simulated extent for the output raster by cutting the edges of the input raster
westernmost <- raster::xmin( raster_data ) + raster::xres( raster_data )
easternmost <- raster::xmax( raster_data ) - raster::xres( raster_data )
northernmost <- raster::ymax( raster_data ) - raster::yres( raster_data )
southernmost <- raster::ymin( raster_data ) + raster::yres( raster_data )
output_extent <- raster::extent( westernmost, easternmost, southernmost, northernmost )
# Cropping of the input raster as the output raster
cropped_raster_data <- raster::crop( raster_data, output_extent )
raster::plot( cropped_raster_data )
# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )
# Generating a simulated extent for the output raster by cutting the edges of the input raster
westernmost <- terra::xmin( raster_data ) + terra::xres( raster_data )
easternmost <- terra::xmax( raster_data ) - terra::xres( raster_data )
northernmost <- terra::ymax( raster_data ) - terra::yres( raster_data )
southernmost <- terra::ymin( raster_data ) + terra::yres( raster_data )
output_extent <- terra::ext( westernmost, easternmost, southernmost, northernmost )
# Cropping of the input raster as the output raster
cropped_raster_data <- terra::crop( raster_data, output_extent )
terra::plot( cropped_raster_data )