8.8 Exercises

E1. List and describe three types of vector, raster, and geodatabase formats.

  • ESRI Shapefile .shp is a very popular proprietary vector format

  • KML .kml is a Google Earth open Vector format. When zipped it is .kmz

  • FlatGeobuf .fgb is a single file format for quick reading and streaming vector data

  • GeoTIFF .tif/.tiff is a popular open layered raster format

  • ArcASCII .asc is an open text format kind of like csv with a six line header

  • Geospatial .pdf is a raster and vector format

  • GeoPackage is an OGC standard for a SQLite container

E2. Name at least two differences between the sf functions read_sf() and st_read().


st_read(
  dsn,
  layer,
  ...,
  query = NA,
  options = NULL,
  quiet = FALSE,
  geometry_column = 1L,
  type = 0,
  promote_to_multi = TRUE,
  stringsAsFactors = sf_stringsAsFactors(),
  int64_as_string = FALSE,
  check_ring_dir = FALSE,
  fid_column_name = character(0),
  drivers = character(0),
  wkt_filter = character(0),
  optional = FALSE
)

read_sf(..., quiet = TRUE, stringsAsFactors = FALSE, as_tibble = TRUE)

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?

These are points

cycle_hire <- system.file("misc/cycle_hire_xy.csv", package = "spData")

cycle_hire_csv <- readr::read_csv(cycle_hire)
## Rows: 742 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): name, area
## dbl (5): X, Y, id, nbikes, nempty
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cycle_hire_csv
## # A tibble: 742 × 7
##          X     Y    id name               area             nbikes nempty
##      <dbl> <dbl> <dbl> <chr>              <chr>             <dbl>  <dbl>
##  1 -0.110   51.5     1 River Street       Clerkenwell           4     14
##  2 -0.198   51.5     2 Phillimore Gardens Kensington            2     34
##  3 -0.0846  51.5     3 Christopher Street Liverpool Street      0     32
##  4 -0.121   51.5     4 St. Chad's Street  King's Cross          4     19
##  5 -0.157   51.5     5 Sedding Street     Sloane Square        15     12
##  6 -0.144   51.5     6 Broadcasting House Marylebone            0     18
##  7 -0.168   51.5     7 Charlbert Street   St. John's Wood      15      0
##  8 -0.170   51.5     8 Lodge Road         St. John's Wood       5     13
##  9 -0.0964  51.5     9 New Globe Walk     Bankside              3     16
## 10 -0.0928  51.5    10 Park Street        Bankside              1     17
## # ℹ 732 more rows

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.

germany_borders <- rnaturalearth::ne_countries(country = "Germany")

write_sf(obj = germany_borders, dsn = "germany.gpkg")

E5. Download the global monthly minimum temperature with a spatial resolution of five minutes using the geodata package. Extract the June values, and save them to a file named tmin_june.tif file (hint: use terra::subset()).

worldclim_tmin <- geodata::worldclim_global("tmin", res = 5, path = tempdir())

worldclim_tmin_june <- terra::subset(worldclim_prec, subset = "wc2.1_5m_tmin_06")
              
terra::writeRaster(worldclim_tmin_june, "tmin_june.tif")

E6. Create a static map of Germany’s borders, and save it to a PNG file.

germany_borders <- rnaturalearth::ne_countries(country = "Germany")

png(filename = "germany.png", width = 500, height = 350)
plot(germany_borders)
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.

cycle_hire <- system.file("misc/cycle_hire_xy.csv", package = "spData")

cycle_hire_csv <- readr::read_csv(cycle_hire)

library(mapview)
mapview_obj <- mapview(cycle_hire_csv, xcol = "X", ycol = "Y", zcol = "nbikes", legend = TRUE)

mapview_obj

mapshot(mapview_obj, file = "cycle_hire.html")