diff --git a/foundations.qmd b/foundations.qmd
index e46a886..821d659 100644
--- a/foundations.qmd
+++ b/foundations.qmd
@@ -240,6 +240,7 @@ _ Manipulation:
A demonstration using `dataRetrieval` package stream gage data from USGS:
```{r}
+library(dataRetrieval)
flows <- dataRetrieval::readNWISdv(
siteNumbers = '14187200',
parameterCd = "00060"
@@ -721,6 +722,8 @@ summary(us_states['total_pop_15'])
The geometry is **sticky**, which means that it is carried along by default!
+You can read `sf` objects from shapefiles using `st_read()` or `read_sf()`.
+
### Raster Data Model
Raster data can be *continuous* (e.g. elevation, precipitation, atmospheric deposition) or it can be *categorical* (e.g. land use, soil type, geology type). Raster data can also be image based rasters which can be single-band or multi-band. You can also have a temporal component with space-time cubes.
@@ -754,7 +757,7 @@ Try providing a different bounding box for an area of your choosing or changing
```{r raster-layer, message=FALSE, warning=FALSE, error=FALSE}
library(terra)
-myrast <- rast(ncol=10, nrow = 10, xmax=-116,xmin=-126,ymin=42,ymax=46)
+myrast <- rast(ncol = 10, nrow = 10, xmax = -116, xmin = -126, ymin = 42, ymax = 46)
myrast
```
:::
@@ -764,7 +767,7 @@ myrast
```{r}
#| eval: false
# just an example:
-myrast <- rast(ncol=40, nrow = 40, xmax=-120,xmin=-136,ymin=52,ymax=56)
+myrast <- rast(ncol=40, nrow = 40, xmax=-120, xmin=-136, ymin=52, ymax=56)
```
:::
@@ -976,7 +979,7 @@ A coordinate reference system for spatial data can be defined in different ways
- 'authority:code' identifier
+ `EPSG:4326`
-Proj4 used to be the standard crs identifier in R but the preferrable way is ty use the AUTHORITY:CODE method which `sf` as well as software like [QGIS](https://docs.qgis.org/3.16/en/docs/user_manual/working_with_projections/working_with_projections.html) will recognize. The AUTHORITY:CODE method is durable and easily discoverable online.
+Proj4 used to be the standard crs identifier in R but the preferable way is to use the AUTHORITY:CODE method which `sf` as well as software like [QGIS](https://docs.qgis.org/3.16/en/docs/user_manual/working_with_projections/working_with_projections.html) will recognize. The AUTHORITY:CODE method is durable and easily discoverable online.
The `WKT` representation of EPSG:4326 in `sf` is:
```{r}
@@ -1004,8 +1007,8 @@ st_crs(gages_sf)==st_crs(pnw)
# transform one to the other
gages_sf <- st_transform(gages_sf, st_crs(pnw))
ggplot() +
- geom_sf(data=gages_sf, color="blue") +
- geom_sf(data=pnw, color="black", fill=NA) +
+ geom_sf(data = gages_sf, color="blue") +
+ geom_sf(data = pnw, color="black", fill=NA) +
labs(title="USGS Stream Gages in the Pacific Northwest") +
theme_bw()
```
@@ -1099,7 +1102,7 @@ See the [Geocomputation with R s2 section for further details](https://r.geocomp
:::{.callout-note}
There are sometimes good reasons for turning `S2` off during an R session. See issue 1771 in sf’s GitHub repo - the default behavior can make code that would work with S2 turned off (and with older versions of sf) fail.
-Situations that can cause problems include operations on polygons that are not valid according to S2’s stricter definition. If you see error message such as #> Error in s2_geography_from_wkb ... you may want to turn `s2` off and try your operation again, perhaps turning of `s2` and running a topology clean operation like `st_make_valid()`.
+Situations that can cause problems include operations on polygons that are not valid according to S2’s stricter definition. If you see error message such as `#> Error in s2_geography_from_wkb ...` you may want to turn `s2` off and try your operation again, perhaps turning of `s2` and running a topology clean operation like `st_make_valid()`.
:::
## Geographic Data I/O
@@ -1157,11 +1160,11 @@ plot(citylims$geometry, axes=T, main='Oregon City Limits') # plot it!
#### Solution
`read_sf` is an `sf` alternative to st_read (see [this](https://keen-swartz-3146c4.netlify.com/intro.html#read) section 1.2.2). Try reading in `citylims` data above using `read_sf` and notice difference, and check out `help(read_sf)`. `read_sf` and `write_sf` are simply aliases for st_read and st_write with modified default arguments. Big differences are:
-- stringsAsFactors=FALSE
-- quiet=TRUE
-- as_tibble=TRUE
+- `stringsAsFactors=FALSE`
+- `quiet=TRUE`
+- `as_tibble=TRUE`
-Note that stringsAsFactors = FALSE is the new default in R versions >= 4.0
+Note that `stringsAsFactors = FALSE` is the new default in R versions >= 4.0
#### Geodatabases
We use `st_read` or `read_sf` similarly for reading in an ESRI file geodatabase feature:
@@ -1285,8 +1288,8 @@ Load stock Landsat 7 .tif file that comes with package
```{r stars_read, message=FALSE, warning=FALSE, error=FALSE}
library(stars)
tif = system.file("tif/L7_ETMs.tif", package = "stars")
-read_stars(tif) %>%
- dplyr::slice(index = 1, along = "band") %>%
+read_stars(tif) |>
+ dplyr::slice(index = 1, along = "band") |>
plot()
```
@@ -1307,7 +1310,7 @@ In the steps below, we
library(readr)
library(ggplot2)
library(Rspatialworkshop)
-gages = read_csv(system.file("extdata/Gages_flowdata.csv", package = "Rspatialworkshop"))
+gages = read.csv(system.file("extdata/Gages_flowdata.csv", package = "Rspatialworkshop"))
gages_sf <- gages |>
st_as_sf(coords = c("LON_SITE", "LAT_SITE"), crs = 4269, remove = FALSE) |>
diff --git a/slides.html b/slides.html
index a50b16c..89027d5 100644
--- a/slides.html
+++ b/slides.html
@@ -1,12 +1,12 @@
We use the sf package to work with spatial vector data in R
@@ -1914,7 +1916,8 @@
+Figure 4: sf POINT
+
@@ -1941,7 +1944,8 @@
+Figure 5: sf LINESTRING
+
@@ -1971,7 +1975,8 @@
+Figure 6: sf POLYGON
+
Putting Them All Together
@@ -1986,7 +1991,8 @@
+Figure 7: sf POINT, LINESTRING, and POLYGON overlain
+
Spatial Data Frames
@@ -1994,6 +2000,8 @@
sf objects combine data.frame attributes with POINT, LINESTRING, and POLYGON building blocks (these are the geometry)
Also characterize dimensionality, bounding box, coordinate reference system, attribute headers
+
Read from shapefiles using st_read() or read_sf()
+
library(spData)
@@ -2038,10 +2046,414 @@
So far we have explored plotting using ggplot(), but you can also use sf’s base plotting via plot(). Read more about plotting in sf by running ?plot.sf and then make an appropriate plot of the us_states data.
+
+
+
+
+
+
+05:00
+
+
+
+
Raster Data Model
+
+
Raster data can be continuous or categorical
+
Can be image based; have a temporal component
+
+
+
Figure 9: The raster data model.
The terra Package
+
+
We use the terra package to work with spatial raster data in R
+
We just printed myrast and saw several components: class, dimensions, resolution, extent, and coord. ref. Explicitly return and inspect each of these pieces using class(), ncell(), res(), ext(), and crs(). Bonus: Return the number of raster cells using ncell().
+
+
+
+
+
+
+
+05:00
+
+
+
+
Manipulating Raster Objects
+
+
Let’s add values to the raster object
+
+
+
values(myrast) <-1:ncell(myrast)
+
+
+
plot(myrast)
+
+
+
Manipulating Raster Objects
+
+
+
+
+
+
Reading a Raster Object
+
+
Lets read in a raster object from the spDataLarge package that contains elevation data from Zion National Park
The authority:code identifier is the modern identifier in R, and our preferred method
+
Projected Coordinate Systems
+
+
Projected coordinates have been projected to two-dimensional space according to a CRS
+
Projected coordinates have an origin, x-axis, y-axis, and unit of measure
+
Conformal projections preserve shape
+
Equal-area projections preserve area
+
Equidistant projections preserve distance
+
Azimuthal projection preserves direction
+
Projected Coordinate Systems
+
+
Here an example using vector data; see the workbook for an example using raster data
+
+
+
library(Rspatialworkshop)
+data("pnw")
+# transform one to the other
+utmz11 <-2153
+pnw <-st_transform(pnw, crs = utmz11)
+ggplot() +
+geom_sf(data = pnw, color="black", fill=NA) +
+labs(title="State Boundaries in the Pacific Northwest") +
+theme_bw()
+
+
+
Projected Coordinate Systems
+
+
+
+
+
+
Your Turn
+
+
Re-project the pnw data to a different projected CRS. Then plot using base R or ggplot2.
+
+
+
+
+
+
+
+06:00
+
+
+
+
A Note on S2
+
+
+
+sf version 1.0.0 supports spherical geometry operations via its interface to Google’s S2 spherical geometry engine
+
+S2 is an example of a Discrete Global Grid System (DGGS)
+
+sf can run with s2 on or off and by default the S2 geometry engine is turned on:
+
+
+
sf::sf_use_s2()
+
+
[1] TRUE
+
+
+
Geographic Data I/O (Input/Ouptut)
+
There are several ways to read spatial data into R
+
+
Load spatial data from our machine or a remote source
+
Load spatial data as part of an R package
+
Load data using an API (which often makes use of an R package)
+
Convert flat files with x, y coordinates to spatial data
+
Geocoding data “by-hand” (we saw this earlier)
+
Vector Data I/O
+
sf can read numerous file types:
+
+
shapefiles
+
geodatabases
+
geopackages
+
geojson
+
spatial database files
+
Read in a Shapefile
+
+
filepath <-system.file("extdata/city_limits.shp", package ="Rspatialworkshop")
+citylims <-read_sf(filepath)
+plot(st_geometry(citylims), axes = T, main ="Oregon City Limits")
+
+
+
Read in a Shapefile
+
+
+
+
+
+
Your Turn
+
+
Run ?read_sf() and compare read_sf() to st_read(). Our preference is to use read_sf() – why do you think that is?
+
+
+
+
+
+
+
+03:00
+
+
+
+
Read in a Geodatabase
+
+
filepath <-system.file("extdata/StateParkBoundaries.gdb", package ="Rspatialworkshop")
+# List all feature classes in a file geodatabase
+st_layers(filepath)
+# Read the feature class
+parks <-st_read(dsn = filepath, layer="StateParkBoundaries")
+ggplot(parks) +
+geom_sf()