-
-
Notifications
You must be signed in to change notification settings - Fork 585
/
_08-ex.Rmd
82 lines (62 loc) · 3.02 KB
/
_08-ex.Rmd
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
```{r 08-ex-e0, message=FALSE}
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`).
```
```{r 08-ex-e2}
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?
```{r 08-ex-e3}
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.
```{r 08-ex-e4}
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()`).
```{r 08-ex-e5}
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.
```{r 08-ex-e6}
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`.
```{r 08-ex-e7, eval=FALSE}
library(mapview)
mapview_obj = mapview(c_h, zcol = "nbikes", legend = TRUE)
mapshot(mapview_obj, file = "cycle_hire.html")
```