Skip to content

Instantly share code, notes, and snippets.

@abelcallejo
Last active May 29, 2024 08:23
Show Gist options
  • Save abelcallejo/61197a04c58135109fa11f5142d0640e to your computer and use it in GitHub Desktop.
Save abelcallejo/61197a04c58135109fa11f5142d0640e to your computer and use it in GitHub Desktop.
Next generation of spatial tools for R

r

Next generation of spatial tools for R

raster maptools rgdal rgeos sp1 sp2

terra sf stars sp3

How do you do, fellow R programmers?

This is how you would look if you just started studying spatial tools for R in 2022-2026.

oldschool

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.

nextgeneration

Definition of terms

Table of contents

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()

Creating a raster

❌ Old school

raster_data <- raster::raster(ncol=3, nrow=3, xmn=0, xmx=3, ymn=0, ymx=3)
print( raster_data )

✅ Next generation

raster_data <- terra::rast(ncol=3, nrow=3, xmin=0, xmax=3, ymin=0, ymax=3)
print( raster_data )
Differences
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

Setting raster values

❌ Old school

# 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 )

✅ Next generation

# 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 )

Storing raster as a file

❌ Old school

# 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' )

✅ Next generation

# 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 from a file

❌ Old school

# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )

✅ Next generation

# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )

Getting raster values

❌ Old school

# 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 )

✅ Next generation

# 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 )

Getting the extent of a raster

❌ Old school

# Reading raster data from an existing raster file
raster_data <- raster::raster( '/path/to/raster.tif' )

# Getting the extent
raster::extent( raster_data )

✅ Next generation

# Reading raster data from an existing raster file
raster_data <- terra::rast( '/path/to/raster.tif' )

# Getting the extent
terra::ext( raster_data )
Differences
Aspect raster terra
Function name extent() ext()

Getting the raw bounding coordinates of a raster

❌ Old school

# 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 )

✅ Next generation

# 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 )

Getting the dimensions of a raster

❌ Old school

# 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 )

✅ Next generation

# 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 )

Getting the resolution of a raster

❌ Old school

# 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 )

✅ Next generation

# 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 )

Getting the CRS of a raster

❌ Old school

# 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 )

✅ Next generation

# 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 )

Getting the cell index from x and y coordinates

❌ Old school

# 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) )

✅ Next generation

# 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 )

Getting the x and y coordinates from cell index

❌ Old school

# 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 )

✅ Next generation

# 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 )

Cropping a raster

❌ Old school

# 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 )

✅ Next generation

# 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 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment