KEMBAR78
Handling Digital Elevation Model Data With R | PDF | Spatial Analysis | Computer Programming
0% found this document useful (0 votes)
3 views10 pages

Handling Digital Elevation Model Data With R

This document provides a comprehensive guide on handling Digital Elevation Models (DEMs) using R and RStudio, focusing on geospatial analysis techniques. It covers essential skills such as importing, visualizing, and processing DEM data, as well as deriving key terrain attributes like slope, aspect, Terrain Ruggedness Index (TRI), and Topographic Wetness Index (TWI). The exercise emphasizes reproducibility and automation in analysis, along with practical steps and required packages for effective DEM manipulation.

Uploaded by

manasranjandash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Handling Digital Elevation Model Data With R

This document provides a comprehensive guide on handling Digital Elevation Models (DEMs) using R and RStudio, focusing on geospatial analysis techniques. It covers essential skills such as importing, visualizing, and processing DEM data, as well as deriving key terrain attributes like slope, aspect, Terrain Ruggedness Index (TRI), and Topographic Wetness Index (TWI). The exercise emphasizes reproducibility and automation in analysis, along with practical steps and required packages for effective DEM manipulation.

Uploaded by

manasranjandash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Exercise 7 Handling DEM Data with R

...............................................….….........................................................................................................

EXERCISE 7

HANDLING DEM DATA WITH R


Outline of Experiment_______________________________________________
7.1 Introduction Reclassify DEM (Optional)

Expected Learning Skills Calculation of TWI (Topographic Wetness


Index)
7.2 Requirements
7.4 Exercise: To Be Submitted
7.3 Steps
7.5 Exercise: Do it Yourself
Calculation of Slope
7.6 Further/Suggested Readings
Calculation of Aspect

Calculation of TRI (Terrain Ruggedness Index)

7.1 INTRODUCTION
This chapter introduces the application of R programming and RStudio for geospatial analysis,
specifically focusing on the manipulation and analysis of Digital Elevation Models (DEMs). DEMs
are fundamental datasets in Earth science, environmental management, and various other
disciplines, representing the bare Earth's surface elevation.They provide a crucial foundation for
understanding terrain characteristics, hydrological processes, geomorphology, and numerous other
spatial phenomena. This chapter empowers readers to leverage the robust analytical capabilities of
R to extract valuable information from DEMs, enabling them to conduct sophisticated geospatial
analyses.
Traditional Geographic Information Systems (GIS) software often relies on graphical user interfaces
(GUIs), which can be limiting for complex or repetitive tasks. R, a powerful open-source
programming language and environment for statistical computing and graphics, offers a flexible and
scriptable approach to geospatial analysis. This allows for automation, reproducibility, and the
development of custom analytical workflows. RStudio, an integrated development environment
(IDE) for R, further enhances the user experience by providing a user-friendly interface with
features like code editing, debugging, and visualization tools. 4

1
MGYL-012 Advanced Geoinformatics Laboratory
.............….….............................................................................................................................................
This exercise is structured to guide you through the essential steps of working
with DEMs in R, from data import and manipulation to the derivation of key
terrain attributes. We will cover the following key aspects:
1. Introduction to R and RStudio for Geospatial Analysis:
This section provides a brief overview of R and RStudio, emphasizing their
relevance in the context of geospatial analysis. It covers the installation of
necessary packages, such as raster, rgdal (or sf for more modern shapefile
handling), rasterVis, and others that provide functionalities for raster data
processing, spatial data input/output, and visualization. We will also
introduce basic R syntax and data structures relevant to working with spatial
data.
2. Importing and Visualizing DEM Data:
This section focuses on importing DEM data into R. We will demonstrate
how to read various DEM formats, such as GeoTIFF, into R using the raster
package. We will then explore methods for visualization tools provided by
packages like rasterVis. Effective visualization is crucial for understanding
the spatial distribution of elevation values and identifying potential data
issues.
3. DEM Pre-processing:
Before deriving terrain attributes, DEMs often require pre-processing steps.5
This section covers essential pre-processing techniques, including:
 Projection and Coordinate Reference Systems (CRS): Ensuring that the
DEM is in the correct CRS is crucial for accurate spatial analysis. We
will demonstrate how to check, define, and reproject DEMs using R.
 Data Cleaning and Gap Filling: DEMs can sometimes contain data gaps
