@@ -22,8 +22,7 @@ source("setup.R")
22
22
::::::::::::::::::::::::::::::::::::::::::::::::::
23
23
24
24
``` {r load-libraries, echo=FALSE, results="hide", message=FALSE, warning=FALSE}
25
- library(raster)
26
- library(rgdal)
25
+ library(terra)
27
26
library(sf)
28
27
library(ggplot2)
29
28
library(dplyr)
@@ -33,21 +32,22 @@ library(dplyr)
33
32
34
33
## Things You'll Need To Complete This Episode
35
34
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.
38
38
39
39
40
40
::::::::::::::::::::::::::::::::::::::::::::::::::
41
41
42
42
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
48
48
[ 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.
51
51
52
52
We will continue to work with the three shapefiles that we loaded in the
53
53
[ 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
57
57
aoi_boundary_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
58
58
lines_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")
59
59
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")
61
61
CHM_HARV_df <- as.data.frame(CHM_HARV, xy = TRUE)
62
62
roadColors <- c("blue", "green", "grey", "purple")[lines_HARV$TYPE]
63
63
```
64
64
65
65
## Working With Spatial Data From Different Sources
66
66
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).
71
70
72
71
Some reasons for data being in different CRSs include:
73
72
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
96
91
the same CRS to ensure accurate results.
97
92
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.
99
94
100
95
## Import US Boundaries - Census Data
101
96
102
97
There are many good sources of boundary base layers that we can use to create a
103
98
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.
109
104
110
105
## Read US Boundary File
111
106
@@ -116,7 +111,8 @@ these data have been modified and reprojected from the original data downloaded
116
111
from the Census website to support the learning goals of this episode.
117
112
118
113
``` {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()
120
116
```
121
117
122
118
Next, let's plot the U.S. states data:
@@ -135,12 +131,13 @@ nicer. We will import
135
131
` NEON-DS-Site-Layout-Files/US-Boundary-Layers/US-Boundary-Dissolved-States ` .
136
132
137
133
``` {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()
139
136
```
140
137
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.
144
141
145
142
``` {r us-boundaries-thickness}
146
143
ggplot() +
@@ -155,36 +152,34 @@ As we are adding these layers, take note of the CRS of each object.
155
152
First let's look at the CRS of our tower location object:
156
153
157
154
``` {r crs-sleuthing-1}
158
- st_crs(point_HARV)
155
+ st_crs(point_HARV)$proj4string
159
156
```
160
157
161
158
Our project string for ` DSM_HARV ` specifies the UTM projection as follows:
162
159
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 `
164
161
165
162
- ** proj=utm:** the projection is UTM, UTM has several zones.
166
163
- ** zone=18:** the zone is 18
167
164
- ** datum=WGS84:** the datum WGS84 (the datum refers to the 0,0 reference for
168
165
the coordinate system used in the projection)
169
166
- ** 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
172
167
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
175
169
zone.
176
170
177
171
Let's check the CRS of our state and country boundary objects:
178
172
179
173
``` {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
182
176
```
183
177
184
178
Our project string for ` state_boundary_US ` and ` country_boundary_US ` specifies
185
179
the lat/long projection as follows:
186
180
187
- ` +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 `
181
+ ` +proj=longlat +datum=WGS84 +no_defs `
182
+
188
183
189
184
- ** proj=longlat:** the data are in a geographic (latitude and longitude)
190
185
coordinate system
@@ -194,15 +189,15 @@ the lat/long projection as follows:
194
189
is WGS84
195
190
196
191
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.
199
194
200
195
::::::::::::::::::::::::::::::::::::::::: callout
201
196
202
197
## Data Tip
203
198
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
206
201
conversion is required. We will not deal with datums in this episode series.
207
202
208
203
@@ -237,21 +232,22 @@ represented in meters.
237
232
- [ Official PROJ library documentation] ( https://proj4.org/ )
238
233
- [ More information on the proj4 format.] ( https://proj.maptools.org/faq.html )
239
234
- [ 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.
242
238
243
239
244
240
::::::::::::::::::::::::::::::::::::::::::::::::::
245
241
246
242
## Reproject Vector Data or No?
247
243
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:
255
251
256
252
``` {r layer-point-on-states}
257
253
ggplot() +
@@ -268,24 +264,30 @@ ggplot() +
268
264
269
265
Create a map of the North Eastern United States as follows:
270
266
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.
273
271
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.
276
274
277
275
::::::::::::::: solution
278
276
279
277
## Answers
280
278
281
279
``` {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()
283
282
284
283
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")) +
287
288
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)) +
289
291
ggtitle("Fisher Tower location") +
290
292
theme(legend.background = element_rect(color = NA)) +
291
293
coord_sf()
0 commit comments