Skip to content

Commit 882a5b1

Browse files
authored
Merge pull request #395 from albhasan/m2022q1_e10
Episode 10 updated by replacing calls to raster and rgdal packages
2 parents 1b69ae7 + 3311d2b commit 882a5b1

File tree

1 file changed

+72
-54
lines changed

1 file changed

+72
-54
lines changed

episodes/10-vector-csv-to-shapefile-in-r.Rmd

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ source("setup.R")
2424
::::::::::::::::::::::::::::::::::::::::::::::::::
2525

2626
```{r load-libraries, echo=FALSE, results="hide", message=FALSE, warning=FALSE}
27-
library(raster)
28-
library(rgdal)
27+
library(terra)
2928
library(ggplot2)
3029
library(dplyr)
3130
library(sf)
@@ -43,13 +42,17 @@ point_HARV <- st_read("data/NEON-DS-Site-Layout-Files/HARV/HARVtower_UTM18N.shp"
4342

4443
## Things You'll Need To Complete This Episode
4544

46-
See the [lesson homepage](.) for detailed information about the software,
47-
data, and other prerequisites you will need to work through the examples in this episode.
45+
See the [lesson homepage](.) for detailed information about the software, data,
46+
and other prerequisites you will need to work through the examples in this
47+
episode.
4848

4949

5050
::::::::::::::::::::::::::::::::::::::::::::::::::
5151

52-
This episode will review how to import spatial points stored in `.csv` (Comma Separated Value) format into R as an `sf` spatial object. We will also reproject data imported from a shapefile format, export this data as a shapefile, and plot raster and vector data as layers in the same plot.
52+
This episode will review how to import spatial points stored in `.csv` (Comma
53+
Separated Value) format into R as an `sf` spatial object. We will also
54+
reproject data imported from a shapefile format, export this data as a
55+
shapefile, and plot raster and vector data as layers in the same plot.
5356

5457
## Spatial Data in Text Format
5558

@@ -65,17 +68,18 @@ We would like to:
6568

6669
Spatial data are sometimes stored in a text file format (`.txt` or `.csv`). If
6770
the text file has an associated `x` and `y` location column, then we can
68-
convert it into an `sf` spatial object. The `sf` object allows us to store both the `x,y` values that represent the coordinate location
69-
of each point and the associated attribute data - or columns describing each
70-
feature in the spatial object.
71+
convert it into an `sf` spatial object. The `sf` object allows us to store both
72+
the `x,y` values that represent the coordinate location of each point and the
73+
associated attribute data - or columns describing each feature in the spatial
74+
object.
7175

72-
We will continue using the `sf` and `raster` packages in this episode.
76+
We will continue using the `sf` and `terra` packages in this episode.
7377

7478
## Import .csv
7579

76-
To begin let's import a `.csv` file that contains plot coordinate `x, y`
77-
locations at the NEON Harvard Forest Field Site (`HARV_PlotLocations.csv`) and look at the structure of
78-
that new object:
80+
To begin let's import a `.csv` file that contains plot coordinate `x, y`
81+
locations at the NEON Harvard Forest Field Site (`HARV_PlotLocations.csv`) and
82+
look at the structure of that new object:
7983

8084
```{r read-csv}
8185
plot_locations_HARV <-
@@ -84,7 +88,11 @@ plot_locations_HARV <-
8488
str(plot_locations_HARV)
8589
```
8690

87-
We now have a data frame that contains 21 locations (rows) and 16 variables (attributes). Note that all of our character data was imported into R as factor (categorical) data. Next, let's explore the dataframe to determine whether it contains columns with coordinate values. If we are lucky, our `.csv` will contain columns labeled:
91+
We now have a data frame that contains 21 locations (rows) and 16 variables
92+
(attributes). Note that all of our character data was imported into R as
93+
character (text) data. Next, let's explore the dataframe to determine whether
94+
it contains columns with coordinate values. If we are lucky, our `.csv` will
95+
contain columns labeled:
8896

8997
- "X" and "Y" OR
9098
- Latitude and Longitude OR
@@ -98,18 +106,19 @@ names(plot_locations_HARV)
98106

99107
## Identify X,Y Location Columns
100108

101-
Our column names include several fields that might contain spatial information. The `plot_locations_HARV$easting`
102-
and `plot_locations_HARV$northing` columns contain coordinate values. We can confirm
103-
this by looking at the first six rows of our data.
109+
Our column names include several fields that might contain spatial information.
110+
The `plot_locations_HARV$easting` and `plot_locations_HARV$northing` columns
111+
contain coordinate values. We can confirm this by looking at the first six rows
112+
of our data.
104113

105114
```{r check-out-coordinates}
106115
head(plot_locations_HARV$easting)
107116
head(plot_locations_HARV$northing)
108117
```
109118

110-
We have coordinate values in our data frame. In order to convert our
111-
data frame to an `sf` object, we also need to know the CRS
112-
associated with those coordinate values.
119+
We have coordinate values in our data frame. In order to convert our data frame
120+
to an `sf` object, we also need to know the CRS associated with those
121+
coordinate values.
113122

114123
There are several ways to figure out the CRS of spatial data in text format.
115124

@@ -119,8 +128,8 @@ There are several ways to figure out the CRS of spatial data in text format.
119128
file header or somewhere in the data columns.
120129

121130
Following the `easting` and `northing` columns, there is a `geodeticDa` and a
122-
`utmZone` column. These appear to contain CRS information
123-
(`datum` and `projection`). Let's view those next.
131+
`utmZone` column. These appear to contain CRS information (`datum` and
132+
`projection`). Let's view those next.
124133

125134
```{r view-CRS-info}
126135
head(plot_locations_HARV$geodeticDa)
@@ -140,23 +149,27 @@ we learned about the components of a `proj4` string. We have everything we need
140149
to assign a CRS to our data frame.
141150

