Skip to content

Commit 54e1f52

Browse files
committed
Update exercises, close #766
1 parent 0c5c56f commit 54e1f52

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

Diff for: _04-ex.Rmd

+35-12
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,50 @@ nz_height_combined %>%
6666
na.omit()
6767
```
6868

69-
E4. To test your knowledge of spatial predicates:
69+
E4. Test your knowledge of spatial predicates by finding out and plotting how US states relate to each other and other spatial objects.
7070

71-
- Create an object representing Colorado state in the USA, e.g. with the command
72-
`colorado = us_states[us_states$NAME == "Colorado",]` (base R) or
73-
`colorado = us_states %>% filter(NAME == "Colorado")` (tidyverse).
74-
- Create a new object representing all the objects that intersect, in some way, with Colorado and plot the result.
75-
- Create another object representing all the objects that touch Colorado and plot the result.
71+
The starting point of this exercise is to create an object representing Colorado state in the USA. Do this with the command
72+
`colorado = us_states[us_states$NAME == "Colorado",]` (base R) or with with the `filter()` function (tidyverse) and plot the resulting object in the context of US states.
7673

77-
```{r 04-ex-4}
78-
plot(us_states$geometry)
79-
plot(Colorado$geometry, col = 2, add = TRUE)
74+
- Create a new object representing all the states that geographically intersect with Colorado and plot the result (hint: the most concise way to do this is with the subsetting method `[`).
75+
- Create another object representing all the objects that touch (have a shared boundary with) Colorado and plot the result (hint: remember you can use the argument `op = st_intersects` and other spatial relations during spatial subsetting operations in base R).
76+
77+
```{r 04-ex-4-1}
8078
colorado = us_states[us_states$NAME == "Colorado", ]
79+
plot(us_states$geometry)
80+
plot(colorado$geometry, col = "grey", add = TRUE)
81+
```
82+
83+
```{r 04-ex-4-2}
8184
intersects_with_colorado = us_states[colorado, , op = st_intersects]
85+
plot(us_states$geometry, main = "States that intersect with Colorado")
86+
plot(intersects_with_colorado$geometry, col = "grey", add = TRUE)
87+
```
88+
89+
```{r 04-ex-4-3}
90+
# Alternative but more verbose solutions
91+
# 2: With intermediate object, one list for each state
92+
sel_intersects_colorado = st_intersects(us_states, colorado)
93+
sel_intersects_colorado_list = lengths(sel_intersects_colorado) > 0
94+
intersects_with_colorado = us_states[sel_intersects_colorado_list, ]
95+
96+
# 3: With intermediate object, one index for each state
97+
sel_intersects_colorado2 = st_intersects(colorado, us_states)
98+
sel_intersects_colorado2
99+
us_states$NAME[unlist(sel_intersects_colorado2)]
100+
101+
# 4: With tidyverse
102+
us_states %>%
103+
st_filter(y = colorado, .predicate = st_intersects)
104+
```
105+
106+
```{r 04-ex-4-4}
82107
touches_colorado = us_states[colorado, , op = st_touches]
83-
plot(us_states$geometry)
108+
plot(us_states$geometry, main = "States that touch Colorado")
84109
plot(touches_colorado$geometry, col = "grey", add = TRUE)
85110
```
86111

87112

88-
# What are the neighbouring states of Colorado?
89-
90113
E4. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
91114
Secondly, read the NDVI raster (`ndvi = rast(system.file("raster/ndvi.tif", package = "spDataLarge"))`) and compute the mean NDVI and the mean elevation for each altitudinal class.
92115

0 commit comments

Comments
 (0)