2  Data Acquisition

2.1 Import from lat / long file

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.

library(sf)
place_sf <- st_as_sf(read.csv("data_cambodia/adress.csv"), coords = c("long", "lat"), crs = 4326)
place_sf
Simple feature collection with 2 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 104.8443 ymin: 11.54366 xmax: 104.9047 ymax: 11.55349
Geodetic CRS:  WGS 84
                                                 address
1 Phnom Penh International Airport, Phnom Penh, Cambodia
2 Khmer Soviet Friendship Hospital, Phnom Penh, Cambodia
                   geometry
1 POINT (104.8443 11.55349)
2 POINT (104.9047 11.54366)

To crate a sf POINT type object with only one pair of coordinate (WGS84, longitude=0.5, latitude = 45.5) :

library(sf)
test_point <- st_as_sf(data.frame(x = 0.5, y = 45.5), coords = c("x", "y"), crs = 4326)
test_point
Simple feature collection with 1 feature and 0 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 0.5 ymin: 45.5 xmax: 0.5 ymax: 45.5
Geodetic CRS:  WGS 84
          geometry
1 POINT (0.5 45.5)

We can display this object sf on an OpenStreetMap basesmap with the package maptiles maptiles (Giraud 2021).

library(maptiles)
osm <- get_tiles(x = place_sf, zoom = 12)
plot_tiles(osm)
plot(st_geometry(place_sf), pch = 2, cex = 2, col = "red", add = TRUE)

2.2 Online databases

2.3 OpenStreetMap

OpenStreetMap (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.

Terms of use

OpenStreetMap 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. (…)

Contributors

(…) Our contributors incloude enthusiastic mapmakers, GIS professional, engineers running OSM servers, humanitarians mapping disaster-stricken areas and many mmore.(…)

2.3.1 Display and interactive map

The 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).

2.3.1.1 leaflet

leaflet uses the javascript library Leaflet (Agafonkin 2015) to create interactive maps.

library(sf)
library(leaflet)

district <- st_read("data_cambodia/cambodia.gpkg", layer = "district", quiet = TRUE)
hospital <- st_read("data_cambodia/cambodia.gpkg", layer = "hospital", quiet = TRUE)


banan <- district[district$ADM2_PCODE == "KH0201", ]     #Select one district (Banan district: KH0201)
health_banan <- hospital[hospital$DCODE == "201", ]      #Select Health centers in Banan

banan <- st_transform(banan, 4326)                       #Transform coordinate system to WGS84
health_banan <- st_transform(health_banan, 4326)

banan_map <- leaflet(banan) %>%                          #Create interactive map
  addTiles() %>%
  addPolygons() %>%
  addMarkers(data = health_banan)
banan_map

Website of leaflet
Leaflet for R

2.3.1.2 mapview

mapview relies on leaflet to create interactive maps, its use is easier and its documentation is a bit dense.

library(mapview)
mapview(banan) + mapview(health_banan)

Website of mapview
mapview

2.3.2 Import basemaps

The package maptiles (Giraud 2021) allows downlaoding and displaying raster basemaps.
The function get_tiles() allow you to download OSM background maps and the function plot_tiles() allows to display them.
Renders are better if the input data used the same coordinate system as the tiles (EPSG:3857).

library(sf)
library(maptiles)
district <- st_read("data_cambodia/cambodia.gpkg", layer = "district", quiet = TRUE)
district <- st_transform(district, 3857)
osm_tiles <- get_tiles(x = district, zoom = 10, crop = TRUE)
plot_tiles(osm_tiles)
plot(st_geometry(district), border = "grey20", lwd = .7, add = TRUE)
mtext(side = 1, line = -2, text = get_credit("OpenStreetMap"), col="tomato")