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
<!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 - 3 Using vector 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 */
</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="./04-raster_data.html" rel="next">
<link href="./02-data_acquisition.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>
<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">3</span> <span class="chapter-title">Using vector 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 active"><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"><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>
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
</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="#importing-and-exporting-data" id="toc-importing-and-exporting-data" class="nav-link active" data-scroll-target="#importing-and-exporting-data"><span class="toc-section-number">3.1</span> Importing and exporting data</a></li>
<li><a href="#display" id="toc-display" class="nav-link" data-scroll-target="#display"><span class="toc-section-number">3.2</span> Display</a></li>
<li><a href="#coordinate-systems" id="toc-coordinate-systems" class="nav-link" data-scroll-target="#coordinate-systems"><span class="toc-section-number">3.3</span> Coordinate systems</a>
<ul>
<li><a href="#look-up-the-coordinate-system-of-an-object" id="toc-look-up-the-coordinate-system-of-an-object" class="nav-link" data-scroll-target="#look-up-the-coordinate-system-of-an-object"><span class="toc-section-number">3.3.1</span> Look up the coordinate system of an object</a></li>
<li><a href="#changing-the-coordinate-system-of-an-object" id="toc-changing-the-coordinate-system-of-an-object" class="nav-link" data-scroll-target="#changing-the-coordinate-system-of-an-object"><span class="toc-section-number">3.3.2</span> Changing the coordinate system of an object</a></li>
</ul></li>
<li><a href="#selection-by-attributes" id="toc-selection-by-attributes" class="nav-link" data-scroll-target="#selection-by-attributes"><span class="toc-section-number">3.4</span> Selection by attributes</a></li>
<li><a href="#spatial-selection" id="toc-spatial-selection" class="nav-link" data-scroll-target="#spatial-selection"><span class="toc-section-number">3.5</span> Spatial selection</a>
<ul>
<li><a href="#intersections" id="toc-intersections" class="nav-link" data-scroll-target="#intersections"><span class="toc-section-number">3.5.1</span> Intersections</a>
<ul class="collapse">
<li><a href="#difference-between-sparse-true-and-sparse-false" id="toc-difference-between-sparse-true-and-sparse-false" class="nav-link" data-scroll-target="#difference-between-sparse-true-and-sparse-false"><span class="toc-section-number">3.5.1.1</span> Difference between <code>sparse = TRUE</code> and <code>sparse = FALSE</code></a></li>
</ul></li>
<li><a href="#contains-within" id="toc-contains-within" class="nav-link" data-scroll-target="#contains-within"><span class="toc-section-number">3.5.2</span> Contains / Within</a></li>
</ul></li>
<li><a href="#operation-of-geometries" id="toc-operation-of-geometries" class="nav-link" data-scroll-target="#operation-of-geometries"><span class="toc-section-number">3.6</span> Operation of geometries</a>
<ul>
<li><a href="#extract-centroids" id="toc-extract-centroids" class="nav-link" data-scroll-target="#extract-centroids"><span class="toc-section-number">3.6.1</span> Extract centroids</a></li>
<li><a href="#aggregate-polygons" id="toc-aggregate-polygons" class="nav-link" data-scroll-target="#aggregate-polygons"><span class="toc-section-number">3.6.2</span> Aggregate polygons</a></li>
<li><a href="#aggregate-polygons-based-on-a-variable" id="toc-aggregate-polygons-based-on-a-variable" class="nav-link" data-scroll-target="#aggregate-polygons-based-on-a-variable"><span class="toc-section-number">3.6.3</span> Aggregate polygons based on a variable</a></li>
<li><a href="#create-a-buffer-zone" id="toc-create-a-buffer-zone" class="nav-link" data-scroll-target="#create-a-buffer-zone"><span class="toc-section-number">3.6.4</span> Create a buffer zone</a></li>
<li><a href="#making-an-intersection" id="toc-making-an-intersection" class="nav-link" data-scroll-target="#making-an-intersection"><span class="toc-section-number">3.6.5</span> Making an intersection</a></li>
<li><a href="#create-regular-grid" id="toc-create-regular-grid" class="nav-link" data-scroll-target="#create-regular-grid"><span class="toc-section-number">3.6.6</span> Create regular grid</a></li>
<li><a href="#counting-points-in-a-polygon-in-a-grid-tile" id="toc-counting-points-in-a-polygon-in-a-grid-tile" class="nav-link" data-scroll-target="#counting-points-in-a-polygon-in-a-grid-tile"><span class="toc-section-number">3.6.7</span> Counting points in a polygon (in a grid tile)</a></li>
<li><a href="#aggregate-point-values-into-polygons" id="toc-aggregate-point-values-into-polygons" class="nav-link" data-scroll-target="#aggregate-point-values-into-polygons"><span class="toc-section-number">3.6.8</span> Aggregate point values into polygons</a></li>
</ul></li>
<li><a href="#measurements" id="toc-measurements" class="nav-link" data-scroll-target="#measurements"><span class="toc-section-number">3.7</span> Measurements</a>
<ul>
<li><a href="#create-a-distance-matrix" id="toc-create-a-distance-matrix" class="nav-link" data-scroll-target="#create-a-distance-matrix"><span class="toc-section-number">3.7.1</span> Create a distance matrix</a></li>
<li><a href="#calculate-routes" id="toc-calculate-routes" class="nav-link" data-scroll-target="#calculate-routes"><span class="toc-section-number">3.7.2</span> Calculate routes</a>
<ul class="collapse">
<li><a href="#calculate-a-route" id="toc-calculate-a-route" class="nav-link" data-scroll-target="#calculate-a-route"><span class="toc-section-number">3.7.2.1</span> Calculate a route</a></li>
<li><a href="#calculation-of-a-time-matrix" id="toc-calculation-of-a-time-matrix" class="nav-link" data-scroll-target="#calculation-of-a-time-matrix"><span class="toc-section-number">3.7.2.2</span> Calculation of a time matrix</a></li>
</ul></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">3</span> <span class="chapter-title">Using vector data</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="importing-and-exporting-data" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="importing-and-exporting-data"><span class="header-section-number">3.1</span> Importing and exporting data</h2>
<p>The <code>st_read()</code> and <code>st_write()</code> function are used to import and export many types of files. The following lines import the administrative data in district level layer located in the <strong>district.shp</strong> shapefile file.</p>
<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>(sf)</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>Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.1; sf_use_s2() is TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>district <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/district.shp"</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 class="code-out"><code>Reading layer `district' from data source
`/home/lucas/Documents/ForgeIRD/rspatial-for-onehealth/data_cambodia/district.shp'
using driver `ESRI Shapefile'
Simple feature collection with 197 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 211534.7 ymin: 1149105 xmax: 784612.1 ymax: 1625495
Projected CRS: WGS 84 / UTM zone 48N</code></pre>
</div>
</div>
<div class="callout-important callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Shapefile format limitations
</div>
</div>
<div class="callout-body-container callout-body">
<p>For the multiple limitations of this format (multi-file, limited number of records…) we advise you to prefer another format such as the geopackage <code>*.gpkg</code>. All the good reasons not to use the shapefile are <a href="http://switchfromshapefile.org/">here</a>.</p>
</div>
</div>
<p>A geopackage is a database, to load a layer, you must know its name</p>
<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">st_layers</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<pre class="code-out"><code>Driver: GPKG
Available layers:
layer_name geometry_type features fields crs_name
1 country Multi Polygon 1 10 WGS 84 / UTM zone 48N
2 district Multi Polygon 197 10 WGS 84 / UTM zone 48N
3 education Multi Polygon 25 19 WGS 84 / UTM zone 48N
4 hospital Point 956 13 WGS 84 / UTM zone 48N
5 cases Multi Point 972 2 WGS 84 / UTM zone 48N
6 road Multi Line String 6 9 WGS 84 / UTM zone 48N</code></pre>
</div>
</div>
<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>road <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"road"</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 class="code-out"><code>Reading layer `road' from data source
`/home/lucas/Documents/ForgeIRD/rspatial-for-onehealth/data_cambodia/cambodia.gpkg'
using driver `GPKG'
Simple feature collection with 6 features and 9 fields
Geometry type: MULTILINESTRING
Dimension: XY
Bounding box: xmin: 212377 ymin: 1152214 xmax: 784654.7 ymax: 1625281
Projected CRS: WGS 84 / UTM zone 48N</code></pre>
</div>
<div class="cell">
<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><span class="fu">library</span>(sf)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>district <span class="ot">=</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"district"</span>) <span class="co">#import district data</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<pre class="code-out"><code>Reading layer `district' from data source
`/home/lucas/Documents/ForgeIRD/rspatial-for-onehealth/data_cambodia/cambodia.gpkg'
using driver `GPKG'
Simple feature collection with 197 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 211534.7 ymin: 1149105 xmax: 784612.1 ymax: 1625495
Projected CRS: WGS 84 / UTM zone 48N</code></pre>
<p>The following lines export the <strong>district object</strong> to a <strong>data</strong> folder in geopackage and shapefile format.</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><span class="fu">st_write</span>(<span class="at">obj =</span> district, <span class="at">dsn =</span> <span class="st">"data_cambodia/district.gpkg"</span>, <span class="at">delete_layer =</span> <span class="cn">TRUE</span>)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">st_write</span>(<span class="at">obj =</span> district, <span class="st">"data_cambodia/district.shp"</span>, <span class="at">layer_options =</span> <span class="st">"ENCODING=UTF-8"</span>, <span class="at">delete_layer =</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>
</section>
<section id="display" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="display"><span class="header-section-number">3.2</span> Display</h2>
<p><strong>Preview of the variables</strong> via the function <code>head()</code> and <code>plot()</code>.</p>
<div class="cell" data-nm="true">
<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><span class="fu">head</span>(district)</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 class="code-out"><code>Simple feature collection with 6 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 300266.9 ymin: 1180566 xmax: 767313.9 ymax: 1563861
Projected CRS: WGS 84 / UTM zone 48N
ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP Area.Km2.
1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416 1067.8638
2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708 837.7064
3 Angk Snuol KH0808 Kandal KH08 45436 47141 92577 183.9050
4 Angkor Borei KH2101 Takeo KH21 26306 27168 53474 301.0502
5 Angkor Chey KH0701 Kampot KH07 42448 44865 87313 316.7576
6 Angkor Chum KH1701 Siemreap KH17 34269 34576 68845 478.6988
Status DENs geom
1 <4500km2 79.98773 MULTIPOLYGON (((306568.1 14...
2 <4500km2 17.55747 MULTIPOLYGON (((751459.2 15...
3 <4500km2 503.39580 MULTIPOLYGON (((471954.3 12...
4 <4500km2 177.62485 MULTIPOLYGON (((490048.2 12...
5 <4500km2 275.64610 MULTIPOLYGON (((462702.2 12...
6 <4500km2 143.81696 MULTIPOLYGON (((363642.5 15...</code></pre>
</div>
<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><span class="fu">plot</span>(district)</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="03-vector_data_files/figure-html/aff1-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p><strong>for Geometry display</strong> only.</p>
<div class="cell" data-nm="true">
<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">plot</span>(<span class="fu">st_geometry</span>(district))</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="03-vector_data_files/figure-html/aff2-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="coordinate-systems" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="coordinate-systems"><span class="header-section-number">3.3</span> Coordinate systems</h2>
<section id="look-up-the-coordinate-system-of-an-object" class="level3" data-number="3.3.1">
<h3 data-number="3.3.1" class="anchored" data-anchor-id="look-up-the-coordinate-system-of-an-object"><span class="header-section-number">3.3.1</span> Look up the coordinate system of an object</h3>
<p>The function <code>st_crs()</code> makes it possible to consult the system of coordinates used and object sf.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">st_crs</span>(district)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Coordinate Reference System:
User input: WGS 84 / UTM zone 48N
wkt:
PROJCRS["WGS 84 / UTM zone 48N",
BASEGEOGCRS["WGS 84",
ENSEMBLE["World Geodetic System 1984 ensemble",
MEMBER["World Geodetic System 1984 (Transit)"],
MEMBER["World Geodetic System 1984 (G730)"],
MEMBER["World Geodetic System 1984 (G873)"],
MEMBER["World Geodetic System 1984 (G1150)"],
MEMBER["World Geodetic System 1984 (G1674)"],
MEMBER["World Geodetic System 1984 (G1762)"],
MEMBER["World Geodetic System 1984 (G2139)"],
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]],
ENSEMBLEACCURACY[2.0]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]],
CONVERSION["UTM zone 48N",
METHOD["Transverse Mercator",
ID["EPSG",9807]],
PARAMETER["Latitude of natural origin",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8801]],
PARAMETER["Longitude of natural origin",105,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["Scale factor at natural origin",0.9996,
SCALEUNIT["unity",1],
ID["EPSG",8805]],
PARAMETER["False easting",500000,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",0,
LENGTHUNIT["metre",1],
ID["EPSG",8807]]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1]],
USAGE[
SCOPE["Engineering survey, topographic mapping."],
AREA["Between 102°E and 108°E, northern hemisphere between equator and 84°N, onshore and offshore. Cambodia. China. Indonesia. Laos. Malaysia - West Malaysia. Mongolia. Russian Federation. Singapore. Thailand. Vietnam."],
BBOX[0,102,84,108]],
ID["EPSG",32648]]</code></pre>
</div>
</div>
</section>
<section id="changing-the-coordinate-system-of-an-object" class="level3" data-number="3.3.2">
<h3 data-number="3.3.2" class="anchored" data-anchor-id="changing-the-coordinate-system-of-an-object"><span class="header-section-number">3.3.2</span> Changing the coordinate system of an object</h3>
<p>The function <code>st_transform()</code> allows to change the coordinate system of an sf object, to re-project it.</p>
<div class="cell" data-sm="true">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="fu">title</span>(<span class="st">"WGS 84 / UTM zone 48N"</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="03-vector_data_files/figure-html/proj2-1.png" class="img-fluid" width="672"></p>
</div>
<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>dist_reproj <span class="ot"><-</span> <span class="fu">st_transform</span>(district, <span class="st">"epsg:4326"</span>)</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dist_reproj))</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">title</span>(<span class="st">"WGS84"</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="03-vector_data_files/figure-html/proj2-2.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>The <a href="http://spatialreference.org/" target="_blank">Spatial Reference</a> site provides reference for a large number of coordinate systems.</p>
</section>
</section>
<section id="selection-by-attributes" class="level2" data-number="3.4">
<h2 data-number="3.4" class="anchored" data-anchor-id="selection-by-attributes"><span class="header-section-number">3.4</span> Selection by attributes</h2>
<p>The object <code>sf</code> <strong>are</strong> <code>data.frame</code>, so you can select their rows and columns in the same way as <code>data.frame</code>.</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><span class="co"># row Selection</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>district[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</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 class="code-out"><code>Simple feature collection with 2 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 300266.9 ymin: 1449408 xmax: 767313.9 ymax: 1563861
Projected CRS: WGS 84 / UTM zone 48N
ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP Area.Km2.
1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416 1067.8638
2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708 837.7064
Status DENs geom
1 <4500km2 79.98773 MULTIPOLYGON (((306568.1 14...
2 <4500km2 17.55747 MULTIPOLYGON (((751459.2 15...</code></pre>
</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>district[district<span class="sc">$</span>ADM1_EN <span class="sc">==</span> <span class="st">"Phnom Penh"</span>, ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Simple feature collection with 12 features and 10 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 468677.5 ymin: 1262590 xmax: 505351.9 ymax: 1297419
Projected CRS: WGS 84 / UTM zone 48N
First 10 features:
ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP
29 Chamkar Mon KH1201 Phnom Penh KH12 52278 54478 106756
31 Chbar Ampov KH1212 Phnom Penh KH12 64816 68243 133059
43 Chraoy Chongvar KH1210 Phnom Penh KH12 30920 31087 62007
48 Dangkao KH1205 Phnom Penh KH12 46999 48525 95524
50 Doun Penh KH1202 Phnom Penh KH12 33844 36471 70315
93 Mean Chey KH1206 Phnom Penh KH12 68381 70366 138747
117 Praek Pnov KH1211 Phnom Penh KH12 27566 27698 55264
118 Prampir Meakkakra KH1203 Phnom Penh KH12 31091 33687 64778
133 Pur SenChey KH1209 Phnom Penh KH12 95050 109297 204347
141 Russey Keo KH1207 Phnom Penh KH12 67357 68419 135776
Area.Km2. Status DENs geom
29 11.049600 <4500km2 9661.5265 MULTIPOLYGON (((494709.4 12...
31 86.780498 <4500km2 1533.2823 MULTIPOLYGON (((498855.3 12...
43 85.609156 <4500km2 724.3034 MULTIPOLYGON (((491161.3 12...
48 113.774833 <4500km2 839.5881 MULTIPOLYGON (((489191.1 12...
50 7.734808 <4500km2 9090.7234 MULTIPOLYGON (((492447.1 12...
93 28.998026 <4500km2 4784.7051 MULTIPOLYGON (((491068.2 12...
117 115.384300 <4500km2 478.9560 MULTIPOLYGON (((481483.3 12...
118 2.224892 <4500km2 29115.1253 MULTIPOLYGON (((491067.6 12...
133 148.357984 <4500km2 1377.3913 MULTIPOLYGON (((479078.8 12...
141 23.381517 <4500km2 5806.9800 MULTIPOLYGON (((490264.8 12...</code></pre>
</div>
<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><span class="co"># column selection</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>district[district<span class="sc">$</span>ADM1_EN <span class="sc">==</span> <span class="st">"Phnom Penh"</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>] </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Simple feature collection with 12 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 468677.5 ymin: 1262590 xmax: 505351.9 ymax: 1297419
Projected CRS: WGS 84 / UTM zone 48N
First 10 features:
ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE
29 Chamkar Mon KH1201 Phnom Penh KH12
31 Chbar Ampov KH1212 Phnom Penh KH12
43 Chraoy Chongvar KH1210 Phnom Penh KH12
48 Dangkao KH1205 Phnom Penh KH12
50 Doun Penh KH1202 Phnom Penh KH12
93 Mean Chey KH1206 Phnom Penh KH12
117 Praek Pnov KH1211 Phnom Penh KH12
118 Prampir Meakkakra KH1203 Phnom Penh KH12
133 Pur SenChey KH1209 Phnom Penh KH12
141 Russey Keo KH1207 Phnom Penh KH12
geom
29 MULTIPOLYGON (((494709.4 12...
31 MULTIPOLYGON (((498855.3 12...
43 MULTIPOLYGON (((491161.3 12...
48 MULTIPOLYGON (((489191.1 12...
50 MULTIPOLYGON (((492447.1 12...
93 MULTIPOLYGON (((491068.2 12...
117 MULTIPOLYGON (((481483.3 12...
118 MULTIPOLYGON (((491067.6 12...
133 MULTIPOLYGON (((479078.8 12...
141 MULTIPOLYGON (((490264.8 12...</code></pre>
</div>
</div>
</section>
<section id="spatial-selection" class="level2" data-number="3.5">
<h2 data-number="3.5" class="anchored" data-anchor-id="spatial-selection"><span class="header-section-number">3.5</span> Spatial selection</h2>
<section id="intersections" class="level3" data-number="3.5.1">
<h3 data-number="3.5.1" class="anchored" data-anchor-id="intersections"><span class="header-section-number">3.5.1</span> Intersections</h3>
<p>Selection of roads that are intersecting <strong>dangkao</strong> district</p>
<div class="cell" data-nm="true">
<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>road <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"road"</span>, <span class="at">quiet =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span> <span class="fu">st_cast</span>(<span class="st">"LINESTRING"</span>)</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>dangkao <span class="ot"><-</span> district[district<span class="sc">$</span>ADM2_EN <span class="sc">==</span> <span class="st">"Dangkao"</span>, ]</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>inter <span class="ot"><-</span> <span class="fu">st_intersects</span>(<span class="at">x =</span> road, <span class="at">y =</span> dangkao, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(inter)</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 class="code-out"><code> [,1]
[1,] FALSE
[2,] FALSE
[3,] FALSE
[4,] FALSE
[5,] FALSE
[6,] FALSE</code></pre>
</div>
<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><span class="fu">dim</span>(inter)</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 class="code-out"><code>[1] 108285 1</code></pre>
</div>
</div>
<p>The <strong>inter</strong> object is a matrix which indicates for each of element of the <strong>road</strong> object (6 elements) whether it intersects each elements the <strong>dangkao</strong> object (1 element). The dimension of the matrix is therefore indeed 6 rows * 1 column. Note the use of the parameter <code>sparse = FALSE</code> here. It is then possible to create a column from this object:</p>
<div class="cell" data-nm="true">
<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>road<span class="sc">$</span>intersect_dangkao <span class="ot"><-</span> inter</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dangkao), <span class="at">col =</span> <span class="st">"lightblue"</span>)</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(road), <span class="at">add =</span> <span class="cn">TRUE</span>)</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(road[road<span class="sc">$</span>intersect_dangkao, ]),</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> <span class="at">col =</span> <span class="st">"tomato"</span>, <span class="at">lwd =</span> <span class="fl">1.5</span>, <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">
<p><img src="03-vector_data_files/figure-html/intersects2-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<section id="difference-between-sparse-true-and-sparse-false" class="level4" data-number="3.5.1.1">
<h4 data-number="3.5.1.1" class="anchored" data-anchor-id="difference-between-sparse-true-and-sparse-false"><span class="header-section-number">3.5.1.1</span> Difference between <code>sparse = TRUE</code> and <code>sparse = FALSE</code></h4>
<div class="cell">
<div class="cell-output-display">
<p><img src="03-vector_data_files/figure-html/st_intersparse-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<ul>
<li><code>sparse = TRUE</code></li>
</ul>
<div class="cell">
<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>inter <span class="ot"><-</span> <span class="fu">st_intersects</span>(<span class="at">x =</span> grid, <span class="at">y =</span> pt, <span class="at">sparse =</span> <span class="cn">TRUE</span>)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>inter</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 class="code-out"><code>Sparse geometry binary predicate list of length 4, where the predicate
was `intersects'
1: (empty)
2: 6, 7
3: 1, 4
4: 2, 3, 5, 8</code></pre>
</div>
</div>
<ul>
<li><code>sparse = FALSE</code></li>
</ul>
<div class="cell">
<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>inter <span class="ot"><-</span> <span class="fu">st_intersects</span>(<span class="at">x =</span> grid, <span class="at">y =</span> pt, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="fu">rownames</span>(inter) <span class="ot"><-</span> grid<span class="sc">$</span>id</span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(inter) <span class="ot"><-</span> pt<span class="sc">$</span>id</span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a>inter</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 class="code-out"><code> a b c d e f g h
1 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
2 FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
3 TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
4 FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE</code></pre>
</div>
</div>
</section>
</section>
<section id="contains-within" class="level3" data-number="3.5.2">
<h3 data-number="3.5.2" class="anchored" data-anchor-id="contains-within"><span class="header-section-number">3.5.2</span> Contains / Within</h3>
<p>Selection of roads contained in the municipality of <em>Dangkao</em>. The function <code>st_within()</code> works like the function <code>st_intersects()</code></p>
<div class="cell" data-nm="true">
<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>road<span class="sc">$</span>within_dangkao <span class="ot"><-</span> <span class="fu">st_within</span>(road, dangkao, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dangkao), <span class="at">col =</span> <span class="st">"lightblue"</span>)</span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(road), <span class="at">add =</span> <span class="cn">TRUE</span>)</span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(road[road<span class="sc">$</span>within_dangkao, ]), <span class="at">col =</span> <span class="st">"tomato"</span>,</span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a> <span class="at">lwd =</span> <span class="dv">2</span>, <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">
<p><img src="03-vector_data_files/figure-html/within-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</section>
<section id="operation-of-geometries" class="level2" data-number="3.6">
<h2 data-number="3.6" class="anchored" data-anchor-id="operation-of-geometries"><span class="header-section-number">3.6</span> Operation of geometries</h2>
<section id="extract-centroids" class="level3" data-number="3.6.1">
<h3 data-number="3.6.1" class="anchored" data-anchor-id="extract-centroids"><span class="header-section-number">3.6.1</span> Extract centroids</h3>
<div class="cell" data-nm="true">
<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>dist_c <span class="ot"><-</span> <span class="fu">st_centroid</span>(district)</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dist_c), <span class="at">add =</span> <span class="cn">TRUE</span>, <span class="at">cex =</span> <span class="fl">1.2</span>, <span class="at">col =</span> <span class="st">"red"</span>, <span class="at">pch =</span> <span class="dv">20</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="03-vector_data_files/figure-html/centroid-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="aggregate-polygons" class="level3" data-number="3.6.2">
<h3 data-number="3.6.2" class="anchored" data-anchor-id="aggregate-polygons"><span class="header-section-number">3.6.2</span> Aggregate polygons</h3>
<div class="cell" data-nm="true">
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>cambodia_dist <span class="ot"><-</span> <span class="fu">st_union</span>(district) </span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district), <span class="at">col =</span> <span class="st">"lightblue"</span>)</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(cambodia_dist), <span class="at">add =</span> <span class="cn">TRUE</span>, <span class="at">lwd =</span> <span class="dv">2</span>, <span class="at">border =</span> <span class="st">"red"</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="03-vector_data_files/figure-html/aggreg-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="aggregate-polygons-based-on-a-variable" class="level3" data-number="3.6.3">
<h3 data-number="3.6.3" class="anchored" data-anchor-id="aggregate-polygons-based-on-a-variable"><span class="header-section-number">3.6.3</span> Aggregate polygons based on a variable</h3>
<div class="cell" data-nm="true">
<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>dist_union <span class="ot"><-</span> <span class="fu">aggregate</span>(<span class="at">x =</span> district[,<span class="fu">c</span>(<span class="st">"T_POP"</span>)],</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="fu">list</span>(<span class="at">STATUT =</span> district<span class="sc">$</span>Status),</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="at">FUN =</span> <span class="st">"sum"</span>)</span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(dist_union)</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="03-vector_data_files/figure-html/aggreg2-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="create-a-buffer-zone" class="level3" data-number="3.6.4">
<h3 data-number="3.6.4" class="anchored" data-anchor-id="create-a-buffer-zone"><span class="header-section-number">3.6.4</span> Create a buffer zone</h3>
<div class="cell" data-nm="true">
<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>dangkao_buffer <span class="ot"><-</span> <span class="fu">st_buffer</span>(<span class="at">x =</span> dangkao, <span class="at">dist =</span> <span class="dv">1000</span>)</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dangkao_buffer), <span class="at">col =</span> <span class="st">"#E8DAEF"</span>, <span class="at">lwd=</span><span class="dv">2</span>, <span class="at">border =</span> <span class="st">"#6C3483"</span>)</span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dangkao), <span class="at">add =</span> <span class="cn">TRUE</span>, <span class="at">lwd =</span> <span class="dv">2</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="03-vector_data_files/figure-html/buffers-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="making-an-intersection" class="level3" data-number="3.6.5">
<h3 data-number="3.6.5" class="anchored" data-anchor-id="making-an-intersection"><span class="header-section-number">3.6.5</span> Making an intersection</h3>
<p>By using the function <code>st_intersection()</code> we will cut one layer by another.</p>
<div class="cell" data-nm="true">
<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">library</span>(magrittr)</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="co"># creation of a buffer zone around the centroid of the municipality of Dangkao district</span></span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a><span class="co"># using the pipe</span></span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a>zone <span class="ot"><-</span> <span class="fu">st_geometry</span>(dangkao) <span class="sc">%>%</span></span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">st_centroid</span>() <span class="sc">%>%</span></span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">st_buffer</span>(<span class="dv">30000</span>)</span>
<span id="cb40-7"><a href="#cb40-7" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb40-8"><a href="#cb40-8" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(zone, <span class="at">border =</span> <span class="st">"#F06292"</span>, <span class="at">lwd =</span> <span class="dv">2</span>, <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">
<p><img src="03-vector_data_files/figure-html/intersect2-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>dist_z <span class="ot"><-</span> <span class="fu">st_intersection</span>(<span class="at">x =</span> district, <span class="at">y =</span> zone)</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dist_z), <span class="at">col=</span><span class="st">"#AF7AC5"</span>, <span class="at">border=</span><span class="st">"#F9E79F"</span>, <span class="at">add=</span>T)</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="03-vector_data_files/figure-html/intersect2-2.png" class="img-fluid" width="672"></p>
</div>
<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">plot</span>(<span class="fu">st_geometry</span>(dist_z))</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="03-vector_data_files/figure-html/intersect2-3.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="create-regular-grid" class="level3" data-number="3.6.6">
<h3 data-number="3.6.6" class="anchored" data-anchor-id="create-regular-grid"><span class="header-section-number">3.6.6</span> Create regular grid</h3>
<p>The function <code>st_make_grid()</code> allows you to create regular grid. The function produce and object <code>sfc</code>, you must then use the function <code>st_sf()</code> to transform the object <code>sfc</code> into and object <code>sf</code>. During this transformation we add here a column of unique identifiers.</p>
<div class="cell" data-nm="true">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>grid <span class="ot"><-</span> <span class="fu">st_make_grid</span>(<span class="at">x =</span> district, <span class="at">cellsize =</span> <span class="dv">10000</span>)</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a>grid <span class="ot"><-</span> <span class="fu">st_sf</span>(<span class="at">ID =</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(grid), <span class="at">geom =</span> grid)</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(grid), <span class="at">col =</span> <span class="st">"grey"</span>, <span class="at">border =</span> <span class="st">"white"</span>)</span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district), <span class="at">border =</span> <span class="st">"grey50"</span>, <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">
<p><img src="03-vector_data_files/figure-html/grid-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="counting-points-in-a-polygon-in-a-grid-tile" class="level3" data-number="3.6.7">
<h3 data-number="3.6.7" class="anchored" data-anchor-id="counting-points-in-a-polygon-in-a-grid-tile"><span class="header-section-number">3.6.7</span> Counting points in a polygon (in a grid tile)</h3>
<div class="cell" data-nm="true">
<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="co"># selection of grid tiles that intersect the district</span></span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a>inter <span class="ot"><-</span> <span class="fu">st_intersects</span>(grid, cambodia_dist, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a>grid <span class="ot"><-</span> grid[inter, ]</span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a>case_cambodia <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>, <span class="at">layer =</span> <span class="st">"cases"</span> , <span class="at">quiet =</span> <span class="cn">TRUE</span>)</span>
<span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(grid), <span class="at">col =</span> <span class="st">"grey"</span>, <span class="at">border =</span> <span class="st">"white"</span>)</span>
<span id="cb44-8"><a href="#cb44-8" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(case_cambodia), <span class="at">pch =</span> <span class="dv">20</span>, <span class="at">col =</span> <span class="st">"red"</span>, <span class="at">add =</span> <span class="cn">TRUE</span>, <span class="at">cex =</span> <span class="fl">0.8</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="03-vector_data_files/figure-html/intersect3.1-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>inter <span class="ot"><-</span> <span class="fu">st_intersects</span>(grid, case_cambodia, <span class="at">sparse =</span> <span class="cn">TRUE</span>)</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(inter)</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 class="code-out"><code>[1] 1964</code></pre>
</div>
</div>
<p>Here we use the argument <code>sparse = TRUE</code>. The <strong>inter</strong> object is a list the length of the <strong>grid</strong> and each item in the list contain the index of the object items of <strong>cases</strong> and <strong>grid</strong> intersection.</p>
<p>For example <strong>grid tile</strong> 35th intersect with four <strong>cases</strong> 97, 138, 189, 522, 624, 696</p>
<div class="cell" data-nm="true">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>inter[<span class="dv">35</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 class="code-out"><code>[[1]]
[1] 97 138 189 522 624 696</code></pre>
</div>
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(grid[<span class="dv">35</span>, ]))</span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(case_cambodia), <span class="at">add =</span> T)</span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(case_cambodia[<span class="fu">c</span>(<span class="dv">97</span>, <span class="dv">138</span>, <span class="dv">189</span>, <span class="dv">522</span>, <span class="dv">624</span>, <span class="dv">696</span>), ]), </span>
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a> <span class="at">col =</span> <span class="st">"red"</span>, <span class="at">pch =</span> <span class="dv">19</span>, <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">
<p><img src="03-vector_data_files/figure-html/intersect3.3-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>To count number of <strong>case</strong>, simply go to the list and report length of the elements.</p>
<div class="cell">
<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>grid<span class="sc">$</span>nb_case <span class="ot"><-</span> <span class="fu">sapply</span>(<span class="at">X =</span> inter, <span class="at">FUN =</span> length) <span class="co"># create 'nb_case' column to store number of health centers in each grid tile </span></span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(grid[<span class="st">"nb_case"</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="03-vector_data_files/figure-html/intersect3.4-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="aggregate-point-values-into-polygons" class="level3" data-number="3.6.8">
<h3 data-number="3.6.8" class="anchored" data-anchor-id="aggregate-point-values-into-polygons"><span class="header-section-number">3.6.8</span> Aggregate point values into polygons</h3>
<p>In this example we import a csv file that contain data from a population grid. Once import we transform it <code>data.frame</code> into an object <code>sf</code>.</p>
<p>The objective is to aggregate the values id these points (the population contained in the “DENs” field) in the municipalities of the district.</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>pp_pop_raw <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"data_cambodia/pp_pop_dens.csv"</span>) <span class="co"># import file</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a>pp_pop_raw<span class="sc">$</span>id <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">nrow</span>(pp_pop_raw) <span class="co"># adding a unique identifier</span></span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a>pp_pop <span class="ot"><-</span> <span class="fu">st_as_sf</span>(pp_pop_raw, <span class="at">coords =</span> <span class="fu">c</span>(<span class="st">"X"</span>, <span class="st">"Y"</span>), <span class="at">crs =</span> <span class="dv">32648</span>) <span class="co"># Transform into object sf</span></span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a>pp_pop <span class="ot"><-</span> <span class="fu">st_transform</span>(pp_pop, <span class="fu">st_crs</span>(district)) <span class="co"># Transform projection</span></span>
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a>inter <span class="ot"><-</span> <span class="fu">st_intersection</span>(pp_pop, district) <span class="co"># Intersection</span></span>
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a>inter</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Simple feature collection with 1295 features and 12 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 469177.5 ymin: 1263090 xmax: 505177.5 ymax: 1297090
Projected CRS: WGS 84 / UTM zone 48N
First 10 features:
DENs id ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP
149 NA 149 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
150 NA 150 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
151 NA 151 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
186 NA 186 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
187 NA 187 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
188 NA 188 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
223 NA 223 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
224 NA 224 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
225 NA 225 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
226 3.400075 226 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
Area.Km2. Status DENs.1 geometry
149 183.905 <4500km2 503.3958 POINT (469177.5 1267090)
150 183.905 <4500km2 503.3958 POINT (470177.5 1267090)
151 183.905 <4500km2 503.3958 POINT (471177.5 1267090)
186 183.905 <4500km2 503.3958 POINT (469177.5 1268090)
187 183.905 <4500km2 503.3958 POINT (470177.5 1268090)
188 183.905 <4500km2 503.3958 POINT (471177.5 1268090)
223 183.905 <4500km2 503.3958 POINT (469177.5 1269090)
224 183.905 <4500km2 503.3958 POINT (470177.5 1269090)
225 183.905 <4500km2 503.3958 POINT (471177.5 1269090)
226 183.905 <4500km2 503.3958 POINT (472177.5 1269090)</code></pre>
</div>
</div>
<p>By using the function <code>st_intersection()</code> we add to each point of the grid all the information on the municipality in which it is located.</p>
<p>We can then use the function <code>aggregate()</code> to aggregate the population by municipalities.</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>resultat <span class="ot"><-</span> <span class="fu">aggregate</span>(<span class="at">x =</span> <span class="fu">list</span>(<span class="at">pop_from_grid =</span> inter<span class="sc">$</span>DENs), </span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="fu">list</span>(<span class="at">ADM2_EN =</span> inter<span class="sc">$</span>ADM2_EN), </span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a> <span class="at">FUN =</span> <span class="st">"sum"</span>)</span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(resultat)</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 class="code-out"><code> ADM2_EN pop_from_grid
1 Angk Snuol NA
2 Chamkar Mon 10492.7159
3 Chbar Ampov 1593.9593
4 Chraoy Chongvar 1434.1785
5 Dangkao 942.3595
6 Doun Penh 10781.8026</code></pre>
</div>
</div>
<p>We can then create a new object with this result.</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>dist_result <span class="ot"><-</span> <span class="fu">merge</span>(district, resultat, <span class="at">by =</span> <span class="st">"ADM2_EN"</span>, <span class="at">all.x =</span> <span class="cn">TRUE</span>)</span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>dist_result</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Simple feature collection with 197 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 211534.7 ymin: 1149105 xmax: 784612.1 ymax: 1625495
Projected CRS: WGS 84 / UTM zone 48N
First 10 features:
ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP
1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416
2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708
3 Angk Snuol KH0808 Kandal KH08 45436 47141 92577
4 Angkor Borei KH2101 Takeo KH21 26306 27168 53474
5 Angkor Chey KH0701 Kampot KH07 42448 44865 87313
6 Angkor Chum KH1701 Siemreap KH17 34269 34576 68845
7 Angkor Thum KH1702 Siemreap KH17 13802 14392 28194
8 Anlong Veaeng KH2201 Oddar Meanchey KH22 24122 23288 47410
9 Aoral KH0504 Kampong Speu KH05 19874 19956 39830
10 Ba Phnum KH1401 Prey Veng KH14 46562 49852 96414
Area.Km2. Status DENs pop_from_grid geometry
1 1067.8638 <4500km2 79.98773 NA MULTIPOLYGON (((306568.1 14...
2 837.7064 <4500km2 17.55747 NA MULTIPOLYGON (((751459.2 15...
3 183.9050 <4500km2 503.39580 NA MULTIPOLYGON (((471954.3 12...
4 301.0502 <4500km2 177.62485 NA MULTIPOLYGON (((490048.2 12...
5 316.7576 <4500km2 275.64610 NA MULTIPOLYGON (((462702.2 12...
6 478.6988 <4500km2 143.81696 NA MULTIPOLYGON (((363642.5 15...
7 357.8890 <4500km2 78.77862 NA MULTIPOLYGON (((376584.4 15...
8 1533.5702 <4500km2 30.91479 NA MULTIPOLYGON (((404936.4 15...
9 2381.7084 <4500km2 16.72329 NA MULTIPOLYGON (((414000.6 13...
10 342.3439 <4500km2 281.62910 NA MULTIPOLYGON (((545045.4 12...</code></pre>
</div>
</div>
</section>
</section>
<section id="measurements" class="level2" data-number="3.7">
<h2 data-number="3.7" class="anchored" data-anchor-id="measurements"><span class="header-section-number">3.7</span> Measurements</h2>
<section id="create-a-distance-matrix" class="level3" data-number="3.7.1">
<h3 data-number="3.7.1" class="anchored" data-anchor-id="create-a-distance-matrix"><span class="header-section-number">3.7.1</span> Create a distance matrix</h3>
<p>If the dataset’s projection system is specified, the distance are expressed in the projection measurement unit (most often in meter)</p>
<div class="cell" data-nm="true">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>mat <span class="ot"><-</span> <span class="fu">st_distance</span>(<span class="at">x =</span> dist_c, <span class="at">y =</span> dist_c)</span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>mat[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,<span class="dv">1</span><span class="sc">:</span><span class="dv">5</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 class="code-out"><code>Units: [m]
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 425993.7 232592.12 298254.12 299106.92
[2,] 425993.7 0.0 386367.88 414428.82 452431.87
[3,] 232592.1 386367.9 0.00 67060.05 82853.88
[4,] 298254.1 414428.8 67060.05 0.00 40553.15
[5,] 299106.9 452431.9 82853.88 40553.15 0.00</code></pre>
</div>
</div>
</section>
<section id="calculate-routes" class="level3" data-number="3.7.2">
<h3 data-number="3.7.2" class="anchored" data-anchor-id="calculate-routes"><span class="header-section-number">3.7.2</span> Calculate routes</h3>
<p><img src="img/logo_osrm.svg" align="right" width="120"> The package <code>osrm</code> <span class="citation" data-cites="R-osrm">(<a href="references.html#ref-R-osrm" role="doc-biblioref"><strong>R-osrm?</strong></a>)</span> acts as an interface R and the <a href="http://project-osrm.org/">OSRM</a> <span class="citation" data-cites="luxen-vetter-2011">(<a href="references.html#ref-luxen-vetter-2011" role="doc-biblioref"><strong>luxen-vetter-2011?</strong></a>)</span>. This package allows to calculate time and distance matrices, road routes, isochrones. The package uses the OSRM demo server by default. In case of intensive use <a href="https://github.com/Project-OSRM/osrm-backend#using-docker">it is strongly recommended to use your own instance of OSRM (with Docker)</a>.</p>
<section id="calculate-a-route" class="level4" data-number="3.7.2.1">
<h4 data-number="3.7.2.1" class="anchored" data-anchor-id="calculate-a-route"><span class="header-section-number">3.7.2.1</span> Calculate a route</h4>
<p>The fonction <code>osrmRoute()</code> allows you to calculate routes.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(osrm)</span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(maptiles)</span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a>district <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>,<span class="at">layer =</span> <span class="st">"district"</span>, <span class="at">quiet =</span> <span class="cn">TRUE</span>)</span>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a>district <span class="ot"><-</span> <span class="fu">st_transform</span>(district, <span class="dv">32648</span>)</span>
<span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-7"><a href="#cb59-7" aria-hidden="true" tabindex="-1"></a>odongk <span class="ot"><-</span> district[district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0505"</span>, ] <span class="co"># Itinerary between Odongk district and Toul Kouk</span></span>
<span id="cb59-8"><a href="#cb59-8" aria-hidden="true" tabindex="-1"></a>takmau <span class="ot"><-</span> district[district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0811"</span>,]</span>
<span id="cb59-9"><a href="#cb59-9" aria-hidden="true" tabindex="-1"></a>route <span class="ot"><-</span> <span class="fu">osrmRoute</span>(<span class="at">src =</span> odongk, </span>
<span id="cb59-10"><a href="#cb59-10" aria-hidden="true" tabindex="-1"></a> <span class="at">dst =</span> takmau, </span>
<span id="cb59-11"><a href="#cb59-11" aria-hidden="true" tabindex="-1"></a> <span class="at">returnclass =</span> <span class="st">"sf"</span>)</span>
<span id="cb59-12"><a href="#cb59-12" aria-hidden="true" tabindex="-1"></a>osm <span class="ot"><-</span> <span class="fu">get_tiles</span>(route, <span class="at">crop =</span> <span class="cn">TRUE</span>)</span>
<span id="cb59-13"><a href="#cb59-13" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_tiles</span>(osm)</span>
<span id="cb59-14"><a href="#cb59-14" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(route), <span class="at">col =</span> <span class="st">"#b23a5f"</span>, <span class="at">lwd =</span> <span class="dv">6</span>, <span class="at">add =</span> T)</span>
<span id="cb59-15"><a href="#cb59-15" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(route), <span class="at">col =</span> <span class="st">"#eee0e5"</span>, <span class="at">lwd =</span> <span class="dv">1</span>, <span class="at">add =</span> T)</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="03-vector_data_files/figure-html/osrmroute-1.png" class="img-fluid" width="288"></p>
</div>
</div>
</section>
<section id="calculation-of-a-time-matrix" class="level4" data-number="3.7.2.2">
<h4 data-number="3.7.2.2" class="anchored" data-anchor-id="calculation-of-a-time-matrix"><span class="header-section-number">3.7.2.2</span> Calculation of a time matrix</h4>
<p>The function <code>osrmTable()</code> makes it possible to calculate matrices of distances or times by road.</p>
<p>In this example we calculate a time matrix between 2 addresses and health centers in Phnom Penh on foot.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidygeocoder)</span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a>hospital <span class="ot"><-</span> <span class="fu">st_read</span>(<span class="st">"data_cambodia/cambodia.gpkg"</span>,<span class="at">layer=</span> <span class="st">"hospital"</span>, <span class="at">quiet =</span> <span class="cn">TRUE</span>)</span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a>hospital_pp <span class="ot"><-</span> hospital[hospital<span class="sc">$</span>PCODE <span class="sc">==</span> <span class="st">"12"</span>, ] <span class="co"># Selection of health centers in Phnom Penh</span></span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-7"><a href="#cb60-7" aria-hidden="true" tabindex="-1"></a>adresses <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">adr =</span> <span class="fu">c</span>(<span class="st">"Royal Palace Park, Phnom Penh Phnom, Cambodia"</span>,</span>
<span id="cb60-8"><a href="#cb60-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"Wat Phnom Daun Penh, Phnom Penh, Cambodia"</span>)) <span class="co"># Geocoding of 2 addresses in Phnom Penh</span></span>
<span id="cb60-9"><a href="#cb60-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-10"><a href="#cb60-10" aria-hidden="true" tabindex="-1"></a>places <span class="ot"><-</span> tidygeocoder<span class="sc">::</span><span class="fu">geocode</span>(<span class="at">.tbl =</span> adresses,<span class="at">address =</span> adr)</span>
<span id="cb60-11"><a href="#cb60-11" aria-hidden="true" tabindex="-1"></a>places</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 class="code-out"><code># A tibble: 2 × 3
adr lat long
<chr> <dbl> <dbl>
1 Royal Palace Park, Phnom Penh Phnom, Cambodia 11.6 105.
2 Wat Phnom Daun Penh, Phnom Penh, Cambodia 11.6 105.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculation of the distance matrix between the 2 addresses and the health center in Phnom Penh</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a>cal_mat <span class="ot"><-</span> <span class="fu">osrmTable</span>(<span class="at">src =</span> places[,<span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">3</span>,<span class="dv">2</span>)], </span>
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a> <span class="at">dst =</span> hospital_pp, </span>
<span id="cb62-5"><a href="#cb62-5" aria-hidden="true" tabindex="-1"></a> <span class="at">osrm.profile =</span> <span class="st">"foot"</span>)</span>
<span id="cb62-6"><a href="#cb62-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-7"><a href="#cb62-7" aria-hidden="true" tabindex="-1"></a>cal_mat<span class="sc">$</span>durations[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">5</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 class="code-out"><code> 684 685 686 687 691
Royal Palace Park, Phnom Penh Phnom, Cambodia 55.9 71.6 64.4 40.2 76.7
Wat Phnom Daun Penh, Phnom Penh, Cambodia 60.1 80.4 40.1 32.8 53.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Which address has better accessibility to health center in Phnom Penh?</span></span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="fu">boxplot</span>(<span class="fu">t</span>(cal_mat<span class="sc">$</span>durations[,]), <span class="at">cex.axis =</span> <span class="fl">0.7</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
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-output-display">
<p><img src="03-vector_data_files/figure-html/osrmtable-1.png" class="img-fluid" width="768"></p>
</div>
</div>
</section>
</section>
</section>
</main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});