Today

  • Read and write raster data
  • Create rasters
  • Raster and its attributes
  • Conversion between raster and vector

A warm up practice

  1. Read the country and carnivorous plant data, then convert both to the projection ESRI:54009.
  2. Keep only the countries in South America, then use a spatial join to identify the plant records located in South America.
  3. Use ggplot2 to make a map of South America with plant occurrence points colored by family.

A warm up practice

# Load libraries

# Read data
countries <- rnaturalearth::ne_countries(scale = 110) %>% 
    select(continent, name)

# Add extra code to process countries here, then
# Filter countries in South America
countries_sa <- 
    
# Load carnivorous plant data
car_plants <- read.csv("docs/Riched_Carnivorous_Plant.csv")

# Add extra code to process car_plants here
car_plants <- 

# Subset carnivorous plant to South America only
car_plants_sa <- 

# Plot
ggplot() +
    # finish this line
    geom_sf(data = countries_sa, ) + 
    # finish this line
    geom_sf(data = car_plants_sa, ) + 
    # Add extra setting

Read raster

rast in package terra to read raster.

library(terra)
fname <- system.file('extdata/bioclim.tif', package = 'sdadata')

# read one band
bio2 <- rast(fname, lyrs = 2)

# Read the whole raster stack
bioclim_vars <- rast(fname)

Raster attributes

# Check info
res(bioclim_vars)
crs(bioclim_vars)
ext(bioclim_vars)
nlyr(bioclim_vars)
names(bioclim_vars)

# Get pixel values
vals <- values(bioclim_vars)

# Set values
values(bioclim_vars) <- vals

# Get one layer using $
bio1 <- bioclim_vars$bio01

# Or subset multiple layers
sub_vars <- subset(bioclim_vars, c(1, 3, 5))

Create a raster

The same, rast in package terra.

# From extent
e <- ext(bioclim_vars)
rst <- rast(e, nrows = 10, ncols = 10, 
              crs = crs("EPSG:4326"), # important
              vals = 1:100)
plot(rst)

# From scratch
rst <- rast(nrows = 10, ncols = 10, 
              xmin = 29.59, xmax = 40.44,
              ymin = -11.76, ymax = -0.98)
crs(rst) <- crs("EPSG:4326")
values(rst) <- sample(1:100, 100)

# From another raster
rst <- rast(bioclim_vars) # Just a blank raster, no values

# Choose layer
rst <- rast(bioclim_vars, nlyrs = 1)

Reproject

# Use a defined CRS object
vars_proj <- project(bioclim_vars, crs("ESRI:54009"))

# Or get crs from an existing object
vars_latlon <- project(vars_proj, crs(bioclim_vars))

# Or directly use an existing object
vars_latlon_rep <- project(vars_proj, bioclim_vars)

Write raster

writeRaster to save out a raster.

dst_fname <- "your_class_note/bioclim_vars.tif"
writeRaster(bioclim_vars, dst_fname, overwrite = TRUE)

Practice 1

  • Download the global gridded population of 2020 using population function in package geodata.
  • Project the layer to projection of ESRI:54009.
  • Change the values of population to log(population + 1).
  • Save it to your class note folder with name “population_log_2020.tif”.

Conversion between raster and vector

  • sf vs. vect
  • rasterize: vector to raster
  • as.polygons, as.points, etc.: raster to vector
# sf vs vect
tza <- rnaturalearth::ne_countries(country = "United Republic of Tanzania")
class(tza)
tza <- vect(tza)
class(tza)
tza <- st_as_sf(tza)
class(tza)

# Raster to vector
cnts_rst <- rasterize(countries_sa, pop_global, field = "name")
cnts_rst <- trim(cnts_rst)

# vector to raster
cnts_vct <- as.polygons(cnts_rst, dissolve = T) %>% 
    st_as_sf()

Practice 2

  • Rasterize car_plants_sa with pop_global as the template.
  • Convert the raster back to points.
  • Save the points to your class note folder with name “car_plants_occurrence.geojson”.

Homework

  • Finish reading Unit2 - Module 2a.