-
-
Notifications
You must be signed in to change notification settings - Fork 591
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
Conversation
Awesome! One thing: could you use |
@Nowosad Yes! Now it looks a lot cleaner, with no warnings and data conversions. Attaching a new reprex. library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(terra)
#> terra version 1.4.20
# Fetch the DEM data for Spain
spain_dem = geodata::elevation_30s(country = "Spain", path = ".", mask = FALSE)
# 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") Created on 2021-11-27 by the reprex package (v2.0.1) |
I also removed unnecessary spaces at the beginning of the lines. |
Great! |
@Nowosad I like both the formulation and the solution for E8 :) |
Thanks for the suggestions! |
Many thanks 🙏 |
The last exercise in chapter 4 does not have a solution at the moment. In e1c41fd @Nowosad commented it out with a to-do of improving/replacing/modifying it. I think it's a good exercise, so I added a solution to the first part, which asks to calculate distance to the coast for Spain.
I removed the second part that asks to calculate weighted distance, at least for now. I could not figure out how to complete this from the text of the chapter and the documentation of
terra::distance
. I am more than willing to explore it further if you can guide me in the right direction :)I also include a reproducible example of the code I added as a solution.
Created on 2021-11-27 by the reprex package (v2.0.1)