Skip to content

Commit 4d7078c

Browse files
authored
Merge pull request #394 from albhasan/m2022q1_e9
Episode 9 updated by replacing calls to raster and rgdal packages
2 parents 607d5dd + 2c87479 commit 4d7078c

File tree

1 file changed

+81
-79
lines changed

1 file changed

+81
-79
lines changed

episodes/09-vector-when-data-dont-line-up-crs.Rmd

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ source("setup.R")
2222
::::::::::::::::::::::::::::::::::::::::::::::::::
2323

2424
```{r load-libraries, echo=FALSE, results="hide", message=FALSE, warning=FALSE}
25-
library(raster)
26-
library(rgdal)
25+
library(terra)
2726
library(sf)
2827
library(ggplot2)
2928
library(dplyr)
@@ -33,21 +32,22 @@ library(dplyr)
3332

3433
## Things You'll Need To Complete This Episode
3534

36-
See the [lesson homepage](.) for detailed information about the software,
37-
data, and other prerequisites you will need to work through the examples in this episode.
35+
See the [lesson homepage](.) for detailed information about the software, data,
36+
and other prerequisites you will need to work through the examples in this
37+
episode.
3838

3939

4040
::::::::::::::::::::::::::::::::::::::::::::::::::
4141

4242
In [an earlier episode](03-raster-reproject-in-r/)
43-
we learned how to handle a situation where you have two different
44-
files with raster data in different projections. Now we will apply
45-
those same principles to working with vector data.
46-
We will create a base map of our study site using United States
47-
state and country boundary information accessed from the
43+
we learned how to handle a situation where you have two different files with
44+
raster data in different projections. Now we will apply those same principles
45+
to working with vector data.
46+
We will create a base map of our study site using United States state and
47+
country boundary information accessed from the
4848
[United States Census Bureau](https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html).
49-
We will learn how to map vector data that are in different CRSs and thus
50-
don't line up on a map.
49+
We will learn how to map vector data that are in different CRSs and thus don't
50+
line up on a map.
5151

5252
We will continue to work with the three shapefiles that we loaded in the
5353
[Open and Plot Shapefiles in R](06-vector-open-shapefile-in-r/) episode.
@@ -57,55 +57,50 @@ We will continue to work with the three shapefiles that we loaded in the
5757
aoi_boundary_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
5858
lines_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")
5959
point_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARVtower_UTM18N.shp")
60-
CHM_HARV <- raster("data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif")
60+
CHM_HARV <- rast("data/NEON-DS-Airborne-Remote-Sensing/HARV/CHM/HARV_chmCrop.tif")
6161
CHM_HARV_df <- as.data.frame(CHM_HARV, xy = TRUE)
6262
roadColors <- c("blue", "green", "grey", "purple")[lines_HARV$TYPE]
6363
```
6464

6565
## Working With Spatial Data From Different Sources
6666

67-
We often need to gather spatial datasets from
68-
different sources and/or data that cover different spatial extents.
69-
These data are often in
70-
different Coordinate Reference Systems (CRSs).
67+
We often need to gather spatial datasets from different sources and/or data
68+
that cover different spatial extents.
69+
These data are often in different Coordinate Reference Systems (CRSs).
7170

7271
Some reasons for data being in different CRSs include:
7372

