Skip to content
Snippets Groups Projects
03-vector_data.html 81.7 KiB
Newer Older
lucas.longour_ird.fr's avatar
lucas.longour_ird.fr committed
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 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 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 365 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
<!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-0.9.617">

<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&nbsp; Vector Data</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
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 { } /* 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 { } /* 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="./references.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">
<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>


<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>&nbsp; <span class="chapter-title">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/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>&nbsp; <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>&nbsp; <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>&nbsp; <span class="chapter-title">Vector Data</span></a>
  </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">
    <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="header-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="header-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="header-section-number">3.3</span> Coordinate systems</a>
  <ul class="collapse">
  <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="header-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="header-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="header-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="header-section-number">3.5</span> Spatial selection</a>
  <ul class="collapse">
  <li><a href="#intersections" id="toc-intersections" class="nav-link" data-scroll-target="#intersections"> <span class="header-section-number">3.5.1</span> Intersections</a></li>
  <li><a href="#contains-within" id="toc-contains-within" class="nav-link" data-scroll-target="#contains-within"> <span class="header-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="header-section-number">3.6</span> Operation of geometries</a>
  <ul class="collapse">
  <li><a href="#extract-centroids" id="toc-extract-centroids" class="nav-link" data-scroll-target="#extract-centroids"> <span class="header-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="header-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="header-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="header-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="header-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="header-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="header-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="header-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="header-section-number">3.7</span> Measurements</a>
  <ul class="collapse">
  <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="header-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="header-section-number">3.7.2</span> Calculate routes</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">3</span>&nbsp; <span class="chapter-title">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>cambodia.gpkg</strong> <a href="https://www.geopackage.org/">geopackage</a> file.</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>(sf)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-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>
<div class="cell-output cell-output-stdout">
<pre class="code-out"><code>Reading layer `district' from data source 
  `/home/lucas/Documents/GitHub/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>
</div>
</div>
<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="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><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></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>Deleting layer `district' using driver `GPKG'
Writing layer `district' to data source 
  `data_cambodia/district.gpkg' using driver `GPKG'
Writing 197 features with 10 fields and geometry type Multi Polygon.</code></pre>
</div>
<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_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 class="cell-output cell-output-stdout">
<pre class="code-out"><code>Deleting layer `district' using driver `ESRI Shapefile'
Writing layer `district' to data source 
  `data_cambodia/district.shp' using driver `ESRI Shapefile'
options:        ENCODING=UTF-8 
Writing 197 features with 10 fields and geometry type Multi Polygon.</code></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="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><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 &lt;4500km2  79.98773 MULTIPOLYGON (((306568.1 14...
2 &lt;4500km2  17.55747 MULTIPOLYGON (((751459.2 15...
3 &lt;4500km2 503.39580 MULTIPOLYGON (((471954.3 12...
4 &lt;4500km2 177.62485 MULTIPOLYGON (((490048.2 12...
5 &lt;4500km2 275.64610 MULTIPOLYGON (((462702.2 12...
6 &lt;4500km2 143.81696 MULTIPOLYGON (((363642.5 15...</code></pre>
</div>
<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">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="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="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="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_crs</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>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="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><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb13-2"><a href="#cb13-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="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>dist_reproj <span class="ot">&lt;-</span> <span class="fu">st_transform</span>(district, <span class="st">"epsg:4326"</span>)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(dist_reproj))</span>
<span id="cb14-3"><a href="#cb14-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="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="co"># row Selection</span></span>
<span id="cb15-2"><a href="#cb15-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 &lt;4500km2 79.98773 MULTIPOLYGON (((306568.1 14...
2 &lt;4500km2 17.55747 MULTIPOLYGON (((751459.2 15...</code></pre>
</div>
<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>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>
<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 &lt;4500km2  9661.5265 MULTIPOLYGON (((494709.4 12...
31   86.780498 &lt;4500km2  1533.2823 MULTIPOLYGON (((498855.3 12...
43   85.609156 &lt;4500km2   724.3034 MULTIPOLYGON (((491161.3 12...
48  113.774833 &lt;4500km2   839.5881 MULTIPOLYGON (((489191.1 12...
50    7.734808 &lt;4500km2  9090.7234 MULTIPOLYGON (((492447.1 12...
93   28.998026 &lt;4500km2  4784.7051 MULTIPOLYGON (((491068.2 12...
117 115.384300 &lt;4500km2   478.9560 MULTIPOLYGON (((481483.3 12...
118   2.224892 &lt;4500km2 29115.1253 MULTIPOLYGON (((491067.6 12...
133 148.357984 &lt;4500km2  1377.3913 MULTIPOLYGON (((479078.8 12...
141  23.381517 &lt;4500km2  5806.9800 MULTIPOLYGON (((490264.8 12...</code></pre>
</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><span class="co"># column selection</span></span>
<span id="cb19-2"><a href="#cb19-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>
<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="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>road <span class="ot">&lt;-</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>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>dangkao <span class="ot">&lt;-</span>  district[district<span class="sc">$</span>ADM2_EN <span class="sc">==</span> <span class="st">"Dangkao"</span>, ]</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>inter <span class="ot">&lt;-</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="cb21-4"><a href="#cb21-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,] TRUE
[2,] TRUE
[3,] TRUE
[4,] TRUE
[5,] TRUE
[6,] TRUE</code></pre>
</div>
<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><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] 6 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="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>road<span class="sc">$</span>intersect_dangkao <span class="ot">&lt;-</span> inter</span>
<span id="cb25-2"><a href="#cb25-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="cb25-3"><a href="#cb25-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="cb25-4"><a href="#cb25-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="cb25-5"><a href="#cb25-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="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>inter <span class="ot">&lt;-</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="cb26-2"><a href="#cb26-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="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>inter <span class="ot">&lt;-</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="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">rownames</span>(inter) <span class="ot">&lt;-</span> grid<span class="sc">$</span>id</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(inter) <span class="ot">&lt;-</span> pt<span class="sc">$</span>id</span>
<span id="cb28-4"><a href="#cb28-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="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>within_dangkao <span class="ot">&lt;-</span> <span class="fu">st_within</span>(road, dangkao, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</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></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 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>(<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="cb31-2"><a href="#cb31-2" 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>
</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="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>dist_c <span class="ot">&lt;-</span> <span class="fu">st_centroid</span>(district)</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb32-3"><a href="#cb32-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="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>cambodia_dist <span class="ot">&lt;-</span> <span class="fu">st_union</span>(district)                 </span>
<span id="cb33-2"><a href="#cb33-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="cb33-3"><a href="#cb33-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="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>dist_union  <span class="ot">&lt;-</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="cb34-2"><a href="#cb34-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="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>                   <span class="at">FUN =</span> <span class="st">"sum"</span>)</span>
<span id="cb34-4"><a href="#cb34-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="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>dangkao_buffer <span class="ot">&lt;-</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="cb35-2"><a href="#cb35-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="cb35-3"><a href="#cb35-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="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="fu">library</span>(magrittr)</span>
<span id="cb36-2"><a href="#cb36-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="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a><span class="co"># using the pipe</span></span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a>zone <span class="ot">&lt;-</span> <span class="fu">st_geometry</span>(dangkao) <span class="sc">%&gt;%</span></span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">st_centroid</span>() <span class="sc">%&gt;%</span></span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">st_buffer</span>(<span class="dv">30000</span>)</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">st_geometry</span>(district))</span>
<span id="cb36-8"><a href="#cb36-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="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>dist_z <span class="ot">&lt;-</span> <span class="fu">st_intersection</span>(<span class="at">x =</span> district, <span class="at">y =</span> zone)</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>
<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>(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="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><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="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>grid <span class="ot">&lt;-</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="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a>grid <span class="ot">&lt;-</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="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-4"><a href="#cb39-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="cb39-5"><a href="#cb39-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="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="co"># selection of grid tiles that intersect the district</span></span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a>inter <span class="ot">&lt;-</span> <span class="fu">st_intersects</span>(grid, cambodia_dist, <span class="at">sparse =</span> <span class="cn">FALSE</span>)</span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a>grid <span class="ot">&lt;-</span> grid[inter, ]</span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a>case_cambodia <span class="ot">&lt;-</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="cb40-7"><a href="#cb40-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="cb40-8"><a href="#cb40-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="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>inter <span class="ot">&lt;-</span> <span class="fu">st_intersects</span>(grid, case_cambodia, <span class="at">sparse =</span> <span class="cn">TRUE</span>)</span>
<span id="cb41-2"><a href="#cb41-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="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>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="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><span class="fu">plot</span>(<span class="fu">st_geometry</span>(grid[<span class="dv">35</span>, ]))</span>
<span id="cb45-2"><a href="#cb45-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="cb45-3"><a href="#cb45-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="cb45-4"><a href="#cb45-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="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>grid<span class="sc">$</span>nb_case <span class="ot">&lt;-</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="cb46-2"><a href="#cb46-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="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>pp_pop_raw <span class="ot">&lt;-</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="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a>pp_pop_raw<span class="sc">$</span>id <span class="ot">&lt;-</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="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a>pp_pop <span class="ot">&lt;-</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="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a>pp_pop <span class="ot">&lt;-</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="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a>inter <span class="ot">&lt;-</span> <span class="fu">st_intersection</span>(pp_pop, district)                         <span class="co"># Intersection</span></span>
<span id="cb47-6"><a href="#cb47-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>
<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 &lt;4500km2 503.3958 POINT (469177.5 1267090)
150   183.905 &lt;4500km2 503.3958 POINT (470177.5 1267090)
151   183.905 &lt;4500km2 503.3958 POINT (471177.5 1267090)
186   183.905 &lt;4500km2 503.3958 POINT (469177.5 1268090)
187   183.905 &lt;4500km2 503.3958 POINT (470177.5 1268090)
188   183.905 &lt;4500km2 503.3958 POINT (471177.5 1268090)
223   183.905 &lt;4500km2 503.3958 POINT (469177.5 1269090)
224   183.905 &lt;4500km2 503.3958 POINT (470177.5 1269090)
225   183.905 &lt;4500km2 503.3958 POINT (471177.5 1269090)
226   183.905 &lt;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="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>resultat <span class="ot">&lt;-</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="cb49-2"><a href="#cb49-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="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a>                      <span class="at">FUN =</span> <span class="st">"sum"</span>)</span>
<span id="cb49-4"><a href="#cb49-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="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>dist_result <span class="ot">&lt;-</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="cb51-2"><a href="#cb51-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>
<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 &lt;4500km2  79.98773            NA MULTIPOLYGON (((306568.1 14...
2   837.7064 &lt;4500km2  17.55747            NA MULTIPOLYGON (((751459.2 15...
3   183.9050 &lt;4500km2 503.39580            NA MULTIPOLYGON (((471954.3 12...
4   301.0502 &lt;4500km2 177.62485            NA MULTIPOLYGON (((490048.2 12...
5   316.7576 &lt;4500km2 275.64610            NA MULTIPOLYGON (((462702.2 12...
6   478.6988 &lt;4500km2 143.81696            NA MULTIPOLYGON (((363642.5 15...
7   357.8890 &lt;4500km2  78.77862            NA MULTIPOLYGON (((376584.4 15...
8  1533.5702 &lt;4500km2  30.91479            NA MULTIPOLYGON (((404936.4 15...
9  2381.7084 &lt;4500km2  16.72329            NA MULTIPOLYGON (((414000.6 13...
10  342.3439 &lt;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="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>mat <span class="ot">&lt;-</span> <span class="fu">st_distance</span>(<span class="at">x =</span> dist_c, <span class="at">y =</span> dist_c)</span>
<span id="cb53-2"><a href="#cb53-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="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="fu">library</span>(sf)</span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(osrm)</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(maptiles)</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a>district <span class="ot">&lt;-</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="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a>district <span class="ot">&lt;-</span> <span class="fu">st_transform</span>(district, <span class="dv">32648</span>)</span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a>odongk  <span class="ot">&lt;-</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="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a>takmau <span class="ot">&lt;-</span> district[district<span class="sc">$</span>ADM2_PCODE <span class="sc">==</span> <span class="st">"KH0811"</span>,]</span>
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a>route <span class="ot">&lt;-</span> <span class="fu">osrmRoute</span>(<span class="at">src =</span> odongk, </span>
<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a>                   <span class="at">dst =</span> takmau, </span>
<span id="cb55-11"><a href="#cb55-11" aria-hidden="true" tabindex="-1"></a>                   <span class="at">returnclass =</span> <span class="st">"sf"</span>)</span>
<span id="cb55-12"><a href="#cb55-12" aria-hidden="true" tabindex="-1"></a>osm <span class="ot">&lt;-</span> <span class="fu">get_tiles</span>(route, <span class="at">crop =</span> <span class="cn">TRUE</span>)</span>
<span id="cb55-13"><a href="#cb55-13" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_tiles</span>(osm)</span>
<span id="cb55-14"><a href="#cb55-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="cb55-15"><a href="#cb55-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="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="fu">library</span>(sf)</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidygeocoder)</span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a>hospital <span class="ot">&lt;-</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="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a>hospital_pp <span class="ot">&lt;-</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="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a>adresses <span class="ot">&lt;-</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="cb56-8"><a href="#cb56-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="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-10"><a href="#cb56-10" aria-hidden="true" tabindex="-1"></a>places <span class="ot">&lt;-</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="cb56-11"><a href="#cb56-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
  &lt;chr&gt;                                         &lt;dbl&gt; &lt;dbl&gt;
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="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-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="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a>cal_mat <span class="ot">&lt;-</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="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a>                 <span class="at">dst =</span> hospital_pp, </span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a>                 <span class="at">osrm.profile =</span> <span class="st">"foot"</span>)</span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-7"><a href="#cb58-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 56.1 71.9 64.2 40.4 76.5
Wat Phnom Daun Penh, Phnom Penh, Cambodia     60.1 80.5 43.8 32.8 55.6</code></pre>
</div>
<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="co"># Which address has better accessibility to health center in Phnom Penh?</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-3"><a href="#cb60-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>
<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 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;
    }
  });
  clipboard.on('success', function(e) {
    // button target
    const button = e.trigger;
    // don't keep focus
    button.blur();
    // flash "checked"
    button.classList.add('code-copy-button-checked');
    var currentTitle = button.getAttribute("title");
    button.setAttribute("title", "Copied!");
    setTimeout(function() {
      button.setAttribute("title", currentTitle);
      button.classList.remove('code-copy-button-checked');
    }, 1000);
    // clear code selection
    e.clearSelection();
  });
  function tippyHover(el, contentFn) {
    const config = {
      allowHTML: true,
      content: contentFn,
      maxWidth: 500,
      delay: 100,
      arrow: false,
      appendTo: function(el) {
          return el.parentElement;
      },
      interactive: true,
      interactiveBorder: 10,
      theme: 'quarto',
      placement: 'bottom-start'
    };
    window.tippy(el, config); 
  }
  const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
  for (var i=0; i<noterefs.length; i++) {
    const ref = noterefs[i];
    tippyHover(ref, function() {
      let href = ref.getAttribute('href');
      try { href = new URL(href).hash; } catch {}
      const id = href.replace(/^#\/?/, "");
      const note = window.document.getElementById(id);
      return note.innerHTML;
    });
  }
  var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
  for (var i=0; i<bibliorefs.length; i++) {
    const ref = bibliorefs[i];
    const cites = ref.parentNode.getAttribute('data-cites').split(' ');
    tippyHover(ref, function() {
      var popup = window.document.createElement('div');
      cites.forEach(function(cite) {
        var citeDiv = window.document.createElement('div');
        citeDiv.classList.add('hanging-indent');
        citeDiv.classList.add('csl-entry');
        var biblioDiv = window.document.getElementById('ref-' + cite);
        if (biblioDiv) {
          citeDiv.innerHTML = biblioDiv.innerHTML;
        }
        popup.appendChild(citeDiv);
      });
      return popup.innerHTML;
    });
  }
    var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
      var filterRegex = new RegExp('/' + window.location.host + '/');
    var isInternal = (href) => {
        return filterRegex.test(href) || localhostRegex.test(href);
    }
    // Inspect non-navigation links and adorn them if external
    var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)');
    for (var i=0; i<links.length; i++) {
      const link = links[i];
      if (!isInternal(link.href)) {
          // target, if specified
          link.setAttribute("target", "_blank");
      }
    }
});
</script>
<nav class="page-navigation">
  <div class="nav-page nav-page-previous">
      <a href="./02-data_acquisition.html" class="pagination-link">
        <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Data Acquisition</span></span>
      </a>          
  </div>
  <div class="nav-page nav-page-next">
      <a href="./references.html" class="pagination-link">
        <span class="nav-page-text">References</span> <i class="bi bi-arrow-right-short"></i>
      </a>
  </div>
</nav>