142151
To create the `proj4` associated with UTM Zone 18 WGS84 we can look up the
143-
projection on the [Spatial Reference website](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/), which contains a list of CRS formats for each projection. From here, we can extract the [proj4 string for UTM Zone 18N WGS84](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/proj4/).
152+
projection on the
153+
[Spatial Reference website](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/),
154+
which contains a list of CRS formats for each projection. From here, we can
155+
extract the
156+
[proj4 string for UTM Zone 18N WGS84](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/proj4/).
144157

145-
However, if we have other data in the UTM Zone 18N projection, it's much
146-
easier to use the `st_crs()` function to extract the CRS in `proj4` format from that object and
147-
assign it to our
148-
new spatial object. We've seen this CRS before with our Harvard Forest study site (`point_HARV`).
158+
However, if we have other data in the UTM Zone 18N projection, it's much easier
159+
to use the `st_crs()` function to extract the CRS in `proj4` format from that
160+
object and assign it to our new spatial object. We've seen this CRS before with
161+
our Harvard Forest study site (`point_HARV`).
149162

150163
```{r explore-units}
151164
st_crs(point_HARV)
152165
```
153166

154-
The output above shows that the points shapefile is in
155-
UTM zone 18N. We can thus use the CRS from that spatial object to convert our
156-
non-spatial dataframe into an `sf` object.
167+
The output above shows that the points shapefile is in UTM zone 18N. We can
168+
thus use the CRS from that spatial object to convert our non-spatial dataframe
169+
into an `sf` object.
157170

158-
Next, let's create a `crs` object that we can use to define the CRS of our
159-
`sf` object when we create it.
171+
Next, let's create a `crs` object that we can use to define the CRS of our `sf`
172+
object when we create it.
160173

