Skip to content

Commit

Permalink
Deploy commit: Add new bonus exercise #766 11da7b1
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Mar 18, 2022
1 parent 9649dfd commit e400f2f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
34 changes: 27 additions & 7 deletions 04-spatial-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,7 @@ For a more detailed introduction to remote sensing with R, see @wegmann_remote_2
```r
library(sf)
library(dplyr)
data(nz, package = "spData")
data(nz_height, package = "spData")
library(spData)
```

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.
Expand All @@ -998,29 +997,50 @@ E3. Generalizing the question to all regions: how many of New Zealand's 16 regio



E4. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
E4. Test your knowledge of spatial predicates by finding out and plotting how US states relate to each other and other spatial objects.

The starting point of this exercise is to create an object representing Colorado state in the USA. Do this with the command
`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.

- 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 `[`).
- 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).
- 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.













E5. Use `dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))`, and reclassify the elevation in three classes: low (<300), medium and high (>500).
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.



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



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



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()`.
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()`.
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()`).
Convert the resulting distances from meters to kilometers.
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.



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.
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.
Next, compute and visualize the difference between the raster created using the Euclidean distance (E7) and the raster weighted by elevation.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified 05-geometry-operations_files/figure-html/points-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion search.json

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions spatial-operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,7 @@ <h2>
<div class="sourceCode" id="cb157"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://r-spatial.github.io/sf/">sf</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">nz</span>, package <span class="op">=</span> <span class="st">"spData"</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">nz_height</span>, package <span class="op">=</span> <span class="st">"spData"</span><span class="op">)</span></code></pre></div>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://jakubnowosad.com/spData/">spData</a></span><span class="op">)</span></code></pre></div>
<p>E1. It was established in Section <a href="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.
How many of these high points does the Canterbury region contain?</p>
<p><strong>Bonus:</strong> plot the result using the <code><a href="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>
Expand All @@ -926,19 +925,27 @@ <h2>
<ul>
<li>Bonus: create a table listing these regions in order of the number of points and their name.</li>
</ul>
<p>E4. Use <code>dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))</code>, and reclassify the elevation in three classes: low (&lt;300), medium and high (&gt;500).
<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>
<p>The starting point of this exercise is to create an object representing Colorado state in the USA. Do this with the command
<code>colorado = us_states[us_states$NAME == "Colorado",]</code> (base R) or with with the <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> function (tidyverse) and plot the resulting object in the context of US states.</p>
<ul>
<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>
<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>
<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><a href="https://r-spatial.github.io/sf/reference/geos_unary.html">st_centroid()</a></code>, <code><a href="https://r-spatial.github.io/sf/reference/geos_combine.html">st_union()</a></code> and <code><a href="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>
</ul>
<p>E5. Use <code>dem = rast(system.file("raster/dem.tif", package = "spDataLarge"))</code>, and reclassify the elevation in three classes: low (&lt;300), medium and high (&gt;500).
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>
<p>E5. Apply a line detection filter to <code>rast(system.file("ex/logo.tif", package = "terra"))</code>.
<p>E6. Apply a line detection filter to <code>rast(system.file("ex/logo.tif", package = "terra"))</code>.
Plot the result.
Hint: Read <code>?terra::focal()</code>.</p>
<p>E6. Calculate the Normalized Difference Water Index (NDWI; <code>(green - nir)/(green + nir)</code>) of a Landsat image.
<p>E7. Calculate the Normalized Difference Water Index (NDWI; <code>(green - nir)/(green + nir)</code>) of a Landsat image.
Use the Landsat image provided by the <strong>spDataLarge</strong> package (<code>system.file("raster/landsat.tif", package = "spDataLarge")</code>).
Also, calculate a correlation between NDVI and NDWI for this area.</p>
<p>E7. A StackOverflow <a href="https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances">post</a> shows how to compute distances to the nearest coastline using <code><a href="https://rdrr.io/pkg/raster/man/distance.html">raster::distance()</a></code>.
<p>E8. A StackOverflow <a href="https://stackoverflow.com/questions/35555709/global-raster-of-geographic-distances">post</a> shows how to compute distances to the nearest coastline using <code><a href="https://rdrr.io/pkg/raster/man/distance.html">raster::distance()</a></code>.
Try to do something similar but with <code><a href="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><a href="https://rdrr.io/pkg/geodata/man/elevation.html">geodata::elevation_30s()</a></code>).
Convert the resulting distances from meters to kilometers.
Note: it may be wise to increase the cell size of the input raster to reduce compute time during this operation.</p>
<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.
<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.
Next, compute and visualize the difference between the raster created using the Euclidean distance (E7) and the raster weighted by elevation.</p>

</div>
Expand Down

0 comments on commit e400f2f

Please sign in to comment.