This document provides instructions and code snippets for preparing soil and covariate data for digital soil mapping. It describes downloading climate and soil map data and processing them into raster stacks. It also shows how to extract covariate values to point data on soil properties and export a formatted data table for use in modeling and mapping soil organic carbon. The overall aim is to generate spatially explicit predictions of soil properties by relating point data to landscape covariates.
1. FAO- Global Soil
Partnership
Training on
Digital Soil Organic Carbon
Mapping
20-24 January 2018
Tehran/Iran
Yusuf YIGINI, PhD - FAO, Land and Water Division (CBL)
Guillermo Federico Olmedo, PhD - FAO, Land and Water Division (CBL)
6. #For handling raster data, we load raster package
library(raster)
#load DEM from tif file
DEM <- raster("covs/DEMENV5.tif")
plot(DEM)
The example covariates from this chapter were prepared by
ISRIC (ISRIC, 2017).
7. DEM
#For handling raster data, we load raster package
library(raster)
#load DEM from tif file
DEM <- raster("covs/DEMENV5.tif")
plot(DEM)
The example covariates from this chapter were prepared by
ISRIC (ISRIC, 2017).
8. Soil Maps
Soil maps play a crucial role for upscaling soil property data from point
locations. They can be the spatial layer for conventional upscaling, they
can also serve as a covariate in digital soil mapping.
Predicted soil property maps have lower quality in areas where the
covariates such as relief, geology and climate so not correlate well with
the dependent variable, here soil carbon stocks. This is especially true
for soils groundwater or stagnic water influence. This information is
well-represented in soil maps.
FAO, IIASA, ISRIC, ISS CAS and JRC produced a gridded 1 km soil
class map (HWSD).
9. Soil Maps
# load the soil map from a shapefile file
soilmap <- shapefile("MK_soilmap_simple.shp")
# plot the DEM together with the soil types
plot(DEM)
lines(soilmap)
Digitized small-scale national soil maps are the most important
spatial layer for soil property mapping. The higher its resolution,
the better soil maps contribute to high quality soil property maps -
considering that the map should cover the target area/full country
coverage.
10. DEM
#For handling raster data, we load raster package
library(raster)
#load DEM from tif file
DEM <- raster("covs/DEMENV5.tif")
plot(DEM)
The example covariates from this chapter were prepared by
ISRIC (ISRIC, 2017).
11. DEM
# the "Symbol" attribute from the vector layer will be used for the
# rasterization process. It has to be a factor
soilmap@data$Symbol <- as.factor(soilmap@data$Symbol)
#save the levels names in a character vector
Symbol.levels <- levels(soilmap$Symbol)
# The rasterization process needs a layer with the target grd
# system: spatial extent and cell size.
soilmap.r <- rasterize(x = soilmap, y = DEM, field = "Symbol")
# The DEM raster layer could be used for this.
plot(soilmap.r, col=rainbow(21))
legend("bottomright",legend = Symbol.levels, fill=rainbow(21),
cex=0.5)
Technical Steps - Rasterizing a vector layer in R
12. DEM
# the "Symbol" attribute from the vector layer will be used for the
# rasterization process. It has to be a factor
soilmap@data$Symbol <- as.factor(soilmap@data$Symbol)
#save the levels names in a character vector
Symbol.levels <- levels(soilmap$Symbol)
# The rasterization process needs a layer with the target grd
# system: spatial extent and cell size.
soilmap.r <- rasterize(x = soilmap, y = DEM, field = "Symbol")
# The DEM raster layer could be used for this.
plot(soilmap.r, col=rainbow(21))
legend("bottomright",legend = Symbol.levels, fill=rainbow(21),
cex=0.5)
Technical Steps - Rasterizing a vector layer in R
13. LAND COVER
landcover <- raster("covs/LCEE10.tif")
# Land cover is a categorical covariate, this has to be made
# explicit using function as.factor()
landcover <- as.factor(landcover)
landcover
## class : RasterLayer
## dimensions : 182, 310, 56420 (nrow, ncol, ncell)
## resolution : 0.008327968, 0.008353187 (x, y)
## extent : 20.45242, 23.03409, 40.8542, 42.37448 (xmin, xmax,
ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0
plot(landcover)
14. LAND COVER
landcover <- raster("covs/LCEE10.tif")
# Land cover is a categorical covariate, this has to be made
# explicit using function as.factor()
landcover <- as.factor(landcover)
landcover
## class : RasterLayer
## dimensions : 182, 310, 56420 (nrow, ncol, ncell)
## resolution : 0.008327968, 0.008353187 (x, y)
## extent : 20.45242, 23.03409, 40.8542, 42.37448 (xmin, xmax,
ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0
plot(landcover)
15. CLIMATE
WorldClim Climate Data are available at: www.worldclim.org
(WorldClim 1.4 (current conditions) by www.worldclim.org;
Hijmans et al., 2005. Int. J. of Clim. 25: 1965-1978. Is licensed
under a Creative Commons Attribution-ShareAlike 4.0
International License).
16. CLIMATE
# load the climate covariates from the raster tif files
files <- list.files(path = "covs/", pattern = "CHE3.tif",
full.names = TRUE)
# stack all the files in one RasterStack
climate <- stack(files)
# plot the first 2 layers
plot(climate[[1:2]])
17. CLIMATE
# load the climate covariates from the raster tif files
files <- list.files(path = "covs/", pattern = "CHE3.tif",
full.names = TRUE)
# stack all the files in one RasterStack
climate <- stack(files)
# plot the first 2 layers
plot(climate[[1:2]])
18. OVERLAYING SOIL DATA AND COVARIATES
# Load the processed data. This table was prepared in the previous
# chapter.
dat <- read.csv("dataproc.csv")
files <- list.files(path = "covs", pattern = "tif$",
full.names = TRUE)
covs <- stack(files)
19. OVERLAYING SOIL DATA AND COVARIATES
covs <- stack(covs, soilmap.r)
# correct the name for layer 14
names(covs)[14] <- "soilmap"
#mask the covariates with the country mask from the data repository
mask <- raster("data/mask.tif")
covs <- mask(x = covs, mask = mask)
plot(covs)
20. OVERLAYING SOIL DATA AND COVARIATES
covs <- stack(covs, soilmap.r)
# correct the name for layer 14
names(covs)[14] <- "soilmap"
#mask the covariates with the country mask from the data repository
mask <- raster("data/mask.tif")
covs <- mask(x = covs, mask = mask)
plot(covs)
21. OVERLAYING SOIL DATA AND COVARIATES
#upgrade points data frame to SpatialPointsDataFrame
coordinates(dat) <- ~ X + Y
# extract values from covariates to the soil points
dat <- extract(x = covs, y = dat, sp = TRUE)
# LCEE10 and soilmap are categorical variables
dat@data$LCEE10 <- as.factor(dat@data$LCEE10)
dat@data$soilmap <- as.factor(dat@data$soilmap)
#levels(soilmap) <- Symbol.levels
summary(dat@data)
22. OVERLAYING SOIL DATA AND COVARIATES
dat <- as.data.frame(dat)
# The points with NA values has to be removed
dat <- dat[complete.cases(dat),]
# export as a csv table
write.csv(dat, "data/MKD_RegMatrix.csv", row.names = FALSE)
23. OVERLAYING SOIL DATA AND COVARIATES
dat <- as.data.frame(dat)
# The points with NA values has to be removed
dat <- dat[complete.cases(dat),]
# export as a csv table
write.csv(dat, "data/MKD_RegMatrix.csv", row.names = FALSE)