Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.1.251">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Mapping and spatial analyses in R for One Health studies - 4 Using raster data</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./05-mapping_with_r.html" rel="next">
<link href="./03-vector_data.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit"
}
}</script>
<style>html{ scroll-behavior: smooth; }</style>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<div class="container-fluid d-flex justify-content-between">
<h1 class="quarto-secondary-nav-title"><span class="chapter-number">4</span> <span class="chapter-title">Using raster data</span></h1>
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
<i class="bi bi-chevron-right"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Mapping and spatial analyses in R for One Health studies</a>
<div class="sidebar-tools-main">
<a href="https://forge.ird.fr/espace-dev/personnels/longour/geohealth/documentation/rspatial-for-onehealth" title="Source Code" class="sidebar-tool px-1"><i class="bi bi-git"></i></a>
</div>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">Preface</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./01-introduction.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./02-data_acquisition.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span> <span class="chapter-title">Data Acquisition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./03-vector_data.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span> <span class="chapter-title">Using vector data</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./04-raster_data.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">4</span> <span class="chapter-title">Using raster data</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./05-mapping_with_r.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span> <span class="chapter-title">Mapping With R</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./07-basic_statistics.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">6</span> <span class="chapter-title">Basic statistics for spatial analysis</span></a>
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./references.html" class="sidebar-item-text sidebar-link">References</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#format-of-objects-spatraster" id="toc-format-of-objects-spatraster" class="nav-link active" data-scroll-target="#format-of-objects-spatraster"><span class="toc-section-number">4.1</span> Format of objects <code>SpatRaster</code></a></li>
<li><a href="#importing-and-exporting-data" id="toc-importing-and-exporting-data" class="nav-link" data-scroll-target="#importing-and-exporting-data"><span class="toc-section-number">4.2</span> Importing and exporting data</a></li>
<li><a href="#displaying-a-spatraster-object" id="toc-displaying-a-spatraster-object" class="nav-link" data-scroll-target="#displaying-a-spatraster-object"><span class="toc-section-number">4.3</span> Displaying a SpatRaster object</a></li>
<li><a href="#change-to-the-study-area" id="toc-change-to-the-study-area" class="nav-link" data-scroll-target="#change-to-the-study-area"><span class="toc-section-number">4.4</span> Change to the study area</a>
<ul>
<li><a href="#reprojections" id="toc-reprojections" class="nav-link" data-scroll-target="#reprojections"><span class="toc-section-number">4.4.1</span> (Re)projections</a></li>
<li><a href="#crop" id="toc-crop" class="nav-link" data-scroll-target="#crop"><span class="toc-section-number">4.4.2</span> Crop</a></li>
<li><a href="#mask" id="toc-mask" class="nav-link" data-scroll-target="#mask"><span class="toc-section-number">4.4.3</span> Mask</a></li>
<li><a href="#aggregation-and-disaggregation" id="toc-aggregation-and-disaggregation" class="nav-link" data-scroll-target="#aggregation-and-disaggregation"><span class="toc-section-number">4.4.4</span> Aggregation and disaggregation</a></li>
<li><a href="#raster-fusion" id="toc-raster-fusion" class="nav-link" data-scroll-target="#raster-fusion"><span class="toc-section-number">4.4.5</span> Raster fusion</a></li>
<li><a href="#segregate" id="toc-segregate" class="nav-link" data-scroll-target="#segregate"><span class="toc-section-number">4.4.6</span> Segregate</a></li>
</ul></li>
<li><a href="#map-algebra" id="toc-map-algebra" class="nav-link" data-scroll-target="#map-algebra"><span class="toc-section-number">4.5</span> Map Algebra</a>
<ul>
<li><a href="#local-operations" id="toc-local-operations" class="nav-link" data-scroll-target="#local-operations"><span class="toc-section-number">4.5.1</span> Local operations</a>
<ul class="collapse">
<li><a href="#value-replacement" id="toc-value-replacement" class="nav-link" data-scroll-target="#value-replacement"><span class="toc-section-number">4.5.1.1</span> Value replacement</a></li>
<li><a href="#operation-on-each-cell" id="toc-operation-on-each-cell" class="nav-link" data-scroll-target="#operation-on-each-cell"><span class="toc-section-number">4.5.1.2</span> Operation on each cell</a></li>
<li><a href="#reclassification" id="toc-reclassification" class="nav-link" data-scroll-target="#reclassification"><span class="toc-section-number">4.5.1.3</span> Reclassification</a></li>
<li><a href="#operation-on-several-layers-ex-ndvi" id="toc-operation-on-several-layers-ex-ndvi" class="nav-link" data-scroll-target="#operation-on-several-layers-ex-ndvi"><span class="toc-section-number">4.5.1.4</span> Operation on several layers (ex: NDVI)</a></li>
</ul></li>
<li><a href="#focal-operations" id="toc-focal-operations" class="nav-link" data-scroll-target="#focal-operations"><span class="toc-section-number">4.5.2</span> Focal operations</a>
<ul class="collapse">
<li><a href="#focal-operations-for-elevation-rasters" id="toc-focal-operations-for-elevation-rasters" class="nav-link" data-scroll-target="#focal-operations-for-elevation-rasters"><span class="toc-section-number">4.5.2.1</span> Focal operations for elevation rasters</a></li>
</ul></li>
<li><a href="#global-operations" id="toc-global-operations" class="nav-link" data-scroll-target="#global-operations"><span class="toc-section-number">4.5.3</span> Global operations</a></li>
<li><a href="#zonal-operation" id="toc-zonal-operation" class="nav-link" data-scroll-target="#zonal-operation"><span class="toc-section-number">4.5.4</span> Zonal operation</a>
<ul class="collapse">
<li><a href="#zonal-operation-on-an-extraction" id="toc-zonal-operation-on-an-extraction" class="nav-link" data-scroll-target="#zonal-operation-on-an-extraction"><span class="toc-section-number">4.5.4.1</span> Zonal operation on an extraction</a></li>
<li><a href="#zonal-operation-from-a-vector-layer" id="toc-zonal-operation-from-a-vector-layer" class="nav-link" data-scroll-target="#zonal-operation-from-a-vector-layer"><span class="toc-section-number">4.5.4.2</span> Zonal operation from a vector layer</a></li>
<li><a href="#zonal-operation-from-raster" id="toc-zonal-operation-from-raster" class="nav-link" data-scroll-target="#zonal-operation-from-raster"><span class="toc-section-number">4.5.4.3</span> Zonal operation from raster</a></li>
</ul></li>
</ul></li>
<li><a href="#transformation-and-conversion" id="toc-transformation-and-conversion" class="nav-link" data-scroll-target="#transformation-and-conversion"><span class="toc-section-number">4.6</span> Transformation and conversion</a>
<ul>
<li><a href="#rasterization" id="toc-rasterization" class="nav-link" data-scroll-target="#rasterization"><span class="toc-section-number">4.6.1</span> Rasterization</a></li>
<li><a href="#vectorisation" id="toc-vectorisation" class="nav-link" data-scroll-target="#vectorisation"><span class="toc-section-number">4.6.2</span> Vectorisation</a></li>
<li><a href="#terra-raster-sf-stars" id="toc-terra-raster-sf-stars" class="nav-link" data-scroll-target="#terra-raster-sf-stars"><span class="toc-section-number">4.6.3</span> terra, raster, sf, stars…</a></li>
</ul></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title d-none d-lg-block"><span class="chapter-number">4</span> <span class="chapter-title">Using raster data</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>This chapter is largely inspired by two presentation; <span class="citation" data-cites="MmadelinSIGR">Madelin (<a href="references.html#ref-MmadelinSIGR" role="doc-biblioref">2021</a>)</span> and <span class="citation" data-cites="JNowosadSIGR">Nowosad (<a href="references.html#ref-JNowosadSIGR" role="doc-biblioref">2021</a>)</span>; carried out as part of the <a href="https://sigr2021.github.io/site/index.html">SIGR2021</a> thematic school.</p>
<section id="format-of-objects-spatraster" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="format-of-objects-spatraster"><span class="header-section-number">4.1</span> Format of objects <code>SpatRaster</code></h2>
<p>The package <code>terra</code> <span class="citation" data-cites="terra">(<a href="references.html#ref-terra" role="doc-biblioref">Hijmans 2022</a>)</span> allows to handle vector and raster data. To manipulate this spatial data, <code>terra</code> store it in object of type <code>SpatVector</code> and <code>SpatRaster</code>. In this chapter, we focus on the manipulation of raster data (<code>SpatRaster</code>) from functions offered by this package.</p>
<p>An object <code>SpatRaster</code> allows to handle vector and raster data, in one or more layers (variables). This object also stores a number of fundamental parameters that describe it (number of columns, rows, spatial extent, coordinate reference system, etc.).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/raster.png" class="img-fluid figure-img" width="350"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></figcaption><p></p>
</figure>
</div>
</section>
<section id="importing-and-exporting-data" class="level2" data-number="4.2">
<h2 data-number="4.2" class="anchored" data-anchor-id="importing-and-exporting-data"><span class="header-section-number">4.2</span> Importing and exporting data</h2>
<p>The package <code>terra</code> allows importing and exporting raster files. It is based on the <a href="https://gdal.org/">GDAL</a> library which makes it possible to read and process a very large number of geographic image formats.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(terra)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The function <code>rast()</code> allows you to create and/or import raster data. The following lines import the raster file <strong>elevation.tif</strong> (<a href="https://fr.wikipedia.org/wiki/Tagged_Image_File_Format"><em>Tagged Image File Format</em></a>) into an object of type <code>SpatRaster</code> (default).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>elevation <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/elevation.tif"</span>) </span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>elevation</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>class : SpatRaster
dimensions : 5235, 6458, 1 (nrow, ncol, nlyr)
resolution : 0.0008333394, 0.0008332568 (x, y)
extent : 102.2935, 107.6752, 10.33984, 14.70194 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : elevation.tif
name : elevation </code></pre>
</div>
</div>
<p>Modifying the name of the stored variable (altitude).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(elevation) <span class="ot"><-</span> <span class="st">"Altitude"</span> </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The function <code>writeRaster()</code> allow you to save an object <code>SpatRaster</code> on your machine, in the format of your choice.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">writeRaster</span>(<span class="at">x =</span> elevation, <span class="at">filename =</span> <span class="st">"data_cambodia/new_elevation.tif"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="displaying-a-spatraster-object" class="level2" data-number="4.3">
<h2 data-number="4.3" class="anchored" data-anchor-id="displaying-a-spatraster-object"><span class="header-section-number">4.3</span> Displaying a SpatRaster object</h2>
<p>The function <code>plot()</code> is use to display an object <code>SpatRaster</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(elevation)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/affichage_1_raster-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>A raster always contains numerical data, but it can be both quantitative data and numerically coded qualitative (categorical) data (ex: type of land cover).</p>
<p>Specify the type of data stored with the augment <code>type</code> (<code>type = "continuous"</code> default), to display them correctly.</p>
<p>Import and display of raster containing categorical data: Phnom Penh Land Cover 2019 (land cover types) with a resolution of 1.5 meters:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>lulc_2019 <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/lulc_2019.tif"</span>) <span class="co">#Import Phnom Penh landcover 2019, landcover types</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The landcover data was produced from SPOT7 satellite image with 1.5 meter spatial resolution. An extraction centered on the municipality of Phnom Penh was then carried out.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019, <span class="at">type =</span> <span class="st">"classes"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/affichage_raster_2-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
<p>To display the actual tiles of landcover types you can proceed as follows.</p>
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>class_name <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"Roads"</span>,</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"Built-up areas"</span>,</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Water Bodies and rivers"</span>,</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"Wetlands"</span>,</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Dry bare area"</span>,</span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Bare crop fields"</span>,</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"Low vegetation areas"</span>,</span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"High vegetation areas"</span>,</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"Forested areas"</span>)</span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a>class_color <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"#070401"</span>, <span class="st">"#c84639"</span>, <span class="st">"#1398eb"</span>,<span class="st">"#8bc2c2"</span>,</span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> <span class="st">"#dc7b34"</span>, <span class="st">"#a6bd5f"</span>,<span class="st">"#e8e8e8"</span>, <span class="st">"#4fb040"</span>, <span class="st">"#35741f"</span>)</span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019,</span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"classes"</span>,</span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a> <span class="at">levels =</span> class_name,</span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a> <span class="at">col =</span> class_color,</span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a> <span class="at">plg =</span> <span class="fu">list</span>(<span class="at">cex =</span> <span class="fl">0.7</span>),</span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a> <span class="at">mar =</span> <span class="fu">c</span>(<span class="fl">3.1</span>, <span class="fl">3.1</span>, <span class="fl">2.1</span>, <span class="dv">10</span>) <span class="co">#The margin are (bottom, left, top, right) respectively</span></span>
<span id="cb9-20"><a href="#cb9-20" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/affichage_raster_3-1.png" class="img-fluid figure-img" width="1152"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="change-to-the-study-area" class="level2" data-number="4.4">
<h2 data-number="4.4" class="anchored" data-anchor-id="change-to-the-study-area"><span class="header-section-number">4.4</span> Change to the study area</h2>
<section id="reprojections" class="level3" data-number="4.4.1">
<h3 data-number="4.4.1" class="anchored" data-anchor-id="reprojections"><span class="header-section-number">4.4.1</span> (Re)projections</h3>
<p>To modify the projection system of a raster, use the function <code>project()</code>. It is then necessary to indicate the method for estimating the new cell values.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/project_raster.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">Source : Centre Canadien de Télédétection</figcaption><p></p>
</figure>
</div>
<p>Four interpolation methods are available:</p>
<ul>
<li><strong><em>near</em></strong> : nearest neighbor, fast and default method for qualitative data;<br>
</li>
<li><strong><em>bilinear</em></strong> : bilinear interpolation. Default method for quantitative data;<br>
</li>
<li><strong><em>cubic</em></strong> : cubic interpolation;<br>
</li>
<li><strong><em>cubicspline</em></strong> : cubic spline interpolation.</li>
</ul>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Re-project data </span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>elevation_utm <span class="ot">=</span> <span class="fu">project</span>(<span class="at">x =</span> elevation, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"bilinear"</span>) <span class="co">#from WGS84(EPSG:4326) to UTM zone48N(EPSG:32648) </span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a>lulc_2019_utm <span class="ot">=</span> <span class="fu">project</span>(<span class="at">x =</span> lulc_2019, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"near"</span>) <span class="co">#keep original projection: UTM zone48N(EPSG:32648)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/reproj_raster_2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="crop" class="level3" data-number="4.4.2">
<h3 data-number="4.4.2" class="anchored" data-anchor-id="crop"><span class="header-section-number">4.4.2</span> Crop</h3>
<p>Clipping a raster to the extent of another object <code>SpatVector</code> or <code>SpatRaster</code> is achievable with the <code>crop()</code>.</p>
<div class="quarto-layout-panel">
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="img/crop.png" class="img-fluid"></p>
</div>
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p><img src="img/crop2.png" class="img-fluid"></p>
</div>
</div>
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="quarto-layout-cell" style="flex-basis: 50.0%;justify-content: center;">
<p>Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></p>
</div>
</div>
</div>
<p>Import vector data of (municipal divisions) using function <code>vect</code>. This data will be stored in an <code>SpatVector</code> object.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>district <span class="ot"><-</span> <span class="fu">vect</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer=</span><span class="st">"district"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Extraction of district boundaries of Thma Bang district (ADM2_PCODE : KH0907).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>thma_bang <span class="ot"><-</span> <span class="fu">subset</span>(district, district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0907"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Using the function <code>crop()</code>, Both data layers must be in the same projection.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>crop_thma_bang <span class="ot"><-</span> <span class="fu">crop</span>(elevation_utm, thma_bang)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(crop_thma_bang)</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(thma_bang, <span class="at">add=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/crop_raster_3-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="mask" class="level3" data-number="4.4.3">
<h3 data-number="4.4.3" class="anchored" data-anchor-id="mask"><span class="header-section-number">4.4.3</span> Mask</h3>
<p>To display only the values of a raster contained in a polygon, use the function <code>mask()</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/mask.png" class="img-fluid figure-img" width="350"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></figcaption><p></p>
</figure>
</div>
<p>Creation of a mask on the <strong>crop_thma_bang</strong> raster to the municipal limits (polygon) of <strong>Thma Bang district</strong>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>mask_thma_bang <span class="ot"><-</span> <span class="fu">mask</span>(crop_thma_bang, thma_bang)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(mask_thma_bang)</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(thma_bang, <span class="at">add =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/mask_raster-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="aggregation-and-disaggregation" class="level3" data-number="4.4.4">
<h3 data-number="4.4.4" class="anchored" data-anchor-id="aggregation-and-disaggregation"><span class="header-section-number">4.4.4</span> Aggregation and disaggregation</h3>
<p>Resampling a raster to a different resolution is done in two steps.</p>
<div class="quarto-layout-panel">
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/raster.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">1</figcaption><p></p>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/agr_raster.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">2</figcaption><p></p>
</figure>
</div>
</div>
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/agr_raster_2.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">3</figcaption><p></p>
</figure>
</div>
</div>
</div>
<div class="quarto-layout-row quarto-layout-valign-top">
<div class="quarto-layout-cell" style="flex-basis: 33.3%;justify-content: center;">
<p>Source : <span class="citation" data-cites="RasterCheatSheet">(<a href="references.html#ref-RasterCheatSheet" role="doc-biblioref">Racine 2016</a>)</span></p>
</div>
</div>
</div>
<p>Display the resolution of a raster with the function <code>res()</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">res</span>(elevation_utm) <span class="co">#check cell size</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 91.19475 91.19475</code></pre>
</div>
</div>
<p>Create a grid with the same extent, then decrease the spatial resolution (larger cells).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid <span class="ot"><-</span> elevation_utm</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="co"># elevation_HigherGrid <- elevation_utm</span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a><span class="fu">res</span>(elevation_LowerGrid) <span class="ot"><-</span> <span class="dv">1000</span> <span class="co">#cells size = 1000 meter</span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="co"># res(elevation_HigherGrid) <- 10 #cells size = 10 meter</span></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>class : SpatRaster
dimensions : 484, 589, 1 (nrow, ncol, nlyr)
resolution : 1000, 1000 (x, y)
extent : 203586.3, 792586.3, 1142954, 1626954 (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 48N (EPSG:32648) </code></pre>
</div>
</div>
<p>The function <code>resample()</code> allows to resample the atarting values in the new spatial resolution. Several resampling methods are available (cf. <a href="#reprojections">partie 5.4.1</a>).</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid <span class="ot"><-</span> <span class="fu">resample</span>(elevation_utm, </span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> elevation_LowerGrid, </span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> <span class="at">method =</span> <span class="st">"bilinear"</span>) </span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(elevation_LowerGrid, </span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> <span class="at">main=</span><span class="st">"Cell size = 1000m</span><span class="sc">\n</span><span class="st">Bilinear resampling method"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/agr_raster_2-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="raster-fusion" class="level3" data-number="4.4.5">
<h3 data-number="4.4.5" class="anchored" data-anchor-id="raster-fusion"><span class="header-section-number">4.4.5</span> Raster fusion</h3>
<p>Merge multiple objects <code>SpatRaster</code> into one with <code>merge()</code> or <code>mosaic()</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/mosaic.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">Source : <a href="https://desktop.arcgis.com/fr/arcmap/10.3/manage-data/raster-and-images/what-is-a-mosaic.htm" class="uri">https://desktop.arcgis.com/fr/arcmap/10.3/manage-data/raster-and-images/what-is-a-mosaic.htm</a></figcaption><p></p>
</figure>
</div>
<p>After cutting the elevation raster by the municipal boundary of Thma Bang district (cf <a href="#crop">partie 5.4.2</a>), we do the same thing for the neighboring municipality of Phnum Kravanh district.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>phnum_kravanh <span class="ot"><-</span> <span class="fu">subset</span>(district, district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH1504"</span>) <span class="co"># Extraction of the municipal boundaries of Phnum Kravanh district</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>crop_phnum_kravanh <span class="ot"><-</span> <span class="fu">crop</span>(elevation_utm, phnum_kravanh) <span class="co">#clipping the elevation raster according to district boundaries</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The <strong>crop_thma_bang</strong> and <strong>crop_phnum_kravanh</strong> elevation raster overlap spatially:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/merge_raster_1-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>The difference between the functions <code>merge()</code> and <code>mosiac()</code> relates to values of the overlapping cells. The function <code>mosaic()</code> calculate the average value while <code>merge()</code> holding the value of the object <code>SpaRaster</code> called n the function.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co">#in this example, merge() and mosaic() give the same result</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>merge_raster <span class="ot"><-</span> <span class="fu">merge</span>(crop_thma_bang, crop_phnum_kravanh) </span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>mosaic_raster <span class="ot"><-</span> <span class="fu">mosaic</span>(crop_thma_bang, crop_phnum_kravanh)</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(merge_raster)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/merge_raster_2-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="co"># plot(mosaic_raster)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="segregate" class="level3" data-number="4.4.6">
<h3 data-number="4.4.6" class="anchored" data-anchor-id="segregate"><span class="header-section-number">4.4.6</span> Segregate</h3>
<p>Decompose a raster by value (or modality) into different rasterlayers with the function <code>segregate</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>lulc_2019_class <span class="ot"><-</span> <span class="fu">segregate</span>(lulc_2019, <span class="at">keep=</span><span class="cn">TRUE</span>, <span class="at">other=</span><span class="cn">NA</span>) <span class="co">#creating a raster layer by modality</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_class)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/segregate-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="map-algebra" class="level2" data-number="4.5">
<h2 data-number="4.5" class="anchored" data-anchor-id="map-algebra"><span class="header-section-number">4.5</span> Map Algebra</h2>
<p>Map algebra is classified into four groups of operation <span class="citation" data-cites="Tomlin_1990">(<a href="references.html#ref-Tomlin_1990" role="doc-biblioref">Tomlin 1990</a>)</span>:</p>
<ul>
<li><strong><em>Local</em></strong> : operation by cell, on one or more layers;<br>
</li>
<li><strong><em>Focal</em></strong> : neighborhood operation (surrounding cells);<br>
</li>
<li><strong><em>Zonal</em></strong> : to summarize the matrix values for certain zones, usually irregular;</li>
<li><strong><em>Global</em></strong> : to summarize the matrix values of one or more matrices.</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/lo_fo_zo_glo.png" class="img-fluid figure-img"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="XingongLi2009">(<a href="references.html#ref-XingongLi2009" role="doc-biblioref">Li 2009</a>)</span></figcaption><p></p>
</figure>
</div>
<section id="local-operations" class="level3" data-number="4.5.1">
<h3 data-number="4.5.1" class="anchored" data-anchor-id="local-operations"><span class="header-section-number">4.5.1</span> Local operations</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/op_local_2.png" class="img-fluid figure-img" width="452"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p>
</figure>
</div>
<section id="value-replacement" class="level4" data-number="4.5.1.1">
<h4 data-number="4.5.1.1" class="anchored" data-anchor-id="value-replacement"><span class="header-section-number">4.5.1.1</span> Value replacement</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>elevation_utm[elevation_utm[[<span class="dv">1</span>]]<span class="sc">==</span> <span class="sc">-</span><span class="dv">9999</span>] <span class="ot"><-</span> <span class="cn">NA</span> <span class="co">#replaces -9999 values with NA</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a>elevation_utm[elevation_utm <span class="sc"><</span> <span class="dv">1500</span>] <span class="ot"><-</span> <span class="cn">NA</span> <span class="co">#Replace values < 1500 with NA</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>elevation_utm[<span class="fu">is.na</span>(elevation_utm)] <span class="ot"><-</span> <span class="dv">0</span> <span class="co">#replace NA values with 0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="operation-on-each-cell" class="level4" data-number="4.5.1.2">
<h4 data-number="4.5.1.2" class="anchored" data-anchor-id="operation-on-each-cell"><span class="header-section-number">4.5.1.2</span> Operation on each cell</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>elevation_1000 <span class="ot"><-</span> elevation_utm <span class="sc">+</span> <span class="dv">1000</span> <span class="co"># Adding 1000 to the value of each cell</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>elevation_median <span class="ot"><-</span> elevation_utm <span class="sc">-</span> <span class="fu">global</span>(elevation_utm, median)[[<span class="dv">1</span>]] <span class="co"># Removed median elevation to each cell's value</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_local_2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="reclassification" class="level4" data-number="4.5.1.3">
<h4 data-number="4.5.1.3" class="anchored" data-anchor-id="reclassification"><span class="header-section-number">4.5.1.3</span> Reclassification</h4>
<p>Reclassifying raster values can be used to discretize quantitative data as well as to categorize qualitative categories.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>reclassif <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">1</span>, </span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">2</span>,</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">4</span>, <span class="dv">6</span>, <span class="dv">3</span>,</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="dv">6</span>, <span class="dv">9</span>, <span class="dv">4</span>), </span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> <span class="at">ncol =</span> <span class="dv">3</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Values between 1 and 2 will be replaced by the value 1.<br>
Values between 3 and 4 will be replaced by the value 2.<br>
Values between 5 and 6 will be replaced by the value 3. Values between 7 and 9 will be replaced by the value 4.</p>
<p>…</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>reclassif</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3]
[1,] 1 2 1
[2,] 2 4 2
[3,] 4 6 3
[4,] 6 9 4</code></pre>
</div>
</div>
<p>The function <code>classify()</code> allows you to perform the reclassification.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>lulc_2019_reclass <span class="ot"><-</span> <span class="fu">classify</span>(lulc_2019, <span class="at">rcl =</span> reclassif)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_reclass, <span class="at">type =</span><span class="st">"classes"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="04-raster_data_files/figure-html/reclass_4-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Display with the official titles and colors of the different categories.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(lulc_2019_reclass, </span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span><span class="st">"classes"</span>, </span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> <span class="at">levels=</span><span class="fu">c</span>(<span class="st">"Urban areas"</span>,</span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Water body"</span>,</span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"Bare areas"</span>,</span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Vegetation areas"</span>),</span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a> <span class="at">col=</span><span class="fu">c</span>(<span class="st">"#E6004D"</span>,</span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"#00BFFF"</span>,</span>
<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"#D3D3D3"</span>, </span>
<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"#32CD32"</span>),</span>
<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a> <span class="at">mar=</span><span class="fu">c</span>(<span class="dv">3</span>, <span class="fl">1.5</span>, <span class="dv">1</span>, <span class="dv">11</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/reclass_6-1.png" class="img-fluid figure-img" style="width:80.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="operation-on-several-layers-ex-ndvi" class="level4" data-number="4.5.1.4">
<h4 data-number="4.5.1.4" class="anchored" data-anchor-id="operation-on-several-layers-ex-ndvi"><span class="header-section-number">4.5.1.4</span> Operation on several layers (ex: NDVI)</h4>
<p>It is possible to calculate the value of a cell from its values stored in different layers of an object <code>SpatRaster</code>.</p>
<p>Perhaps the most common example is the calculation of the <a href="http://www.trameverteetbleue.fr/outils-methodes/donnees-mobilisables/indice-vegetation-modis">Normalized Vegetation Index (<em>NDVI</em>)</a>. For each cell, a value is calculated from two layers of raster from a multispectral satellite image.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Import d'une image satellite multispectrale</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>sentinel2a <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/Sentinel2A.tif"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>This multispectral satellite image (10m resolution) dated 25/02/2020, was produced by Sentinel-2 satellite and was retrieved from <a href="https://scihub.copernicus.eu/dhus/#/home">plateforme Copernicus Open Access Hub</a>. An extraction of Red and near infrared spectral bands, centered on the Phnom Penh city, was then carried out.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(sentinel2a)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/NDVI_1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>To lighten the code, we assign the two matrix layers in different <code>SpatRaster</code> objects.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>B04_Red <span class="ot"><-</span> sentinel2a[[<span class="dv">1</span>]] <span class="co">#spectral band Red</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>B08_NIR <span class="ot"><-</span>sentinel2a[[<span class="dv">2</span>]] <span class="co">#spectral band near infrared</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>From these two raster objects , we can calculate the normalized vegetation index:</p>
<p><span class="math display">\[{NDVI}=\frac{\mathrm{NIR} - \mathrm{Red}} {\mathrm{NIR} + \mathrm{Red}}\]</span></p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>raster_NDVI <span class="ot"><-</span> (B08_NIR <span class="sc">-</span> B04_Red ) <span class="sc">/</span> (B08_NIR <span class="sc">+</span> B04_Red )</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(raster_NDVI)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/NDVI_3-1.png" class="img-fluid figure-img" style="width:90.0%"></p>
</figure>
</div>
</div>
</div>
<p>The higher the values (close to 1), the denser the vegetation.</p>
</section>
</section>
<section id="focal-operations" class="level3" data-number="4.5.2">
<h3 data-number="4.5.2" class="anchored" data-anchor-id="focal-operations"><span class="header-section-number">4.5.2</span> Focal operations</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/op_focal_2.png" class="img-fluid figure-img" width="415"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p>
</figure>
</div>
<p>Focal analysis conisders a cell plus its direct neighbors in contiguous and symmetrical (neighborhood operations). Most often, the value of the output cell is the result of a block of 3 x 3 (odd number) input cells.</p>
<p>The first step is to build a matrix that determines the block of cells that will be considered around each cell.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 5 x 5 matrix, where each cell has the same weight</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>mon_focal <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">1</span>, <span class="at">nrow =</span> <span class="dv">5</span>, <span class="at">ncol =</span> <span class="dv">5</span>)</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>mon_focal</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 1 1 1 1
[3,] 1 1 1 1 1
[4,] 1 1 1 1 1
[5,] 1 1 1 1 1</code></pre>
</div>
</div>
<p>The function <code>focal()</code> Then allows you to perform the desired analysis. For example: calculating the average of the values of all contiguous cells, for each cell in the raster.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>elevation_LowerGrid_mean <span class="ot"><-</span> <span class="fu">focal</span>(elevation_LowerGrid, </span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> <span class="at">w =</span> mon_focal, </span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="at">fun =</span> mean)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_focal_5-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<section id="focal-operations-for-elevation-rasters" class="level4" data-number="4.5.2.1">
<h4 data-number="4.5.2.1" class="anchored" data-anchor-id="focal-operations-for-elevation-rasters"><span class="header-section-number">4.5.2.1</span> Focal operations for elevation rasters</h4>
<p>The function <code>terrain()</code> allows to perform focal analyzes specific to elevation rasters. Six operations are available:</p>
<ul>
<li><strong><em>slope</em></strong> = calculation of the slope or degree of inclination of the surface;<br>
</li>
<li><strong><em>aspect</em></strong> = calculate slope orientation;<br>
</li>
<li><strong><em>roughness</em></strong> = calculate of the variability or irregularity of the elevation;<br>
</li>
<li><strong><em>TPI</em></strong> = calculation of the index of topgraphic positions;<br>
</li>
<li><strong><em>TRI</em></strong> = elevation variability index calculation;<br>
</li>
<li><strong><em>flowdir</em></strong> = calculation of the water flow direction.</li>
</ul>
<p>Example with calculation of slopes(<em>slope</em>).</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="co">#slope calculation</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a>slope <span class="ot"><-</span> <span class="fu">terrain</span>(elevation_utm, <span class="st">"slope"</span>, </span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <span class="at">neighbors =</span> <span class="dv">8</span>, <span class="co">#8 (or 4) cells around taken into account</span></span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <span class="at">unit =</span> <span class="st">"degrees"</span>) <span class="co">#Output unit</span></span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(slope) <span class="co">#Inclination of the slopes, in degrees</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_focal_6-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="global-operations" class="level3" data-number="4.5.3">
<h3 data-number="4.5.3" class="anchored" data-anchor-id="global-operations"><span class="header-section-number">4.5.3</span> Global operations</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/op_global.png" class="img-fluid figure-img" width="297"></p>
<p></p><figcaption class="figure-caption">Source : <a href="https://gisgeography.com/map-algebra-global-zonal-focal-local" class="uri">https://gisgeography.com/map-algebra-global-zonal-focal-local</a></figcaption><p></p>
</figure>
</div>
<p>Global operation are used to summarize the matrix values of one or more matrices.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(elevation_utm, <span class="at">fun =</span> <span class="st">"mean"</span>) <span class="co">#average values</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> mean
Altitude 80.01082</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(elevation_utm, <span class="at">fun =</span> <span class="st">"sd"</span>) <span class="co">#standard deviation</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> sd
Altitude 155.885</code></pre>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">freq</span>(lulc_2019_reclass) <span class="co">#frequency</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> layer value count
1 1 1 47485325
2 1 2 13656289
3 1 3 14880961
4 1 4 37194979</code></pre>
</div>
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">table</span>(lulc_2019_reclass[]) <span class="co">#contingency table</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
1 2 3 4
47485325 13656289 14880961 37194979 </code></pre>
</div>
</div>
<p>Statistical representations that summarize matrix information.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hist</span>(elevation_utm) <span class="co">#histogram</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: [hist] a sample of3% of the cells was used</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_global_4-1.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">density</span>(elevation_utm) <span class="co">#density</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_global_4-2.png" class="img-fluid figure-img" width="576"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="zonal-operation" class="level3" data-number="4.5.4">
<h3 data-number="4.5.4" class="anchored" data-anchor-id="zonal-operation"><span class="header-section-number">4.5.4</span> Zonal operation</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="img/op_zonal_2.png" class="img-fluid figure-img" width="342"></p>
<p></p><figcaption class="figure-caption">Source : <span class="citation" data-cites="JMennis2015">(<a href="references.html#ref-JMennis2015" role="doc-biblioref">Mennis 2015</a>)</span></figcaption><p></p>
</figure>
</div>
<p>The zonal operation make it possible to summarize the matrix values of certain zones (group of contiguous cells in space or in value).</p>
<section id="zonal-operation-on-an-extraction" class="level4" data-number="4.5.4.1">
<h4 data-number="4.5.4.1" class="anchored" data-anchor-id="zonal-operation-on-an-extraction"><span class="header-section-number">4.5.4.1</span> Zonal operation on an extraction</h4>
<p><strong>All global operations can be performed on an extraction of cells resulting from the functions <code>crop()</code>, <code>mask()</code>, <code>segregate()</code>…</strong></p>
<p>Example: average elevation for the city of Thma Bang district (cf <a href="#mask">partie 5.4.3</a>).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Average value of the "mask" raster over Thma Bang district</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="fu">global</span>(mask_thma_bang, <span class="at">fun =</span> <span class="st">"mean"</span>, <span class="at">na.rm=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> mean
Altitude 584.7703</code></pre>
</div>
</div>
</section>
<section id="zonal-operation-from-a-vector-layer" class="level4" data-number="4.5.4.2">
<h4 data-number="4.5.4.2" class="anchored" data-anchor-id="zonal-operation-from-a-vector-layer"><span class="header-section-number">4.5.4.2</span> Zonal operation from a vector layer</h4>
<p>The function <code>extract()</code> allows you to extract and manipulate the values of cells that intersect vector data.</p>
<p>Example from polygons:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Average elevation for each polygon (district)?</span></span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>elevation_by_dist <span class="ot"><-</span> <span class="fu">extract</span>(elevation_LowerGrid, district, <span class="at">fun=</span>mean)</span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(elevation_by_dist, <span class="dv">10</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> ID Altitude
1 1 8.953352
2 2 196.422240
3 3 23.453937
4 4 3.973118
5 5 29.545801
6 6 41.579593
7 7 50.162749
8 8 85.128777
9 9 269.068091
10 10 8.439041</code></pre>
</div>
</div>
</section>
<section id="zonal-operation-from-raster" class="level4" data-number="4.5.4.3">
<h4 data-number="4.5.4.3" class="anchored" data-anchor-id="zonal-operation-from-raster"><span class="header-section-number">4.5.4.3</span> Zonal operation from raster</h4>
<p>Zonal operation can be performed by area bounded by the categorical values of a second raster. For this, the two raster must have exaclty the same extent and the same resolution.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="co">#create a second raster with same resolution and extent as "elevation_clip"</span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>elevation_clip <span class="ot"><-</span> <span class="fu">rast</span>(<span class="st">"data_cambodia/elevation_clip.tif"</span>)</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a>elevation_clip_utm <span class="ot"><-</span> <span class="fu">project</span>(<span class="at">x =</span> elevation_clip, <span class="at">y =</span> <span class="st">"EPSG:32648"</span>, <span class="at">method =</span> <span class="st">"bilinear"</span>)</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a>second_raster_CLC <span class="ot"><-</span> <span class="fu">rast</span>(elevation_clip_utm)</span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a><span class="co">#resampling of lulc_2019_reclass </span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a>second_raster_CLC <span class="ot"><-</span> <span class="fu">resample</span>(lulc_2019_reclass, second_raster_CLC, <span class="at">method =</span> <span class="st">"near"</span>) </span>
<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a><span class="co">#added a variable name for the second raster</span></span>
<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a><span class="fu">names</span>(second_raster_CLC) <span class="ot"><-</span> <span class="st">"lulc_2019_reclass_resample"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="04-raster_data_files/figure-html/op_zonal_2-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p>Calculation of the average elevation for the different areas of the second raster.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co">#average elevation for each area of the "second_raster"</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">zonal</span>(elevation_clip_utm, second_raster_CLC , <span class="st">"mean"</span>, <span class="at">na.rm=</span><span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> lulc_2019_reclass_resample elevation_clip
1 1 12.83846
2 2 8.31809
3 3 11.41178
4 4 11.93546</code></pre>
</div>
</div>
</section>
</section>
</section>
<section id="transformation-and-conversion" class="level2" data-number="4.6">