library(sf) library(terra)
E1. List and describe three types of vector, raster, and geodatabase formats.
```{asis 08-ex-e0-asis} Vector formats: Shapefile (old format supported by many programs), GeoPackage (more recent format with better support of attribute data) and GeoJSON (common format for web mapping).
Raster formats: GeoTiff, Arc ASCII, ERDAS Imagine (IMG).
Database formats: PostGIS, SQLite, FileGDB.
E2. Name at least two differences between the **sf** functions `read_sf()` and `st_read()`. ```{asis 08-ex-e2-asis} `read_sf()` is simply a 'wrapper' around `st_read()`, meaning that it calls `st_read()` behind the scenes. The differences shown in the output of the `read_sf` are `quiet = TRUE`, `stringsAsFactors = FALSE`, and `as_tibble = TRUE`: - `read_sf()` outputs are `quiet` by default, meaning less information printed to the console. - `read_sf()` outputs are tibbles by default, meaning that they are data frames with some additional features. - `read_sf()` does not convert strings to factors by default. The differences can be seen by running the following commands `nc = st_read(system.file("shape/nc.shp", package="sf"))` and `nc = read_sf(system.file("shape/nc.shp", package="sf"))` from the function's help (`?st_read`).
read_sf nc = st_read(system.file("shape/nc.shp", package="sf")) nc = read_sf(system.file("shape/nc.shp", package="sf"))
E3. Read the cycle_hire_xy.csv
file from the spData package as a spatial object (Hint: it is located in the misc
folder).
What is a geometry type of the loaded object?
c_h = read.csv(system.file("misc/cycle_hire_xy.csv", package = "spData")) |> st_as_sf(coords = c("X", "Y")) c_h
E4. Download the borders of Germany using rnaturalearth, and create a new object called germany_borders
.
Write this new object to a file of the GeoPackage format.
library(rnaturalearth) germany_borders = ne_countries(country = "Germany", returnclass = "sf") plot(germany_borders) st_write(germany_borders, "germany_borders.gpkg")
E5. Download the global monthly minimum temperature with a spatial resolution of 5 minutes using the geodata package.
Extract the June values, and save them to a file named tmin_june.tif
file (hint: use terra::subset()
).
library(geodata) gmmt = worldclim_global(var = "tmin", res = 5, path = tempdir()) names(gmmt) plot(gmmt) gmmt_june = terra::subset(gmmt, "wc2.1_5m_tmin_06") plot(gmmt_june) writeRaster(gmmt_june, "tmin_june.tif")
E6. Create a static map of Germany's borders, and save it to a PNG file.
png(filename = "germany.png", width = 350, height = 500) plot(st_geometry(germany_borders), axes = TRUE, graticule = TRUE) dev.off()
E7. Create an interactive map using data from the cycle_hire_xy.csv
file.
Export this map to a file called cycle_hire.html
.
library(mapview) mapview_obj = mapview(c_h, zcol = "nbikes", legend = TRUE) mapshot(mapview_obj, file = "cycle_hire.html")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.