-
lucas.longour_ird.fr authoredlucas.longour_ird.fr authored
search.json 47.02 KiB
[
{
"objectID": "01-introduction.html",
"href": "01-introduction.html",
"title": "1 Introduction",
"section": "",
"text": "Historically, 4 packages make it possible to import, manipulate and transform spatial data:\n\nThe package rgdal (Bivand, Keitt, and Rowlingson 2022) which is an interface between R and the GDAL (GDAL/OGR contributors, n.d.) and PROJ (PROJ contributors 2021) libraries allow you to import and export spatial data (shapefiles for example) and also to manage cartographic projections\n\nThe package sp (E. J. Pebesma and Bivand 2005) provides class and methods for vector spatial data in R. It allows displaying background maps, inspectiong an attribute table etc.\n\nThe package rgeos (Bivand and Rundel 2021) gives access to the GEOS spatial operations library and therefore makes classic GIS operations available: calculation of surfaces or perimeters, calculation of distances, spatial aggregations, buffer zones, intersections, etc.\n\nThe package raster (Hijmans 2022a) is dedicated to the import, manipulation and modeling of raster data.\n\nToday, the main developments concerning vector data have moved away from the old 3 (sp, rgdal, rgeos) to rely mainly on the package sf ((E. Pebesma 2018a), (E. Pebesma 2018b)). In this manual we will rely exclusively on this package to manipulate vector data.\nThe packages stars (E. Pebesma 2021) and terra (Hijmans 2022b) come to replace the package raster for processing raster data. We have chosen to use the package here terra for its proximity to the raster."
},
{
"objectID": "01-introduction.html#the-package-sf",
"href": "01-introduction.html#the-package-sf",
"title": "1 Introduction",
"section": "1.2 The package sf",
"text": "1.2 The package sf\n The package sf was released in late 2016 by Edzer Pebesma (also author of sp). Its goal is to combine the feature of sp, rgeos and rgdal in a single, more ergonomic package. This package offers simple objects (following the simple feature standard) which are easier to manipulate. Particular attention has been paid to the compatibility of the package with the pipe syntax and the operators of the tidyverse.\nsf directly uses the GDAL, GEOS and PROJ libraries.\n\n\n\n\n\nFrom r-spatial.org\n\n\n\n\n\n\nWebsite of package sf : Simple Features for R\n\n\n\n\n1.2.1 Format of spatial objects sf\n\n\n\n\n\nObjectssf are objects in data.frame which one of the columns contains geometries. This column is the class of sfc (simple feature column) and each individual of the column is a sfg (simple feature geometry). This format is very practical insofa as the data and the geometries are intrinsically linked in the same object.\n\n\n\n\n\n\nThumbnail describing the simple feature format: Simple Features for R\n\n\n\n\n\n\n\n\n\nTip\n\n\n\nA benchmark of vector processing libraries is available here."
},
{
"objectID": "01-introduction.html#package-mapsf",
"href": "01-introduction.html#package-mapsf",
"title": "1 Introduction",
"section": "1.3 Package mapsf",
"text": "1.3 Package mapsf\nThe free R software spatial ecosystem is rich, dynamic and mature and several packages allow to import, process and represent spatial data. The package mapsf (Giraud 2022) relies on this ecosystem to integrate the creation of quality thematic maps into processing chains with R.\nOther packages can be used to make thematic maps. The package ggplot2 (Wickham 2016), in association with the package ggspatial (Dunnington 2021), allows for example to display spatial objects and to make simple thematic maps. The package tmap (Tennekes 2018) is dedicated to the creation of thematic maps, it uses a syntax close to that of ggplot2 (sequence of instructions combined with the ‘+’ sign). Documentation and tutorials for using these two packages are readily available on the web.\nHere, we will mainly use the package mapsf whose functionalities are quite complete and the handling rather simple. In addition, the package is relatively light.\n\nmapsf allows you to create most of the types of map usually used in statistical cartography (choropleth maps, typologies, proportional or graduated symbols, etc.). For each type of map, several parameters are used to customize the cartographic representation. These parameters are the same as those found in the usual GIS or cartography software (for example, the choice of discretizations and color palettes, the modification of the size of the symbols or the customization of the legends). Associated with the data representation functions, other functions are dedicated to cartographic dressing (themes or graphic charters, legends, scales, orientation arrows, title, credits, annotations, etc.), the creation of boxes or the exporting maps.\nmapsf is the successor of cartography (Giraud and Lambert 2016), it offers the same main functionalities while being lighter and more ergonomic.\nTo use this package several sources can be consulted:\n\nThe package documentation accessible on the internet or directly in R (?mapsf),\nA cheat sheet,\n\n\n\n\n\n\n\nThe vignettes associated with the package show sample scripts,\nThe R Geomatics blog which provides resources and examples related to the package and more generally to the R spatial ecosystem."
},
{
"objectID": "01-introduction.html#the-package-terra",
"href": "01-introduction.html#the-package-terra",
"title": "1 Introduction",
"section": "1.4 The package terra",
"text": "1.4 The package terra\n The package terra was release in early 2020 by Robert J. Hijmans (also author of raster). Its objective is to propose methods of treatment and analysis of raster data. This package is very similar to the package raster; but it has more features, it’s easier to use, and it’s faster.\n\n\n\n\n\n\nWebsite of package terra : Spatial Data Science with R and “terra”\n\n\n\n\n\n\n\n\n\nTip\n\n\n\nA benchmark of raster processing libraries is available here.\n\n\n\n\n\n\nBivand, Roger, Tim Keitt, and Barry Rowlingson. 2022. “Rgdal: Bindings for the ’Geospatial’ Data Abstraction Library.” https://CRAN.R-project.org/package=rgdal.\n\n\nBivand, Roger, and Colin Rundel. 2021. “Rgeos: Interface to Geometry Engine - Open Source (’GEOS’).” https://CRAN.R-project.org/package=rgeos.\n\n\nDunnington, Dewey. 2021. “Ggspatial: Spatial Data Framework for Ggplot2.” https://CRAN.R-project.org/package=ggspatial.\n\n\nGDAL/OGR contributors. n.d. GDAL/OGR Geospatial Data Abstraction Software Library. Open Source Geospatial Foundation. https://gdal.org.\n\n\nGiraud, Timothée. 2022. “Mapsf: Thematic Cartography.” https://CRAN.R-project.org/package=mapsf.\n\n\nGiraud, Timothée, and Nicolas Lambert. 2016. “Cartography: Create and Integrate Maps in Your r Workflow” 1. https://doi.org/10.21105/joss.00054.\n\n\nHijmans, Robert J. 2022a. “Raster: Geographic Data Analysis and Modeling.” https://CRAN.R-project.org/package=raster.\n\n\n———. 2022b. “Terra: Spatial Data Analysis.” https://CRAN.R-project.org/package=terra.\n\n\nPebesma, Edzer. 2018a. “Simple Features for r: Standardized Support for Spatial Vector Data” 10. https://doi.org/10.32614/RJ-2018-009.\n\n\n———. 2018b. “Simple Features for R: Standardized Support for Spatial Vector Data.” The R Journal 10 (1): 439. https://doi.org/10.32614/rj-2018-009.\n\n\n———. 2021. “Stars: Spatiotemporal Arrays, Raster and Vector Data Cubes.” https://CRAN.R-project.org/package=stars.\n\n\nPebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods for Spatial Data in r” 5. https://CRAN.R-project.org/doc/Rnews/.\n\n\nPROJ contributors. 2021. PROJ Coordinate Transformation Software Library. Open Source Geospatial Foundation. https://proj.org/.\n\n\nTennekes, Martijn. 2018. “Tmap: Thematic Maps in r” 84. https://doi.org/10.18637/jss.v084.i06.\n\n\nWickham, Hadley. 2016. “Ggplot2: Elegant Graphics for Data Analysis.” https://ggplot2.tidyverse.org."
},
{
"objectID": "references.html",
"href": "references.html",
"title": "References",
"section": "",
"text": "Bivand, Roger, Tim Keitt, and Barry Rowlingson. 2022. “Rgdal:\nBindings for the ’Geospatial’ Data Abstraction Library.” https://CRAN.R-project.org/package=rgdal.\n\n\nBivand, Roger, and Colin Rundel. 2021. “Rgeos: Interface to\nGeometry Engine - Open Source (’GEOS’).” https://CRAN.R-project.org/package=rgeos.\n\n\nDunnington, Dewey. 2021. “Ggspatial: Spatial Data Framework for\nGgplot2.” https://CRAN.R-project.org/package=ggspatial.\n\n\nGDAL/OGR contributors. n.d. GDAL/OGR Geospatial Data\nAbstraction Software Library. Open Source Geospatial Foundation. https://gdal.org.\n\n\nGiraud, Timothée. 2022. “Mapsf: Thematic Cartography.” https://CRAN.R-project.org/package=mapsf.\n\n\nGiraud, Timothée, and Nicolas Lambert. 2016. “Cartography: Create\nand Integrate Maps in Your r Workflow” 1. https://doi.org/10.21105/joss.00054.\n\n\nHijmans, Robert J. 2022a. “Raster: Geographic Data Analysis and\nModeling.” https://CRAN.R-project.org/package=raster.\n\n\n———. 2022b. “Terra: Spatial Data Analysis.” https://CRAN.R-project.org/package=terra.\n\n\nPebesma, Edzer. 2018b. “Simple Features for r:\nStandardized Support for Spatial Vector Data” 10. https://doi.org/10.32614/RJ-2018-009.\n\n\n———. 2018a. “Simple Features for R: Standardized Support for\nSpatial Vector Data.” The R Journal 10 (1): 439. https://doi.org/10.32614/rj-2018-009.\n\n\n———. 2021. “Stars: Spatiotemporal Arrays, Raster and Vector Data\nCubes.” https://CRAN.R-project.org/package=stars.\n\n\nPebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods\nfor Spatial Data in r” 5. https://CRAN.R-project.org/doc/Rnews/.\n\n\nPROJ contributors. 2021. PROJ Coordinate Transformation\nSoftware Library. Open Source Geospatial Foundation. https://proj.org/.\n\n\nTennekes, Martijn. 2018. “Tmap: Thematic\nMaps in r” 84. https://doi.org/10.18637/jss.v084.i06.\n\n\nWickham, Hadley. 2016. “Ggplot2: Elegant Graphics for Data\nAnalysis.” https://ggplot2.tidyverse.org."
},
{
"objectID": "02-data_acquisition.html",
"href": "02-data_acquisition.html",
"title": "2 Data Acquisition",
"section": "",
"text": "The function st_as_sf() makes it possible to transform a data.frame container of geographic coordinates into an object sf. Here we use the data.frame places2 created in the previous point.\n\nlibrary(sf)\nplace_sf <- st_as_sf(read.csv(\"data_cambodia/adress.csv\"), coords = c(\"long\", \"lat\"), crs = 4326)\nplace_sf\n\nSimple feature collection with 2 features and 1 field\nGeometry type: POINT\nDimension: XY\nBounding box: xmin: 104.8443 ymin: 11.54366 xmax: 104.9047 ymax: 11.55349\nGeodetic CRS: WGS 84\n address\n1 Phnom Penh International Airport, Phnom Penh, Cambodia\n2 Khmer Soviet Friendship Hospital, Phnom Penh, Cambodia\n geometry\n1 POINT (104.8443 11.55349)\n2 POINT (104.9047 11.54366)\n\n\nTo crate a sf POINT type object with only one pair of coordinate (WGS84, longitude=0.5, latitude = 45.5) :\n\nlibrary(sf)\ntest_point <- st_as_sf(data.frame(x = 0.5, y = 45.5), coords = c(\"x\", \"y\"), crs = 4326)\ntest_point\n\nSimple feature collection with 1 feature and 0 fields\nGeometry type: POINT\nDimension: XY\nBounding box: xmin: 0.5 ymin: 45.5 xmax: 0.5 ymax: 45.5\nGeodetic CRS: WGS 84\n geometry\n1 POINT (0.5 45.5)\n\n\nWe can display this object sf on an OpenStreetMap basesmap with the package maptiles maptiles (Giraud 2021).\n\nlibrary(maptiles)\nosm <- get_tiles(x = place_sf, zoom = 12)\nplot_tiles(osm)\nplot(st_geometry(place_sf), pch = 2, cex = 2, col = \"red\", add = TRUE)"
},
{
"objectID": "02-data_acquisition.html#online-databases",
"href": "02-data_acquisition.html#online-databases",
"title": "2 Data Acquisition",
"section": "2.2 Online databases",
"text": "2.2 Online databases"
},
{
"objectID": "02-data_acquisition.html#openstreetmap",
"href": "02-data_acquisition.html#openstreetmap",
"title": "2 Data Acquisition",
"section": "2.3 OpenStreetMap",
"text": "2.3 OpenStreetMap\n\n\n\nOpenStreetMap (OSM) is a participatory mapping project that aim s to buil a free geographic database on a global scale. OpenStreetMap lets you view, edit and use geographic data around the world.\nTerms of use\n\nOpenStreetMap is open data : you are free to use it for ant purpose as long as you credit OpenStreetMap and its contributers. If you modify or rely data in any way, you may distribute the result only under the same license. (…)\n\nContributors\n\n(…) Our contributors incloude enthusiastic mapmakers, GIS professional, engineers running OSM servers, humanitarians mapping disaster-stricken areas and many mmore.(…)\n\n\n2.3.1 Display and interactive map\nThe two main packages that allow to display as interactive map based on OSM are leaflet (Cheng, Karambelkar, and Xie 2022) and mapview (Appelhans et al. 2022).\n\n2.3.1.1 leaflet\n leaflet uses the javascript library Leaflet (Agafonkin 2015) to create interactive maps.\n\nlibrary(sf)\nlibrary(leaflet)\n\ndistrict <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE)\nhospital <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"hospital\", quiet = TRUE)\n\n\nbanan <- district[district$ADM2_PCODE == \"KH0201\", ] #Select one district (Banan district: KH0201)\nhealth_banan <- hospital[hospital$DCODE == \"201\", ] #Select Health centers in Banan\n\nbanan <- st_transform(banan, 4326) #Transform coordinate system to WGS84\nhealth_banan <- st_transform(health_banan, 4326)\n\nbanan_map <- leaflet(banan) %>% #Create interactive map\n addTiles() %>%\n addPolygons() %>%\n addMarkers(data = health_banan)\nbanan_map\n\n\n\n\n\n\n\n\n\n\n\nWebsite of leaflet\nLeaflet for R\n\n\n\n\n\n2.3.1.2 mapview\n mapview relies on leaflet to create interactive maps, its use is easier and its documentation is a bit dense.\n\nlibrary(mapview)\nmapview(banan) + mapview(health_banan)\n\n\n\n\n\n\n\n\n\n\n\n\nWebsite of mapview\nmapview\n\n\n\n\n\n\n2.3.2 Import basemaps\nThe package maptiles (Giraud 2021) allows downlaoding and displaying raster basemaps.\nThe function get_tiles() allow you to download OSM background maps and the function plot_tiles() allows to display them.\nRenders are better if the input data used the same coordinate system as the tiles (EPSG:3857).\n\nlibrary(sf)\nlibrary(maptiles)\ndistrict <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\", quiet = TRUE)\ndistrict <- st_transform(district, 3857)\nosm_tiles <- get_tiles(x = district, zoom = 10, crop = TRUE)\nplot_tiles(osm_tiles)\nplot(st_geometry(district), border = \"grey20\", lwd = .7, add = TRUE)\nmtext(side = 1, line = -2, text = get_credit(\"OpenStreetMap\"), col=\"tomato\")\n\n\n\n\n\n\n2.3.3 Import OSM data\n\n2.3.3.1 osmdata\n The package osmdata (Padgham et al. 2017) allows extracting vector data from OSM using the Overpass turbo API.\n\nlibrary(sf)\nlibrary(osmdata)\nlibrary(sf)\n\ncountry <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"country\", quiet = TRUE)\next <- opq(bbox = st_bbox(st_transform(country, 4326))) #Define the bounding box\nquery <- add_osm_feature(opq = ext, key = 'amenity', value = \"hospital\") #Health Center Extraction\nhospital <- osmdata_sf(query)\nhospital <- unique_osmdata(hospital) #Result reduction (points composing polygon are detected)\n\nThe result contains a point layer and a polygon layer. The polygon layer contains polygons that represent fast food-food place. To obtain a coherent point layer we can use the centroids of the polygons.\n\nhospital_point <- hospital$osm_points\nhospital_poly <- hospital$osm_polygons #Extracting centroids of polygons\nhospital_poly_centroid <- st_centroid(hospital_poly)\n\ncambodia_point <- intersect(names(hospital_point), names(hospital_poly_centroid)) #Identify fields in Cambodia boundary\nhospitals <- rbind(hospital_point[, cambodia_point], hospital_poly_centroid[, cambodia_point]) #Gather the 2 objects\n\nResult display\n\nlibrary(mapview)\nmapview(country) + mapview(hospitals)\n\n\n\n\n\n\n\n\n\n\n\n\nWebsite of osmdata\nosmdata\n\n\n\n\n\n2.3.3.2 osmextract\n The package osmextract (Gilardi and Lovelace 2021) allows to extract data from an OSM database directly. This package make it possible to work on very large volumes of data.\n\n\n\n\n\n\nWebsite of osmextract\nosmextract\n\n\n\nFor administrative boundaries, check here the administrative levels by country:\n\nlibrary(osmextract)\nlibrary(mapsf)\nprovince <- oe_get(\n place = \"Cambodia\",\n download_directory = \"data_cambodia/\",\n layer = \"multipolygons\",\n extra_tags = c(\"wikidata\", \"ISO3166-2\", \"wikipedia\", \"name:en\"),\n vectortranslate_options = c(\n \"-t_srs\", \"EPSG:32648\",\n \"-nlt\", \"PROMOTE_TO_MULTI\",\n \"-where\", \"type = 'boundary' AND boundary = 'administrative' AND admin_level = '4'\"\n ))\n\n0...10...20...30...40...50...60...70...80...90...100 - done.\nReading layer `multipolygons' from data source \n `/home/lucas/Documents/GitHub/rspatial-for-onehealth/data_cambodia/geofabrik_cambodia-latest.gpkg' \n using driver `GPKG'\nSimple feature collection with 25 features and 29 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 211418.1 ymin: 1047956 xmax: 784614.9 ymax: 1625621\nProjected CRS: WGS 84 / UTM zone 48N\n\nmf_map(x = province)\n\n\n\n\n\nroads <- oe_get(\n place = \"Cambodia\",\n download_directory = \"data_cambodia/\",\n layer = \"lines\",\n extra_tags = c(\"access\", \"service\", \"maxspeed\"),\n vectortranslate_options = c(\n \"-t_srs\", \"EPSG:32648\",\n \"-nlt\", \"PROMOTE_TO_MULTI\",\n \"-where\", \"\n highway IS NOT NULL\n AND\n highway NOT IN (\n 'abandonded', 'bus_guideway', 'byway', 'construction', 'corridor', 'elevator',\n 'fixme', 'escalator', 'gallop', 'historic', 'no', 'planned', 'platform',\n 'proposed', 'cycleway', 'pedestrian', 'bridleway', 'footway',\n 'steps', 'path', 'raceway', 'road', 'service', 'track'\n )\n \"\n),\n boundary = subset(province, name_en == \"Phnom Penh\"),\n boundary_type = \"clipsrc\"\n)\n\n0...10...20...30...40...50...60...70...80...90...100 - done.\nReading layer `lines' from data source \n `/home/lucas/Documents/GitHub/rspatial-for-onehealth/data_cambodia/geofabrik_cambodia-latest.gpkg' \n using driver `GPKG'\nSimple feature collection with 18794 features and 12 fields\nGeometry type: MULTILINESTRING\nDimension: XY\nBounding box: xmin: 469524.2 ymin: 1263268 xmax: 503494.3 ymax: 1296780\nProjected CRS: WGS 84 / UTM zone 48N\n\nmf_map(x = roads)"
},
{
"objectID": "02-data_acquisition.html#geocoding",
"href": "02-data_acquisition.html#geocoding",
"title": "2 Data Acquisition",
"section": "2.4 Geocoding",
"text": "2.4 Geocoding\nServeral pakages alow you to geocode addresses. The package tidygeocoder (Cambon et al. 2021) allow the use of a large number of online geocoding sevices. The package banR (Gombin and Chevalier 2022), which is based on the National Address Base, is the particularly suitable for geocoding addresses in France.\n\n2.4.1 tidygeocoder\n\nlibrary(tidygeocoder)\ntest_adresses <- data.frame(\n address = c(\"Phnom Penh International Airport, Phnom Penh, Cambodia\",\n \"Khmer Soviet Friendship Hospital, Phnom Penh, Cambodia\"))\nplaces1 <- geocode(test_adresses, address)\nplaces1\n\n# A tibble: 2 × 3\n address lat long\n <chr> <dbl> <dbl>\n1 Phnom Penh International Airport, Phnom Penh, Cambodia 11.6 105.\n2 Khmer Soviet Friendship Hospital, Phnom Penh, Cambodia 11.5 105.\n\n\n\n\n\n\n\n\nWebsite by tidygeocoder :\ntidygeocoder\n\n\n\n\n\n2.4.2 banR (Base Adresse Nationale)\n\n# remotes::install_github(\"joelgombin/banR\")\nlibrary(banR)\nmes_adresses <- data.frame(\n address = c(\"19 rue Michel Bakounine, 29600 Morlaix, France\",\n \"2 Allee Emile Pouget, 920128 Boulogne-Billancourt\")\n)\nplaces2 <- geocode_tbl(tbl = mes_adresses, adresse = address)\nplaces2\n\n# A tibble: 2 × 17\n address latit…¹ longi…² resul…³ resul…⁴ resul…⁵ resul…⁶ resul…⁷ resul…⁸\n <chr> <dbl> <dbl> <chr> <dbl> <chr> <chr> <chr> <chr> \n1 19 rue Michel… 48.6 -3.82 19 Rue… 0.81 housen… 29151_… 19 Rue Mi…\n2 2 Allee Emile… 48.8 2.24 2 Allé… 0.83 housen… 92012_… 2 Allée …\n# … with 8 more variables: result_street <chr>, result_postcode <chr>,\n# result_city <chr>, result_context <chr>, result_citycode <chr>,\n# result_oldcitycode <chr>, result_oldcity <chr>, result_district <chr>, and\n# abbreviated variable names ¹latitude, ²longitude, ³result_label,\n# ⁴result_score, ⁵result_type, ⁶result_id, ⁷result_housenumber, ⁸result_name\n\n\n\n\n\n\n\n\nWebsite of banR :\nAn R client for the BAN API"
},
{
"objectID": "02-data_acquisition.html#digitization",
"href": "02-data_acquisition.html#digitization",
"title": "2 Data Acquisition",
"section": "2.5 Digitization",
"text": "2.5 Digitization\nThe package mapedit (Appelhans, Russell, and Busetto 2020) allows you to digitize base map directly in R. Although it can be practical in some cases, in package cannot replace the functionalities of a GIS for important digitization tasks.\n\n\n\nGif taken from mapedit website\n\n\n\n\n\n\nAgafonkin, Vladimir. 2015. “Leaflet Javascript Libary.”\n\n\nAppelhans, Tim, Florian Detsch, Christoph Reudenbach, and Stefan Woellauer. 2022. “Mapview: Interactive Viewing of Spatial Data in r.” https://CRAN.R-project.org/package=mapview.\n\n\nAppelhans, Tim, Kenton Russell, and Lorenzo Busetto. 2020. “Mapedit: Interactive Editing of Spatial Data in r.” https://CRAN.R-project.org/package=mapedit.\n\n\nCambon, Jesse, Diego Hernangómez, Christopher Belanger, and Daniel Possenriede. 2021. “Tidygeocoder: An r Package for Geocoding” 6: 3544. https://doi.org/10.21105/joss.03544.\n\n\nCheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2022. “Leaflet: Create Interactive Web Maps with the JavaScript ’Leaflet’ Library.” https://CRAN.R-project.org/package=leaflet.\n\n\nGilardi, Andrea, and Robin Lovelace. 2021. “Osmextract: Download and Import Open Street Map Data Extracts.” https://CRAN.R-project.org/package=osmextract.\n\n\nGiraud, Timothée. 2021. “Maptiles: Download and Display Map Tiles.” https://CRAN.R-project.org/package=maptiles.\n\n\nGombin, Joel, and Paul-Antoine Chevalier. 2022. “banR: R Client for the BAN API.”\n\n\nPadgham, Mark, Bob Rudis, Robin Lovelace, and Maëlle Salmon. 2017. “Osmdata” 2. https://doi.org/10.21105/joss.00305."
},
{
"objectID": "03-vector_data.html",
"href": "03-vector_data.html",
"title": "3 Vector Data",
"section": "",
"text": "The st_read() and st_write() 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 cambodia.gpkg geopackage file.\n\nlibrary(sf)\n\ndistrict = st_read(\"data_cambodia/cambodia.gpkg\", layer = \"district\") #import district data\n\nReading layer `district' from data source \n `/home/lucas/Documents/GitHub/rspatial-for-onehealth/data_cambodia/cambodia.gpkg' \n using driver `GPKG'\nSimple feature collection with 197 features and 10 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 211534.7 ymin: 1149105 xmax: 784612.1 ymax: 1625495\nProjected CRS: WGS 84 / UTM zone 48N\n\n\nThe following lines export the district object to a data folder in geopackage and shapefile format.\n\nst_write(obj = district, dsn = \"data_cambodia/district.gpkg\", delete_layer = TRUE)\n\nDeleting layer `district' using driver `GPKG'\nWriting layer `district' to data source \n `data_cambodia/district.gpkg' using driver `GPKG'\nWriting 197 features with 10 fields and geometry type Multi Polygon.\n\nst_write(obj = district, \"data_cambodia/district.shp\", layer_options = \"ENCODING=UTF-8\", delete_layer = TRUE)\n\nDeleting layer `district' using driver `ESRI Shapefile'\nWriting layer `district' to data source \n `data_cambodia/district.shp' using driver `ESRI Shapefile'\noptions: ENCODING=UTF-8 \nWriting 197 features with 10 fields and geometry type Multi Polygon."
},
{
"objectID": "03-vector_data.html#display",
"href": "03-vector_data.html#display",
"title": "3 Vector Data",
"section": "3.2 Display",
"text": "3.2 Display\nPreview of the variables via the function head() and plot().\n\nhead(district)\n\nSimple feature collection with 6 features and 10 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 300266.9 ymin: 1180566 xmax: 767313.9 ymax: 1563861\nProjected CRS: WGS 84 / UTM zone 48N\n ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP Area.Km2.\n1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416 1067.8638\n2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708 837.7064\n3 Angk Snuol KH0808 Kandal KH08 45436 47141 92577 183.9050\n4 Angkor Borei KH2101 Takeo KH21 26306 27168 53474 301.0502\n5 Angkor Chey KH0701 Kampot KH07 42448 44865 87313 316.7576\n6 Angkor Chum KH1701 Siemreap KH17 34269 34576 68845 478.6988\n Status DENs geom\n1 <4500km2 79.98773 MULTIPOLYGON (((306568.1 14...\n2 <4500km2 17.55747 MULTIPOLYGON (((751459.2 15...\n3 <4500km2 503.39580 MULTIPOLYGON (((471954.3 12...\n4 <4500km2 177.62485 MULTIPOLYGON (((490048.2 12...\n5 <4500km2 275.64610 MULTIPOLYGON (((462702.2 12...\n6 <4500km2 143.81696 MULTIPOLYGON (((363642.5 15...\n\nplot(district)\n\n\n\n\nfor Geometry display only.\n\nplot(st_geometry(district))"
},
{
"objectID": "03-vector_data.html#coordinate-systems",
"href": "03-vector_data.html#coordinate-systems",
"title": "3 Vector Data",
"section": "3.3 Coordinate systems",
"text": "3.3 Coordinate systems\n\n3.3.1 Look up the coordinate system of an object\nThe function st_crs() makes it possible to consult the system of coordinates used and object sf.\n\nst_crs(district)\n\nCoordinate Reference System:\n User input: WGS 84 / UTM zone 48N \n wkt:\nPROJCRS[\"WGS 84 / UTM zone 48N\",\n BASEGEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4326]],\n CONVERSION[\"UTM zone 48N\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",105,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",500000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",0,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"(E)\",east,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"(N)\",north,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"Engineering survey, topographic mapping.\"],\n 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.\"],\n BBOX[0,102,84,108]],\n ID[\"EPSG\",32648]]\n\n\n\n\n3.3.2 Changing the coordinate system of an object\nThe function st_transform() allows to change the coordinate system of an sf object, to re-project it.\n\nplot(st_geometry(district))\ntitle(\"WGS 84 / UTM zone 48N\")\n\n\n\ndist_reproj <- st_transform(district, \"epsg:4326\")\nplot(st_geometry(dist_reproj))\ntitle(\"WGS84\")\n\n\n\n\nThe Spatial Reference site provides reference for a large number of coordinate systems."
},
{
"objectID": "03-vector_data.html#selection-by-attributes",
"href": "03-vector_data.html#selection-by-attributes",
"title": "3 Vector Data",
"section": "3.4 Selection by attributes",
"text": "3.4 Selection by attributes\nThe object sf are data.frame, so you can select their rows and columns in the same way as data.frame.\n\n# row Selection\ndistrict[1:2, ]\n\nSimple feature collection with 2 features and 10 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 300266.9 ymin: 1449408 xmax: 767313.9 ymax: 1563861\nProjected CRS: WGS 84 / UTM zone 48N\n ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP Area.Km2.\n1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416 1067.8638\n2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708 837.7064\n Status DENs geom\n1 <4500km2 79.98773 MULTIPOLYGON (((306568.1 14...\n2 <4500km2 17.55747 MULTIPOLYGON (((751459.2 15...\n\ndistrict[district$ADM1_EN == \"Phnom Penh\", ]\n\nSimple feature collection with 12 features and 10 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 468677.5 ymin: 1262590 xmax: 505351.9 ymax: 1297419\nProjected CRS: WGS 84 / UTM zone 48N\nFirst 10 features:\n ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP\n29 Chamkar Mon KH1201 Phnom Penh KH12 52278 54478 106756\n31 Chbar Ampov KH1212 Phnom Penh KH12 64816 68243 133059\n43 Chraoy Chongvar KH1210 Phnom Penh KH12 30920 31087 62007\n48 Dangkao KH1205 Phnom Penh KH12 46999 48525 95524\n50 Doun Penh KH1202 Phnom Penh KH12 33844 36471 70315\n93 Mean Chey KH1206 Phnom Penh KH12 68381 70366 138747\n117 Praek Pnov KH1211 Phnom Penh KH12 27566 27698 55264\n118 Prampir Meakkakra KH1203 Phnom Penh KH12 31091 33687 64778\n133 Pur SenChey KH1209 Phnom Penh KH12 95050 109297 204347\n141 Russey Keo KH1207 Phnom Penh KH12 67357 68419 135776\n Area.Km2. Status DENs geom\n29 11.049600 <4500km2 9661.5265 MULTIPOLYGON (((494709.4 12...\n31 86.780498 <4500km2 1533.2823 MULTIPOLYGON (((498855.3 12...\n43 85.609156 <4500km2 724.3034 MULTIPOLYGON (((491161.3 12...\n48 113.774833 <4500km2 839.5881 MULTIPOLYGON (((489191.1 12...\n50 7.734808 <4500km2 9090.7234 MULTIPOLYGON (((492447.1 12...\n93 28.998026 <4500km2 4784.7051 MULTIPOLYGON (((491068.2 12...\n117 115.384300 <4500km2 478.9560 MULTIPOLYGON (((481483.3 12...\n118 2.224892 <4500km2 29115.1253 MULTIPOLYGON (((491067.6 12...\n133 148.357984 <4500km2 1377.3913 MULTIPOLYGON (((479078.8 12...\n141 23.381517 <4500km2 5806.9800 MULTIPOLYGON (((490264.8 12...\n\n# column selection\ndistrict[district$ADM1_EN == \"Phnom Penh\", 1:4] \n\nSimple feature collection with 12 features and 4 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 468677.5 ymin: 1262590 xmax: 505351.9 ymax: 1297419\nProjected CRS: WGS 84 / UTM zone 48N\nFirst 10 features:\n ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE\n29 Chamkar Mon KH1201 Phnom Penh KH12\n31 Chbar Ampov KH1212 Phnom Penh KH12\n43 Chraoy Chongvar KH1210 Phnom Penh KH12\n48 Dangkao KH1205 Phnom Penh KH12\n50 Doun Penh KH1202 Phnom Penh KH12\n93 Mean Chey KH1206 Phnom Penh KH12\n117 Praek Pnov KH1211 Phnom Penh KH12\n118 Prampir Meakkakra KH1203 Phnom Penh KH12\n133 Pur SenChey KH1209 Phnom Penh KH12\n141 Russey Keo KH1207 Phnom Penh KH12\n geom\n29 MULTIPOLYGON (((494709.4 12...\n31 MULTIPOLYGON (((498855.3 12...\n43 MULTIPOLYGON (((491161.3 12...\n48 MULTIPOLYGON (((489191.1 12...\n50 MULTIPOLYGON (((492447.1 12...\n93 MULTIPOLYGON (((491068.2 12...\n117 MULTIPOLYGON (((481483.3 12...\n118 MULTIPOLYGON (((491067.6 12...\n133 MULTIPOLYGON (((479078.8 12...\n141 MULTIPOLYGON (((490264.8 12..."
},
{
"objectID": "03-vector_data.html#spatial-selection",
"href": "03-vector_data.html#spatial-selection",
"title": "3 Vector Data",
"section": "3.5 Spatial selection",
"text": "3.5 Spatial selection\n\n3.5.1 Intersections\nSelection of roads that are intersecting dangkao district\n\nroad <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"road\", quiet = TRUE)\ndangkao <- district[district$ADM2_EN == \"Dangkao\", ]\ninter <- st_intersects(x = road, y = dangkao, sparse = FALSE)\nhead(inter)\n\n [,1]\n[1,] TRUE\n[2,] TRUE\n[3,] TRUE\n[4,] TRUE\n[5,] TRUE\n[6,] TRUE\n\ndim(inter)\n\n[1] 6 1\n\n\nThe inter object is a matrix which indicates for each of element of the road object (6 elements) whether it intersects each elements the dangkao object (1 element). The dimension of the matrix is therefore indeed 6 rows * 1 column. Note the use of the parameter sparse = FALSE here. It is then possible to create a column from this object:\n\nroad$intersect_dangkao <- inter\nplot(st_geometry(dangkao), col = \"lightblue\")\nplot(st_geometry(road), add = TRUE)\nplot(st_geometry(road[road$intersect_dangkao, ]),\n col = \"tomato\", lwd = 1.5, add = TRUE)\n\n\n\n\n\n3.5.1.1 Difference between sparse = TRUE and sparse = FALSE\n\n\n\n\n\n\nsparse = TRUE\n\n\ninter <- st_intersects(x = grid, y = pt, sparse = TRUE)\ninter\n\nSparse geometry binary predicate list of length 4, where the predicate\nwas `intersects'\n 1: (empty)\n 2: 6, 7\n 3: 1, 4\n 4: 2, 3, 5, 8\n\n\n\nsparse = FALSE\n\n\ninter <- st_intersects(x = grid, y = pt, sparse = FALSE)\nrownames(inter) <- grid$id\ncolnames(inter) <- pt$id\ninter\n\n a b c d e f g h\n1 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE\n2 FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE\n3 TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE\n4 FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE\n\n\n\n\n\n3.5.2 Contains / Within\nSelection of roads contained in the municipality of Dangkao. The function st_within() works like the function st_intersects()\n\nroad$within_dangkao <- st_within(road, dangkao, sparse = FALSE)\nplot(st_geometry(dangkao), col = \"lightblue\")\nplot(st_geometry(road), add = TRUE)\n\n\n\nplot(st_geometry(road[road$within_dangkao, ]), col = \"tomato\",\n lwd = 2, add = TRUE)"
},
{
"objectID": "03-vector_data.html#operation-of-geometries",
"href": "03-vector_data.html#operation-of-geometries",
"title": "3 Vector Data",
"section": "3.6 Operation of geometries",
"text": "3.6 Operation of geometries\n\n3.6.1 Extract centroids\n\ndist_c <- st_centroid(district)\nplot(st_geometry(district))\nplot(st_geometry(dist_c), add = TRUE, cex = 1.2, col = \"red\", pch = 20)\n\n\n\n\n\n\n3.6.2 Aggregate polygons\n\ncambodia_dist <- st_union(district) \nplot(st_geometry(district), col = \"lightblue\")\nplot(st_geometry(cambodia_dist), add = TRUE, lwd = 2, border = \"red\")\n\n\n\n\n\n\n3.6.3 Aggregate polygons based on a variable\n\ndist_union <- aggregate(x = district[,c(\"T_POP\")],\n by = list(STATUT = district$Status),\n FUN = \"sum\")\nplot(dist_union)\n\n\n\n\n\n\n3.6.4 Create a buffer zone\n\ndangkao_buffer <- st_buffer(x = dangkao, dist = 1000)\nplot(st_geometry(dangkao_buffer), col = \"#E8DAEF\", lwd=2, border = \"#6C3483\")\nplot(st_geometry(dangkao), add = TRUE, lwd = 2)\n\n\n\n\n\n\n3.6.5 Making an intersection\nBy using the function st_intersection() we will cut one layer by another.\n\nlibrary(magrittr)\n# creation of a buffer zone around the centroid of the municipality of Dangkao district\n# using the pipe\nzone <- st_geometry(dangkao) %>%\n st_centroid() %>%\n st_buffer(30000)\nplot(st_geometry(district))\nplot(zone, border = \"#F06292\", lwd = 2, add = TRUE)\n\n\n\ndist_z <- st_intersection(x = district, y = zone)\nplot(st_geometry(district))\nplot(st_geometry(dist_z), col=\"#AF7AC5\", border=\"#F9E79F\", add=T)\n\n\n\nplot(st_geometry(dist_z))\n\n\n\n\n\n\n3.6.6 Create regular grid\nThe function st_make_grid() allows you to create regular grid. The function produce and object sfc, you must then use the function st_sf() to transform the object sfc into and object sf. During this transformation we add here a column of unique identifiers.\n\ngrid <- st_make_grid(x = district, cellsize = 10000)\ngrid <- st_sf(ID = 1:length(grid), geom = grid)\n\nplot(st_geometry(grid), col = \"grey\", border = \"white\")\nplot(st_geometry(district), border = \"grey50\", add = TRUE)\n\n\n\n\n\n\n3.6.7 Counting points in a polygon (in a grid tile)\n\n# selection of grid tiles that intersect the district\n\ninter <- st_intersects(grid, cambodia_dist, sparse = FALSE)\ngrid <- grid[inter, ]\n\ncase_cambodia <- st_read(\"data_cambodia/cambodia.gpkg\", layer = \"cases\" , quiet = TRUE)\nplot(st_geometry(grid), col = \"grey\", border = \"white\")\nplot(st_geometry(case_cambodia), pch = 20, col = \"red\", add = TRUE, cex = 0.8)\n\n\n\ninter <- st_intersects(grid, case_cambodia, sparse = TRUE)\nlength(inter)\n\n[1] 1964\n\n\nHere we use the argument sparse = TRUE. The inter object is a list the length of the grid and each item in the list contain the index of the object items of cases and grid intersection.\nFor example grid tile 35th intersect with four cases 97, 138, 189, 522, 624, 696\n\ninter[35]\n\n[[1]]\n[1] 97 138 189 522 624 696\n\nplot(st_geometry(grid[35, ]))\nplot(st_geometry(case_cambodia), add = T)\nplot(st_geometry(case_cambodia[c(97, 138, 189, 522, 624, 696), ]), \n col = \"red\", pch = 19, add = TRUE)\n\n\n\n\nTo count number of case, simply go to the list and report length of the elements.\n\ngrid$nb_case <- sapply(X = inter, FUN = length) # create 'nb_case' column to store number of health centers in each grid tile \nplot(grid[\"nb_case\"])\n\n\n\n\n\n\n3.6.8 Aggregate point values into polygons\nIn this example we import a csv file that contain data from a population grid. Once import we transform it data.frame into an object sf.\nThe objective is to aggregate the values id these points (the population contained in the “DENs” field) in the municipalities of the district.\n\npp_pop_raw <- read.csv(\"data_cambodia/pp_pop_dens.csv\") # import file\npp_pop_raw$id <- 1:nrow(pp_pop_raw) # adding a unique identifier\npp_pop <- st_as_sf(pp_pop_raw, coords = c(\"X\", \"Y\"), crs = 32648) # Transform into object sf\npp_pop <- st_transform(pp_pop, st_crs(district)) # Transform projection\ninter <- st_intersection(pp_pop, district) # Intersection\ninter\n\nSimple feature collection with 1295 features and 12 fields\nGeometry type: POINT\nDimension: XY\nBounding box: xmin: 469177.5 ymin: 1263090 xmax: 505177.5 ymax: 1297090\nProjected CRS: WGS 84 / UTM zone 48N\nFirst 10 features:\n DENs id ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP\n149 NA 149 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n150 NA 150 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n151 NA 151 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n186 NA 186 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n187 NA 187 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n188 NA 188 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n223 NA 223 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n224 NA 224 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n225 NA 225 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n226 3.400075 226 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n Area.Km2. Status DENs.1 geometry\n149 183.905 <4500km2 503.3958 POINT (469177.5 1267090)\n150 183.905 <4500km2 503.3958 POINT (470177.5 1267090)\n151 183.905 <4500km2 503.3958 POINT (471177.5 1267090)\n186 183.905 <4500km2 503.3958 POINT (469177.5 1268090)\n187 183.905 <4500km2 503.3958 POINT (470177.5 1268090)\n188 183.905 <4500km2 503.3958 POINT (471177.5 1268090)\n223 183.905 <4500km2 503.3958 POINT (469177.5 1269090)\n224 183.905 <4500km2 503.3958 POINT (470177.5 1269090)\n225 183.905 <4500km2 503.3958 POINT (471177.5 1269090)\n226 183.905 <4500km2 503.3958 POINT (472177.5 1269090)\n\n\nBy using the function st_intersection() we add to each point of the grid all the information on the municipality in which it is located.\nWe can then use the function aggregate() to aggregate the population by municipalities.\n\nresultat <- aggregate(x = list(pop_from_grid = inter$DENs), \n by = list(ADM2_EN = inter$ADM2_EN), \n FUN = \"sum\")\nhead(resultat)\n\n ADM2_EN pop_from_grid\n1 Angk Snuol NA\n2 Chamkar Mon 10492.7159\n3 Chbar Ampov 1593.9593\n4 Chraoy Chongvar 1434.1785\n5 Dangkao 942.3595\n6 Doun Penh 10781.8026\n\n\nWe can then create a new object with this result.\n\ndist_result <- merge(district, resultat, by = \"ADM2_EN\", all.x = TRUE)\ndist_result\n\nSimple feature collection with 197 features and 11 fields\nGeometry type: MULTIPOLYGON\nDimension: XY\nBounding box: xmin: 211534.7 ymin: 1149105 xmax: 784612.1 ymax: 1625495\nProjected CRS: WGS 84 / UTM zone 48N\nFirst 10 features:\n ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE Male Female T_POP\n1 Aek Phnum KH0205 Battambang KH02 41500 43916 85416\n2 Andoung Meas KH1601 Ratanak Kiri KH16 7336 7372 14708\n3 Angk Snuol KH0808 Kandal KH08 45436 47141 92577\n4 Angkor Borei KH2101 Takeo KH21 26306 27168 53474\n5 Angkor Chey KH0701 Kampot KH07 42448 44865 87313\n6 Angkor Chum KH1701 Siemreap KH17 34269 34576 68845\n7 Angkor Thum KH1702 Siemreap KH17 13802 14392 28194\n8 Anlong Veaeng KH2201 Oddar Meanchey KH22 24122 23288 47410\n9 Aoral KH0504 Kampong Speu KH05 19874 19956 39830\n10 Ba Phnum KH1401 Prey Veng KH14 46562 49852 96414\n Area.Km2. Status DENs pop_from_grid geometry\n1 1067.8638 <4500km2 79.98773 NA MULTIPOLYGON (((306568.1 14...\n2 837.7064 <4500km2 17.55747 NA MULTIPOLYGON (((751459.2 15...\n3 183.9050 <4500km2 503.39580 NA MULTIPOLYGON (((471954.3 12...\n4 301.0502 <4500km2 177.62485 NA MULTIPOLYGON (((490048.2 12...\n5 316.7576 <4500km2 275.64610 NA MULTIPOLYGON (((462702.2 12...\n6 478.6988 <4500km2 143.81696 NA MULTIPOLYGON (((363642.5 15...\n7 357.8890 <4500km2 78.77862 NA MULTIPOLYGON (((376584.4 15...\n8 1533.5702 <4500km2 30.91479 NA MULTIPOLYGON (((404936.4 15...\n9 2381.7084 <4500km2 16.72329 NA MULTIPOLYGON (((414000.6 13...\n10 342.3439 <4500km2 281.62910 NA MULTIPOLYGON (((545045.4 12..."
},
{
"objectID": "03-vector_data.html#measurements",
"href": "03-vector_data.html#measurements",
"title": "3 Vector Data",
"section": "3.7 Measurements",
"text": "3.7 Measurements\n\n3.7.1 Create a distance matrix\nIf the dataset’s projection system is specified, the distance are expressed in the projection measurement unit (most often in meter)\n\nmat <- st_distance(x = dist_c, y = dist_c)\nmat[1:5,1:5]\n\nUnits: [m]\n [,1] [,2] [,3] [,4] [,5]\n[1,] 0.0 425993.7 232592.12 298254.12 299106.92\n[2,] 425993.7 0.0 386367.88 414428.82 452431.87\n[3,] 232592.1 386367.9 0.00 67060.05 82853.88\n[4,] 298254.1 414428.8 67060.05 0.00 40553.15\n[5,] 299106.9 452431.9 82853.88 40553.15 0.00\n\n\n\n\n3.7.2 Calculate routes\n The package osrm (R-osrm?) acts as an interface R and the OSRM (luxen-vetter-2011?). 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 it is strongly recommended to use your own instance of OSRM (with Docker).\n\n3.7.2.1 Calculate a route\nThe fonction osrmRoute() allows you to calculate routes.\n\nlibrary(sf)\nlibrary(osrm)\nlibrary(maptiles)\ndistrict <- st_read(\"data_cambodia/cambodia.gpkg\",layer = \"district\", quiet = TRUE)\ndistrict <- st_transform(district, 32648)\n\nodongk <- district[district$ADM2_PCODE == \"KH0505\", ] # Itinerary between Odongk district and Toul Kouk\ntakmau <- district[district$ADM2_PCODE == \"KH0811\",]\nroute <- osrmRoute(src = odongk, \n dst = takmau, \n returnclass = \"sf\")\nosm <- get_tiles(route, crop = TRUE)\nplot_tiles(osm)\nplot(st_geometry(route), col = \"#b23a5f\", lwd = 6, add = T)\nplot(st_geometry(route), col = \"#eee0e5\", lwd = 1, add = T)\n\n\n\n\n\n\n3.7.2.2 Calculation of a time matrix\nThe function osrmTable() makes it possible to calculate matrices of distances or times by road.\nIn this example we calculate a time matrix between 2 addresses and health centers in Phnom Penh on foot.\n\nlibrary(sf)\nlibrary(tidygeocoder)\nhospital <- st_read(\"data_cambodia/cambodia.gpkg\",layer= \"hospital\", quiet = TRUE)\n\nhospital_pp <- hospital[hospital$PCODE == \"12\", ] # Selection of health centers in Phnom Penh\n\nadresses <- data.frame(adr = c(\"Royal Palace Park, Phnom Penh Phnom, Cambodia\",\n \"Wat Phnom Daun Penh, Phnom Penh, Cambodia\")) # Geocoding of 2 addresses in Phnom Penh\n\nplaces <- tidygeocoder::geocode(.tbl = adresses,address = adr)\nplaces\n\n# A tibble: 2 × 3\n adr lat long\n <chr> <dbl> <dbl>\n1 Royal Palace Park, Phnom Penh Phnom, Cambodia 11.6 105.\n2 Wat Phnom Daun Penh, Phnom Penh, Cambodia 11.6 105.\n\n# Calculation of the distance matrix between the 2 addresses and the health center in Phnom Penh\n\ncal_mat <- osrmTable(src = places[,c(1,3,2)], \n dst = hospital_pp, \n osrm.profile = \"foot\")\n\ncal_mat$durations[1:2, 1:5]\n\n 684 685 686 687 691\nRoyal Palace Park, Phnom Penh Phnom, Cambodia 56.1 71.9 64.2 40.4 76.5\nWat Phnom Daun Penh, Phnom Penh, Cambodia 60.1 80.5 43.8 32.8 55.6\n\n# Which address has better accessibility to health center in Phnom Penh?\n\nboxplot(t(cal_mat$durations[,]), cex.axis = 0.7)"
}
]