Skip to content

Commit 7136b12

Browse files
committed
Add new bonus exercise #766
1 parent 54e1f52 commit 7136b12

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Diff for: _04-ex.Rmd

+25-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The starting point of this exercise is to create an object representing Colorado
7373

7474
- 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 `[`).
7575
- 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+
- Bonus: create a straight line from the centroid of the District of Columbia near the East coast to the centroid of California near the West coast of the USA (hint: functions `st_centroid()`, `st_union()` and `st_cast()` described in Chapter 5 may help) and identify which states this long East-West line crosses.
7677

7778
```{r 04-ex-4-1}
7879
colorado = us_states[us_states$NAME == "Colorado", ]
@@ -110,10 +111,24 @@ plot(touches_colorado$geometry, col = "grey", add = TRUE)
110111
```
111112

112113

113-
E4. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
114+
```{r 04-ex-4-5}
115+
washington_to_cali = us_states %>%
116+
filter(grepl(pattern = "Columbia|Cali", x = NAME)) %>%
117+
st_centroid() %>%
118+
st_union() %>%
119+
st_cast("LINESTRING")
120+
states_crossed = us_states[washington_to_cali, , op = st_crosses]
121+
states_crossed$NAME
122+
plot(us_states$geometry, main = "States crossed by a straight line\n from the District of Columbia to central California")
123+
plot(states_crossed$geometry, col = "grey", add = TRUE)
124+
plot(washington_to_cali, add = TRUE)
125+
```
126+
127+
128+
E5. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
114129
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.
115130

116-
```{r 04-ex-e4}
131+
```{r 04-ex-e5}
117132
library(terra)
118133
dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))
119134
ndvi = rast(system.file("raster/ndvi.tif", package = "spDataLarge"))
@@ -128,11 +143,11 @@ plot(dem_reclass)
128143
zonal(c(dem, ndvi), dem_reclass, fun = "mean")
129144
```
130145

131-
E5. Apply a line detection filter to `rast(system.file("ex/logo.tif", package = "terra"))`.
146+
E6. Apply a line detection filter to `rast(system.file("ex/logo.tif", package = "terra"))`.
132147
Plot the result.
133148
Hint: Read `?terra::focal()`.
134149

135-
```{r 04-ex-e5}
150+
```{r 04-ex-e6}
136151
# from the focal help page (?terra::focal()):
137152
# Laplacian filter: filter=matrix(c(0,1,0,1,-4,1,0,1,0), nrow=3)
138153
# Sobel filters (for edge detection):
@@ -151,11 +166,11 @@ sobel_y = focal(r, w = filter_y)
151166
plot(sobel_y, col = c("black", "white"))
152167
```
153168

154-
E6. Calculate the Normalized Difference Water Index (NDWI; `(green - nir)/(green + nir)`) of a Landsat image.
169+
E7. Calculate the Normalized Difference Water Index (NDWI; `(green - nir)/(green + nir)`) of a Landsat image.
155170
Use the Landsat image provided by the **spDataLarge** package (`system.file("raster/landsat.tif", package = "spDataLarge")`).
156171
Also, calculate a correlation between NDVI and NDWI for this area.
157172

158-
```{r 04-ex-e6}
173+
```{r 04-ex-e7}
159174
file = system.file("raster/landsat.tif", package = "spDataLarge")
160175
multi_rast = rast(file)
161176
@@ -178,12 +193,12 @@ two_rasts_df = as.data.frame(two_rasts)
178193
cor(two_rasts_df$ndvi, two_rasts_df$ndwi)
179194
```
180195

181-
E7. A StackOverflow [post](https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances) shows how to compute distances to the nearest coastline using `raster::distance()`.
196+
E8. A StackOverflow [post](https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances) shows how to compute distances to the nearest coastline using `raster::distance()`.
182197
Try to do something similar but with `terra::distance()`: retrieve a digital elevation model of Spain, and compute a raster which represents distances to the coast across the country (hint: use `geodata::elevation_30s()`).
183198
Convert the resulting distances from meters to kilometers.
184199
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.
185200

186-
```{r 04-ex-e7}
201+
```{r 04-ex-e8}
187202
# Fetch the DEM data for Spain
188203
spain_dem = geodata::elevation_30s(country = "Spain", path = ".", mask = FALSE)
189204
@@ -206,10 +221,10 @@ distance_to_coast_km = distance_to_coast / 1000
206221
plot(distance_to_coast_km, main = "Distance to the coast (km)")
207222
```
208223

209-
E8. Try to modify the approach used in the above exercise by weighting the distance raster with the elevation raster; every 100 altitudinal meters should increase the distance to the coast by 10 km.
224+
E9. Try to modify the approach used in the above exercise by weighting the distance raster with the elevation raster; every 100 altitudinal meters should increase the distance to the coast by 10 km.
210225
Next, compute and visualize the difference between the raster created using the Euclidean distance (E7) and the raster weighted by elevation.
211226

212-
```{r 04-ex-e8}
227+
```{r 04-ex-e9}
213228
# now let's weight each 100 altitudinal meters by an additional distance of 10 km
214229
distance_to_coast_km2 = distance_to_coast_km + ((spain_dem / 100) * 10)
215230
# plot the result

0 commit comments

Comments
 (0)