74-
1. The data are stored in a particular CRS convention used by the data
75-
provider (for example, a government agency).
76-
2. The data are stored in a particular CRS that is customized to a region.
77-
For instance, many states in the US prefer to use a State Plane projection customized
78-
for that state.
79-
80-
![Maps of the United States using data in different projections. Source: opennews.org, from: https://media.opennews.org/cache/06/37/0637aa2541b31f526ad44f7cb2db7b6c.jpg](fig/map_usa_different_projections.jpg)
81-
82-
Notice the differences in shape associated with each different
83-
projection. These differences are a direct result of the calculations
84-
used to "flatten" the data onto a 2-dimensional map. Often data are
85-
stored purposefully in a particular projection that optimizes the
86-
relative shape and size of surrounding geographic boundaries (states,
87-
counties, countries, etc).
88-
89-
In this episode we will learn how to identify and manage spatial data
90-
in different projections. We will learn how to reproject the data so
91-
that they
92-
are in the same projection to support plotting / mapping. Note that
93-
these skills
94-
are also required for any geoprocessing / spatial analysis. Data need
95-
to be in
73+
1. The data are stored in a particular CRS convention used by the data provider
74+
(for example, a government agency).
75+
2. The data are stored in a particular CRS that is customized to a region. For
76+
instance, many states in the US prefer to use a State Plane projection
77+
customized for that state.
78+
79+
![Maps of the United States using data in different projections. Source: opennews.org, from: https://media.opennews.org/cache/06/37/0637aa2541b31f526ad44f7cb2db7b6c.jpg](fig/map_usa_different_projections.jpg){alt='Maps of the United States using data in different projections.}
80+
81+
Notice the differences in shape associated with each different projection.
82+
These differences are a direct result of the calculations used to "flatten" the
83+
data onto a 2-dimensional map. Often data are stored purposefully in a
84+
particular projection that optimizes the relative shape and size of surrounding
85+
geographic boundaries (states, counties, countries, etc).
86+
87+
In this episode we will learn how to identify and manage spatial data in
88+
different projections. We will learn how to reproject the data so that they are
89+
in the same projection to support plotting / mapping. Note that these skills
90+
are also required for any geoprocessing / spatial analysis. Data need to be in
9691
the same CRS to ensure accurate results.
9792

98-
We will continue to use the `sf` and `raster` packages in this episode.
93+
We will continue to use the `sf` and `terra` packages in this episode.
9994

10095
## Import US Boundaries - Census Data
10196

10297
There are many good sources of boundary base layers that we can use to create a
10398
basemap. Some R packages even have these base layers built in to support quick
104-
and efficient mapping. In this episode, we will use boundary layers for the contiguous
105-
United States, provided by the [United States Census Bureau](https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html).
106-
It is useful to have shapefiles to work with because we can add
107-
additional attributes to them if need be - for project specific
108-
mapping.
99+
and efficient mapping. In this episode, we will use boundary layers for the
100+
contiguous United States, provided by the
101+
[United States Census Bureau](https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html).
102+
It is useful to have shapefiles to work with because we can add additional
103+
attributes to them if need be - for project specific mapping.
109104

110105
## Read US Boundary File
111106

@@ -116,7 +111,8 @@ these data have been modified and reprojected from the original data downloaded
116111
from the Census website to support the learning goals of this episode.
117112

118113
```{r read-shp}
119-
state_boundary_US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-State-Boundaries-Census-2014.shp")
114+
state_boundary_US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-State-Boundaries-Census-2014.shp") %>%
115+
st_zm()
120116
```
121117

122118
Next, let's plot the U.S. states data:
@@ -135,12 +131,13 @@ nicer. We will import
135131
`NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-Boundary-Dissolved-States`.
136132

137133
```{r}
138-
country_boundary_US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-Boundary-Dissolved-States.shp")
134+
country_boundary_US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-Boundary-Dissolved-States.shp") %>%
135+
st_zm()
139136
```
140137

141-
If we specify a thicker line width using `size = 2` for the border layer, it will
142-
make our map pop! We will also manually set the colors of the state boundaries
143-
and country boundaries.
138+
If we specify a thicker line width using `size = 2` for the border layer, it
139+
will make our map pop! We will also manually set the colors of the state
140+
boundaries and country boundaries.
144141

145142
```{r us-boundaries-thickness}
146143
ggplot() +
@@ -155,36 +152,34 @@ As we are adding these layers, take note of the CRS of each object.
155152
First let's look at the CRS of our tower location object:
156153

157154
```{r crs-sleuthing-1}
158-
st_crs(point_HARV)
155+
st_crs(point_HARV)$proj4string
159156
```
160157

161158
Our project string for `DSM_HARV` specifies the UTM projection as follows:
162159

163-
`+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0`
160+
`+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs`
164161

165162
- **proj=utm:** the projection is UTM, UTM has several zones.
166163
- **zone=18:** the zone is 18
167164
- **datum=WGS84:** the datum WGS84 (the datum refers to the 0,0 reference for
168165
the coordinate system used in the projection)
169166
- **units=m:** the units for the coordinates are in METERS.
170-
- **ellps=WGS84:** the ellipsoid (how the earth's roundness is calculated) for
171-
the data is WGS84
172167

173-
Note that the `zone` is unique to the UTM projection. Not all CRSs
174-
will have a
168+
Note that the `zone` is unique to the UTM projection. Not all CRSs will have a
175169
zone.
176170

177171
Let's check the CRS of our state and country boundary objects:
178172

179173
```{r crs-sleuthing-2}
180-
st_crs(state_boundary_US)
181-
st_crs(country_boundary_US)
174+
st_crs(state_boundary_US)$proj4string
175+
st_crs(country_boundary_US)$proj4string
182176
```
183177

184178
Our project string for `state_boundary_US` and `country_boundary_US` specifies
185179
the lat/long projection as follows:
186180

187-
`+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0`
181+
`+proj=longlat +datum=WGS84 +no_defs`
182+
188183

189184
- **proj=longlat:** the data are in a geographic (latitude and longitude)
190185
coordinate system
@@ -194,15 +189,15 @@ the lat/long projection as follows:
194189
is WGS84
195190

196191
Note that there are no specified units above. This is because this geographic
197-
coordinate reference system is in latitude and longitude which is most
198-
often recorded in decimal degrees.
192+
coordinate reference system is in latitude and longitude which is most often
193+
recorded in decimal degrees.
199194

200195
::::::::::::::::::::::::::::::::::::::::: callout
201196

202197
## Data Tip
203198

204-
the last portion of each `proj4` string
205-
is `+towgs84=0,0,0 `. This is a conversion factor that is used if a datum
199+
the last portion of each `proj4` string could potentially be something like
200+
`+towgs84=0,0,0 `. This is a conversion factor that is used if a datum
206201
conversion is required. We will not deal with datums in this episode series.
207202

208203

@@ -237,21 +232,22 @@ represented in meters.
237232
- [Official PROJ library documentation](https://proj4.org/)
238233
- [More information on the proj4 format.](https://proj.maptools.org/faq.html)
239234
- [A fairly comprehensive list of CRSs by format.](https://spatialreference.org)
240-
- To view a list of datum conversion factors type: `projInfo(type = "datum")`
241-
into the R console.
235+
- To view a list of datum conversion factors type:
236+
`sf_proj_info(type = "datum")` into the R console. However, the results would
237+
depend on the underlying version of the PROJ library.
242238

243239

244240
::::::::::::::::::::::::::::::::::::::::::::::::::
245241

246242
## Reproject Vector Data or No?
247243

248-
We saw in [an earlier episode](03-raster-reproject-in-r/) that when working with raster
249-
data in different CRSs, we needed to convert all objects to the same
250-
CRS. We can do the same thing with our vector data - however, we
251-
don't need to! When using the `ggplot2` package, `ggplot`
252-
automatically converts all objects to the same CRS before plotting.
253-
This means we can plot our three data sets together
254-
without doing any conversion:
244+
We saw in [an earlier episode](03-raster-reproject-in-r/) that when working
245+
with raster data in different CRSs, we needed to convert all objects to the
246+
same CRS. We can do the same thing with our vector data - however, we don't
247+
need to! When using the `ggplot2` package, `ggplot` automatically converts all
248+
objects to the same CRS before plotting.
249+
This means we can plot our three data sets together without doing any
250+
conversion:
255251

256252
```{r layer-point-on-states}
257253
ggplot() +
@@ -268,24 +264,30 @@ ggplot() +
268264

269265
Create a map of the North Eastern United States as follows:
270266

271-
1. Import and plot `Boundary-US-State-NEast.shp`. Adjust line width as necessary.
272-
2. Layer the Fisher Tower (in the NEON Harvard Forest site) point location `point_HARV` onto the plot.
267+
1. Import and plot `Boundary-US-State-NEast.shp`. Adjust line width as
268+
necessary.
269+
2. Layer the Fisher Tower (in the NEON Harvard Forest site) point location
270+
`point_HARV` onto the plot.
273271
3. Add a title.
274-
4. Add a legend that shows both the state boundary (as a line) and
275-
the Tower location point.
272+
4. Add a legend that shows both the state boundary (as a line) and the Tower
273+
location point.
276274

277275
::::::::::::::: solution
278276

279277
## Answers
280278

281279
```{r ne-states-harv}
282-
NE.States.Boundary.US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/Boundary-US-State-NEast.shp")
280+
NE.States.Boundary.US <- st_read("data/NEON-DS-Site-Layout-Files/US-Boundary-Layers/Boundary-US-State-NEast.shp") %>%
281+
st_zm()
283282
284283
ggplot() +
285-
geom_sf(data = NE.States.Boundary.US, aes(color ="color"), show.legend = "line") +
286-
scale_color_manual(name = "", labels = "State Boundary", values = c("color" = "gray18")) +
284+
geom_sf(data = NE.States.Boundary.US, aes(color ="color"),
285+
show.legend = "line") +
286+
scale_color_manual(name = "", labels = "State Boundary",
287+
values = c("color" = "gray18")) +
287288
geom_sf(data = point_HARV, aes(shape = "shape"), color = "purple") +
288-
scale_shape_manual(name = "", labels = "Fisher Tower", values = c("shape" = 19)) +
289+
scale_shape_manual(name = "", labels = "Fisher Tower",
290+
values = c("shape" = 19)) +
289291
ggtitle("Fisher Tower location") +
290292
theme(legend.background = element_rect(color = NA)) +
291293
coord_sf()

0 commit comments

Comments
 (0)