You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-spatial-operations.md
+27-7
Original file line number
Diff line number
Diff line change
@@ -976,8 +976,7 @@ For a more detailed introduction to remote sensing with R, see @wegmann_remote_2
976
976
```r
977
977
library(sf)
978
978
library(dplyr)
979
-
data(nz, package="spData")
980
-
data(nz_height, package="spData")
979
+
library(spData)
981
980
```
982
981
983
982
E1. It was established in Section \@ref(spatial-vec) that Canterbury was the region of New Zealand containing most of the 100 highest points in the country.
@@ -998,29 +997,50 @@ E3. Generalizing the question to all regions: how many of New Zealand's 16 regio
998
997
999
998
1000
999
1001
-
E4. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
1000
+
E4. Test your knowledge of spatial predicates by finding out and plotting how US states relate to each other and other spatial objects.
1001
+
1002
+
The starting point of this exercise is to create an object representing Colorado state in the USA. Do this with the command
1003
+
`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.
1004
+
1005
+
- 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 `[`).
1006
+
- 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).
1007
+
- 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.
1008
+
1009
+
1010
+
1011
+
1012
+
1013
+
1014
+
1015
+
1016
+
1017
+
1018
+
1019
+
1020
+
1021
+
E5. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
1002
1022
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.
1003
1023
1004
1024
1005
1025
1006
-
E5. Apply a line detection filter to `rast(system.file("ex/logo.tif", package = "terra"))`.
1026
+
E6. Apply a line detection filter to `rast(system.file("ex/logo.tif", package = "terra"))`.
1007
1027
Plot the result.
1008
1028
Hint: Read `?terra::focal()`.
1009
1029
1010
1030
1011
1031
1012
-
E6. Calculate the Normalized Difference Water Index (NDWI; `(green - nir)/(green + nir)`) of a Landsat image.
1032
+
E7. Calculate the Normalized Difference Water Index (NDWI; `(green - nir)/(green + nir)`) of a Landsat image.
1013
1033
Use the Landsat image provided by the **spDataLarge** package (`system.file("raster/landsat.tif", package = "spDataLarge")`).
1014
1034
Also, calculate a correlation between NDVI and NDWI for this area.
1015
1035
1016
1036
1017
1037
1018
-
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()`.
1038
+
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()`.
1019
1039
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()`).
1020
1040
Convert the resulting distances from meters to kilometers.
1021
1041
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.
1022
1042
1023
1043
1024
1044
1025
-
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.
1045
+
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.
1026
1046
Next, compute and visualize the difference between the raster created using the Euclidean distance (E7) and the raster weighted by elevation.
<p>E1. It was established in Section <ahref="spatial-operations.html#spatial-vec">4.2</a> that Canterbury was the region of New Zealand containing most of the 100 highest points in the country.
922
921
How many of these high points does the Canterbury region contain?</p>
923
922
<p><strong>Bonus:</strong> plot the result using the <code><ahref="https://r-spatial.github.io/sf/reference/plot.html">plot()</a></code> function to show all of New Zealand, <code>canterbury</code> region highlighted in yellow, high points in Canterbury represented with black dots and</p>
@@ -926,19 +925,27 @@ <h2>
926
925
<ul>
927
926
<li>Bonus: create a table listing these regions in order of the number of points and their name.</li>
928
927
</ul>
929
-
<p>E4. Use <code>dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))</code>, and reclassify the elevation in three classes: low (<300), medium and high (>500).
928
+
<p>E4. Test your knowledge of spatial predicates by finding out and plotting how US states relate to each other and other spatial objects.</p>
929
+
<p>The starting point of this exercise is to create an object representing Colorado state in the USA. Do this with the command
930
+
<code>colorado = us_states[us_states$NAME == "Colorado",]</code> (base R) or with with the <code><ahref="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> function (tidyverse) and plot the resulting object in the context of US states.</p>
931
+
<ul>
932
+
<li>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 <code>[</code>).</li>
933
+
<li>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 <code>op = st_intersects</code> and other spatial relations during spatial subsetting operations in base R).</li>
934
+
<li>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 <code><ahref="https://r-spatial.github.io/sf/reference/geos_unary.html">st_centroid()</a></code>, <code><ahref="https://r-spatial.github.io/sf/reference/geos_combine.html">st_union()</a></code> and <code><ahref="https://r-spatial.github.io/sf/reference/st_cast.html">st_cast()</a></code> described in Chapter 5 may help) and identify which states this long East-West line crosses.</li>
935
+
</ul>
936
+
<p>E5. Use <code>dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))</code>, and reclassify the elevation in three classes: low (<300), medium and high (>500).
930
937
Secondly, read the NDVI raster (<code>ndvi = rast(system.file("raster/ndvi.tif", package = "spDataLarge"))</code>) and compute the mean NDVI and the mean elevation for each altitudinal class.</p>
931
-
<p>E5. Apply a line detection filter to <code>rast(system.file("ex/logo.tif", package = "terra"))</code>.
938
+
<p>E6. Apply a line detection filter to <code>rast(system.file("ex/logo.tif", package = "terra"))</code>.
932
939
Plot the result.
933
940
Hint: Read <code>?terra::focal()</code>.</p>
934
-
<p>E6. Calculate the Normalized Difference Water Index (NDWI; <code>(green - nir)/(green + nir)</code>) of a Landsat image.
941
+
<p>E7. Calculate the Normalized Difference Water Index (NDWI; <code>(green - nir)/(green + nir)</code>) of a Landsat image.
935
942
Use the Landsat image provided by the <strong>spDataLarge</strong> package (<code>system.file("raster/landsat.tif", package = "spDataLarge")</code>).
936
943
Also, calculate a correlation between NDVI and NDWI for this area.</p>
937
-
<p>E7. A StackOverflow <ahref="https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances">post</a> shows how to compute distances to the nearest coastline using <code><ahref="https://rdrr.io/pkg/raster/man/distance.html">raster::distance()</a></code>.
944
+
<p>E8. A StackOverflow <ahref="https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances">post</a> shows how to compute distances to the nearest coastline using <code><ahref="https://rdrr.io/pkg/raster/man/distance.html">raster::distance()</a></code>.
938
945
Try to do something similar but with <code><ahref="https://rdrr.io/pkg/terra/man/distance.html">terra::distance()</a></code>: retrieve a digital elevation model of Spain, and compute a raster which represents distances to the coast across the country (hint: use <code><ahref="https://rdrr.io/pkg/geodata/man/elevation.html">geodata::elevation_30s()</a></code>).
939
946
Convert the resulting distances from meters to kilometers.
940
947
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.</p>
941
-
<p>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.
948
+
<p>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.
942
949
Next, compute and visualize the difference between the raster created using the Euclidean distance (E7) and the raster weighted by elevation.</p>
0 commit comments