or errors.6 We will introduce basic methods for identifying and
addressing these issues.
 Resampling: Changing the spatial resolution of a DEM can be
necessary for certain analyses. We will demonstrate how to resample
DEMs using different resampling techniques.
4. Deriving Terrain Attributes:
This is the core of the chapter, where we delve into the derivation of key
terrain attributes from DEMs using R. We will cover the following important
metrics:
 Slope: Slope represents the steepness of the terrain and is a
fundamental parameter in various applications, including hydrology,
geomorphology, and land management. We will demonstrate how to
calculate slope in degrees or percent using the terrain function from the
raster package.
 Aspect: Aspect represents the direction a slope faces and is crucial for
understanding solar radiation, microclimate, and vegetation distribution.
We will demonstrate how to calculate aspect in degrees using the terrain
function.

2
Exercise 7 Handling DEM Data with R
...............................................….….........................................................................................................
 Topographic Ruggedness Index (TRI): TRI quantifies the variation in
elevation within a given area and is used to characterize terrain
complexity. We will demonstrate how to calculate TRI using focal
functions or other methods.
 Topographic Wetness Index (TWI): TWI is a measure of the tendency of
an area to accumulate water and is widely used in hydrological
modeling and soil mapping. We will demonstrate how to calculate TWI
using flow accumulation algorithms and slope data.
5. Reproducibility and Automation:
Throughout the chapter, we will emphasize the importance of reproducible
research by demonstrating how to write reusable R scripts for DEM
analysis. This allows for efficient automation of tasks and ensures that
analyses can be easily replicated.

Expected Learning Skills______________________________


Upon completion of this exercise, you should be able to:
 import, visualise, and pre-process DEM data in R;

 derive key terrain attributes, including slope, aspect, TRI, and TWI, using
R; and
 write reproducible R scripts for DEM data processing.

7.2 REQUIREMENTS
You must have R and R Studio installed and updated for this exercise.
Required data i.e. SRTM DEM of INDIA on a scale of 0.0083 degree is provided
for usage.
Install Necessary Packages
install.packages("terra") # For spatial data handling
install.packages("raster") # Alternative to 'terra'
install.packages("sp") # For spatial data
install.packages("rgdal") # For reading/writing spatial data
install.packages("elevatr") # To download DEM (if needed)

7.3 STEPS
Follow the steps given here for deriving various derivatives from DEM.
7.3.1 Calculation of Slope
Calculating slope from DEMs in RStudio is crucial for understanding terrain.
Slope, a fundamental terrain attribute, influences various processes in
geomorphology, hydrology, soil science, and ecology. Its applications span civil
engineering, agriculture, forestry, urban planning, and natural hazard
assessment. RStudio's open-source nature, scripting capabilities, and
integration with geospatial packages like raster (using the terrain function)
enable efficient and reproducible slope calculation and analysis.

3
MGYL-012 Advanced Geoinformatics Laboratory
.............….….............................................................................................................................................
slope <- terrain(elevation, v = "slope", unit = "degrees") # Calculate slope
plot(slope)

7.3.2 Calculation of Aspect


Calculating aspect from DEMs in RStudio is crucial for understanding how slope
orientation influences environmental factors. Aspect directly affects solar
radiation, creating distinct microclimates that impact vegetation, hydrology, and
snowmelt patterns. This information is valuable in ecology, agriculture, forestry,
and geomorphology. RStudio's raster package, particularly the terrain function,
enables efficient aspect calculation and integration with other spatial analyses.
Combined with R's visualization capabilities, aspect maps provide valuable
insights for informed decision-making in various fields.
aspect <- terrain(elevation, v = "aspect", unit = "degrees") # Calculate aspect
plot(aspect)

4
Exercise 7 Handling DEM Data with R
...............................................….….........................................................................................................

7.3.3 Calculation of TRI (Terrain Ruggedness Index)


Calculating the Topographic Ruggedness Index (TRI) from DEMs in RStudio is
important for quantifying terrain heterogeneity. TRI measures elevation
differences within a defined area, indicating surface roughness. This is valuable
in ecological studies (habitat diversity), geomorphology (landform
classification), and soil science (erosion potential). R's raster package, using
focal functions or other methods, enables efficient TRI calculation. Analyzing
TRI helps understand landscape complexity, identify diverse habitats, and
assess terrain influence on various processes, aiding in ecological modeling
and landscape management.
tri <- terrain(elevation, v = "TRI")

