Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify an example in ch4 and add a solution #683

Merged
merged 3 commits into from
Nov 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Modify an example in ch4 and add a solution
  • Loading branch information
iod-ine committed Nov 27, 2021
commit 1bbfc7b82cf88c186accc8b8d5f6480f26da675d
36 changes: 29 additions & 7 deletions _04-ex.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,33 @@ two_rasts_df = as.data.frame(two_rasts)
cor(two_rasts_df$ndvi, two_rasts_df$ndwi)
```

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()`.
Retrieve a digital elevation model of Spain, and compute a raster which represents distances to the coast across the country (hint: use `getData()`).
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.

<!--toDo:jn-->
<!--improve/replace/modify the following q-->
<!-- 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()`. -->
<!-- Retrieve a digital elevation model of Spain, and compute a raster which represents distances to the coast across the country (hint: use `getData()`). -->
<!-- Second, use a simple approach to weight the distance raster with elevation (other weighting approaches are possible, include flow direction and steepness); every 100 altitudinal meters should increase the distance to the coast by 10 km. -->
<!-- Finally, compute the difference between the raster using the Euclidean distance and the raster weighted by elevation. -->
<!-- Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation. -->
```{r}
# Fetch the DEM data for Spain
spain_dem = raster::getData("alt", country = "ESP", mask = FALSE)

# Convert the DEM to terra format
spain_dem = rast(spain_dem)

# Assign the correct CRS to the DEM: WGS 84
crs(spain_dem) = "EPSG:4326"

# Reduce the resolution by a factor of 20 to speed up calculations
spain_dem = aggregate(spain_dem, fact = 20)

# According to the documentation, terra::distance() will calculate distance
# for all cells that are NA to the nearest cell that are not NA. To calculate
# distance to the coast, we need a raster that has NA values over land and any
# other value over water
water_mask = is.na(spain_dem)
water_mask[water_mask == 0] = NA

# Use the distance() function on this mask to get distance to the coast
distance_to_coast = distance(water_mask)

# Plot the result
plot(distance_to_coast, main = "Distance to the coast")
```