161174
```{r crs-object}
162175
utm18nCRS <- st_crs(point_HARV)
@@ -167,16 +180,18 @@ class(utm18nCRS)
167180

168181
## .csv to sf object
169182

170-
Next, let's convert our dataframe into an `sf` object. To do
171-
this, we need to specify:
183+
Next, let's convert our dataframe into an `sf` object. To do this, we need to
184+
specify:
172185

173186
1. The columns containing X (`easting`) and Y (`northing`) coordinate values
174187
2. The CRS that the column coordinate represent (units are included in the CRS) - stored in our `utmCRS` object.
175188

176189
We will use the `st_as_sf()` function to perform the conversion.
177190

178191
```{r convert-csv-shapefile}
179-
plot_locations_sp_HARV <- st_as_sf(plot_locations_HARV, coords = c("easting", "northing"), crs = utm18nCRS)
192+
plot_locations_sp_HARV <- st_as_sf(plot_locations_HARV,
193+
coords = c("easting", "northing"),
194+
crs = utm18nCRS)
180195
```
181196

182197
We should double check the CRS to make sure it is correct.
@@ -200,8 +215,9 @@ ggplot() +
200215
In
201216
[Open and Plot Shapefiles in R](06-vector-open-shapefile-in-r/)
202217
we learned about spatial object extent. When we plot several spatial layers in
203-
R using `ggplot`, all of the layers of the plot are considered in setting the boundaries
204-
of the plot. To show this, let's plot our `aoi_boundary_HARV` object with our vegetation plots.
218+
R using `ggplot`, all of the layers of the plot are considered in setting the
219+
boundaries of the plot. To show this, let's plot our `aoi_boundary_HARV` object
220+
with our vegetation plots.
205221

206222
```{r plot-data}
207223
ggplot() +
@@ -210,8 +226,8 @@ ggplot() +
210226
ggtitle("AOI Boundary Plot")
211227
```
212228

213-
When we plot the two layers together, `ggplot` sets the plot boundaries
214-
so that they are large enough to include all of the data included in all of the layers.
229+
When we plot the two layers together, `ggplot` sets the plot boundaries so that
230+
they are large enough to include all of the data included in all of the layers.
215231
That's really handy!
216232

217233
::::::::::::::::::::::::::::::::::::::: challenge
@@ -223,7 +239,8 @@ locations.
223239

224240
Import the .csv: `HARV/HARV_2NewPhenPlots.csv` into R and do the following:
225241

226-
1. Find the X and Y coordinate locations. Which value is X and which value is Y?
242+
1. Find the X and Y coordinate locations. Which value is X and which value is
243+
Y?
227244
2. These data were collected in a geographic coordinate system (WGS84). Convert
228245
the dataframe into an `sf` object.
229246
3. Plot the new points with the plot location points from above. Be sure to add
@@ -236,8 +253,7 @@ If you have extra time, feel free to add roads and other layers to your map!
236253
## Answers
237254

238255
1)
239-
First we will read in the new csv file and look at the
240-
data structure.
256+
First we will read in the new csv file and look at the data structure.
241257

242258
```{r}
243259
newplot_locations_HARV <-
@@ -246,21 +262,22 @@ str(newplot_locations_HARV)
246262
```
247263

248264
2)
249-
The US boundary data we worked with previously is in a geographic
250-
WGS84 CRS. We can use that data to establish a CRS for this data. First
251-
we will extract the CRS from the `country_boundary_US` object and
252-
confirm that it is WGS84.
265+
The US boundary data we worked with previously is in a geographic WGS84 CRS. We
266+
can use that data to establish a CRS for this data. First we will extract the
267+
CRS from the `country_boundary_US` object and confirm that it is WGS84.
253268

254269
```{r}
255270
geogCRS <- st_crs(country_boundary_US)
256271
geogCRS
257272
```
258273

259-
Then we will convert our new data to a spatial dataframe, using
260-
the `geogCRS` object as our CRS.
274+
Then we will convert our new data to a spatial dataframe, using the `geogCRS`
275+
object as our CRS.
261276

262277
```{r}
263-
newPlot.Sp.HARV <- st_as_sf(newplot_locations_HARV, coords = c("decimalLon", "decimalLat"), crs = geogCRS)
278+
newPlot.Sp.HARV <- st_as_sf(newplot_locations_HARV,
279+
coords = c("decimalLon", "decimalLat"),
280+
crs = geogCRS)
264281
```
265282

266283
Next we'll confirm that the CRS for our new object is correct.
@@ -269,9 +286,9 @@ Next we'll confirm that the CRS for our new object is correct.
269286
st_crs(newPlot.Sp.HARV)
270287
```
271288

272-
We will be adding these new data points to the plot we
273-
created before. The data for the earlier plot was in UTM.
274-
Since we're using `ggplot`, it will reproject the data for us.
289+
We will be adding these new data points to the plot we created before. The data
290+
for the earlier plot was in UTM. Since we're using `ggplot`, it will reproject
291+
the data for us.
275292

276293
3) Now we can create our plot.
277294

@@ -292,8 +309,8 @@ We can write an R spatial object to a shapefile using the `st_write` function
292309
in `sf`. To do this we need the following arguments:
293310

294311
- the name of the spatial object (`plot_locations_sp_HARV`)
295-
- the directory where we want to save our shapefile
296-
(to use `current = getwd()` or you can specify a different path)
312+
- the directory where we want to save our shapefile (to use `current = getwd()`
313+
or you can specify a different path)
297314
- the name of the new shapefile (`PlotLocations_HARV`)
298315
- the driver which specifies the file format (ESRI Shapefile)
299316

@@ -308,7 +325,8 @@ st_write(plot_locations_sp_HARV,
308325

309326
:::::::::::::::::::::::::::::::::::::::: keypoints
310327

311-
- Know the projection (if any) of your point data prior to converting to a spatial object.
328+
- Know the projection (if any) of your point data prior to converting to a
329+
spatial object.
312330
- Convert a data frame to an `sf` object using the `st_as_sf()` function.
313331
- Export an `sf` object as text using the `st_write()` function.
314332

0 commit comments

Comments
 (0)