5
MGYL-012 Advanced Geoinformatics Laboratory
.............….….............................................................................................................................................

7.3.4 Reclassify DEM (Optional)


If you suspect errors or very high ruggedness, reclassify the DEM to limit
extreme elevation differences:
classified_elevation<- clamp(elevation, lower = min(elevation), upper =
max(elevation) * 0.95)
tri_c<- terrain(classified_elevation, v = "TRI")
clamp(elevation, lower = min(elevation), upper = max(elevation) * 0.95)
 Purpose: This function modifies the DEM values by "clamping" (limiting)
them to a specific range.
 Arguments:
6
Exercise 7 Handling DEM Data with R
...............................................….….........................................................................................................
o elevation: The input Digital Elevation Model (DEM) raster.
o lower = min(elevation): Sets the minimum value for the DEM to its
current minimum.
o upper = max(elevation) * 0.95: Limits the maximum value of the
DEM to 95% of its current maximum (to remove extreme outliers).
 Result: Elevation values in the DEM that are above 95% of the maximum
value are "clamped" (set) to this 95% threshold, reducing the influence of
extreme values.
 > plot(tri_c)

7.3.5 Calculation of TWI (Topographic Wetness Index)


Calculating the Topographic Wetness Index (TWI) from DEMs in RStudio is
crucial for understanding water accumulation patterns. TWI, derived from slope
and flow accumulation, predicts areas prone to saturation and runoff. This is
vital for hydrological modeling, soil mapping, and ecological studies. RStudio's
raster package, combined with functions for flow accumulation and slope
calculation, facilitates efficient TWI derivation. Analyzing TWI helps identify
wetlands, predict flood risk, and understand soil moisture distribution, aiding in
land management and environmental planning.
# Calculate flow accumulation
flow_accum<- terrain(elevation, v = "flowdir") # Flow direction
# Calculate TWI
twi<- log(flow_accum / tan(slope))
plot(twi)

7
MGYL-012 Advanced Geoinformatics Laboratory
.............….….............................................................................................................................................

Your twi raster has an issue because it contains Inf (infinity) and possibly
undefined min value (- ?), which is likely causing incorrect calculations. Let’s
resolve this step by step
1. Identify the Problem

From your raster summary:


 Inf values: These can occur due to division by zero or invalid operations
(e.g., flat terrain with a slope of 0).
 Negative or undefined values: This may occur due to errors in DEM or
derived layers (like slope or flow accumulation).
2. Fix the Raster Values

Here’s how to handle these issues:


a) Replace Inf Values
Replace all Inf values with NA:
twi[twi == Inf] <- NA
b) Replace -Inf or Undefined Values
Similarly, handle -Inf or undefined values:
twi[twi == -Inf] <- NA
8
Exercise 7 Handling DEM Data with R
...............................................….….........................................................................................................
c) Replace NA Values
If replacing Inf results in NA values, you can fill them with:
 0
twi[is.na(twi)] <- 0
. Mean of the raster (excluding NA):
twi[is.na(twi)] <- global(twi, fun = "mean", na.rm = TRUE)

Key Notes
 v determines the type of analysis you are conducting on the DEM.

9
MGYL-012 Advanced Geoinformatics Laboratory
.............….….............................................................................................................................................
 Different terrain attributes (like slope or aspect) are useful for specific types
of geographic and environmental analyses.
 You can chain multiple calculations for different attributes on the same
DEM.

7.4 EXERCISES: TO BE SUBMITTED


Submit answers to the following to your counsellor for evaluation:
1. slope map derived from the DEM using R in a different colour scheme.
2. aspect map derived from the DEM using R in a different colour scheme.
3. TRI derived from the DEM using R in a different colour scheme.
4. TWI derived from the DEM using R in a different colour scheme.

7.5 EXERCISES: DO IT YOURSELF


1. Derive other products from DEM such as Hill-shade.
2. Use a boundary map of India and crop the lowest values of TWI within the
boundary.

7.6 FURTHER/SUGGESTED READINGS


 Wickham, H. and Grolemund, G. (2017). R for data science: Import, tidy,
transform, visualize, and model data. O'Reilly Media, Inc.
 Crawley, M. J. (2012). The R book. John Wiley & Sons.

10

You might also like