Skip to content

Commit 3e30050

Browse files
committed
Episode 6 updated by replacing calls to raster and rgdal packages with calls to terra package. #368 #363
1 parent 231af26 commit 3e30050

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

episodes/06-vector-open-shapefile-in-r.Rmd

Lines changed: 54 additions & 41 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", warning=FALSE, message=FALSE}
27-
library(raster)
28-
library(rgdal)
27+
library(terra)
2928
library(ggplot2)
3029
library(dplyr)
3130
library(sf)
@@ -35,23 +34,28 @@ library(sf)
3534

3635
## Things You'll Need To Complete This Episode
3736

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

4141

4242
::::::::::::::::::::::::::::::::::::::::::::::::::
4343

44-
Starting with this episode, we will be moving from working with raster
45-
data to working with vector data. In this episode, we will open and plot point, line and polygon vector data
46-
stored in shapefile format in R. These data refer to the [NEON Harvard Forest field site](https://www.neonscience.org/field-sites/field-sites-map/HARV), which we have been working with in previous
47-
episodes. In later episodes, we will learn how to work with raster and
48-
vector data together and combine them into a single plot.
44+
Starting with this episode, we will be moving from working with raster data to
45+
working with vector data. In this episode, we will open and plot point, line
46+
and polygon vector data stored in shapefile format in R. These data refer to
47+
the
48+
[NEON Harvard Forest field site](https://www.neonscience.org/field-sites/field-sites-map/HARV),
49+
which we have been working with in previous episodes. In later episodes, we
50+
will learn how to work with raster and vector data together and combine them
51+
into a single plot.
4952

5053
## Import Shapefiles
5154

52-
We will use the `sf` package to work with vector data in R. Notice that the
53-
`rgdal` package automatically loads when `sf` is loaded. We will also use the
54-
`raster` package, which has been loaded in previous episodes, so we can explore raster and vector spatial metadata using similar commands. Make sure you have the `sf` library loaded.
55+
We will use the `sf` package to work with vector data in R. We will also use
56+
the `terra` package, which has been loaded in previous episodes, so we can
57+
explore raster and vector spatial metadata using similar commands. Make sure
58+
you have the `sf` library loaded.
5559

5660
```{r load-sf, results="hide", eval=FALSE, message=FALSE}
5761
library(sf)
@@ -66,7 +70,8 @@ The shapefiles that we will import are:
6670

6771
The first shapefile that we will open contains the boundary of our study area
6872
(or our Area Of Interest or AOI, hence the name `aoiBoundary`). To import
69-
shapefiles we use the `sf` function `st_read()`. `st_read()` requires the file path to the shapefile.
73+
shapefiles we use the `sf` function `st_read()`. `st_read()` requires the file
74+
path to the shapefile.
7075

7176
Let's import our AOI:
7277

@@ -80,9 +85,9 @@ aoi_boundary_HARV <- st_read(
8085
When we import the `HarClip_UTMZ18` shapefile layer into R (as our
8186
`aoi_boundary_HARV` object), the `st_read()` function automatically stores
8287
information about the data. We are particularly interested in the geospatial
83-
metadata, describing the format, CRS, extent, and other components of
84-
the vector data, and the attributes which describe properties associated
85-
with each individual vector object.
88+
metadata, describing the format, CRS, extent, and other components of the
89+
vector data, and the attributes which describe properties associated with each
90+
individual vector object.
8691

8792
::::::::::::::::::::::::::::::::::::::::: callout
8893

@@ -101,58 +106,64 @@ Key metadata for all shapefiles include:
101106

102107
1. **Object Type:** the class of the imported object.
103108
2. **Coordinate Reference System (CRS):** the projection of the data.
104-
3. **Extent:** the spatial extent (i.e. geographic area that the shapefile covers) of
105-
the shapefile. Note that the spatial extent for a shapefile represents the combined
106-
extent for all spatial objects in the shapefile.
109+
3. **Extent:** the spatial extent (i.e. geographic area that the shapefile
110+
covers) of the shapefile. Note that the spatial extent for a shapefile
111+
represents the combined extent for all spatial objects in the shapefile.
107112

108-
We can view shapefile metadata using the `st_geometry_type()`, `st_crs()` and `st_bbox()` functions. First, let's view the
109-
geometry type for our AOI shapefile:
113+
We can view shapefile metadata using the `st_geometry_type()`, `st_crs()` and
114+
`st_bbox()` functions. First, let's view the geometry type for our AOI
115+
shapefile:
110116

111117
```{r}
112118
st_geometry_type(aoi_boundary_HARV)
113119
```
114120

115-
Our `aoi_boundary_HARV` is a polygon object. The 18 levels shown below
116-
our output list the possible categories of the geometry type.
117-
Now let's check what CRS this file data is in:
121+
Our `aoi_boundary_HARV` is a polygon object. The 18 levels shown below our
122+
output list the possible categories of the geometry type. Now let's check what
123+
CRS this file data is in:
118124

119125
```{r}
120126
st_crs(aoi_boundary_HARV)
121127
```
122128

123-
Our data in the CRS **UTM zone 18N**. The CRS is critical to
124-
interpreting the object's extent values as it specifies units. To find
125-
the extent of our AOI, we can use the `st_bbox()` function:
129+
Our data in the CRS **UTM zone 18N**. The CRS is critical to interpreting the
130+
object's extent values as it specifies units. To find the extent of our AOI, we
131+
can use the `st_bbox()` function:
126132

127133
```{r}
128134
st_bbox(aoi_boundary_HARV)
129135
```
130136

131-
The spatial extent of a shapefile or R spatial object represents the geographic "edge" or location that is the furthest north, south east and west. Thus is represents the overall geographic coverage of the spatial object. Image Source: National Ecological Observatory Network (NEON).
137+
The spatial extent of a shapefile or R spatial object represents the geographic
138+
"edge" or location that is the furthest north, south east and west. Thus is
139+
represents the overall geographic coverage of the spatial object. Image Source:
140+
National Ecological Observatory Network (NEON).
132141

133142
![](fig/dc-spatial-vector/spatial_extent.png){alt='Extent image'}
134143

135-
Lastly, we can view all of the metadata and attributes for this shapefile object
136-
by printing it to the screen:
144+
Lastly, we can view all of the metadata and attributes for this shapefile
145+
object by printing it to the screen:
137146

138147
```{r}
139148
aoi_boundary_HARV
140149
```
141150

142151
## Spatial Data Attributes
143152

144-
We introduced the idea of spatial data attributes in [an earlier lesson](https://datacarpentry.org/organization-geospatial/02-intro-vector-data). Now we will explore
145-
how to use spatial data attributes stored in our data to plot
146-
different features.
153+
We introduced the idea of spatial data attributes in
154+
[an earlier lesson](https://datacarpentry.org/organization-geospatial/02-intro-vector-data).
155+
Now we will explore how to use spatial data attributes stored in our data to
156+
plot different features.
147157

148158
## Plot a Shapefile
149159

150-
Next, let's visualize the data in our `sf` object using the `ggplot`
151-
package. Unlike with raster data, we do not need to convert vector
152-
data to a dataframe before plotting with `ggplot`.
160+
Next, let's visualize the data in our `sf` object using the `ggplot` package.
161+
Unlike with raster data, we do not need to convert vector data to a dataframe
162+
before plotting with `ggplot`.
153163

154-
We're going to customize our boundary plot by setting the
155-
size, color, and fill for our plot. When plotting `sf` objects with `ggplot2`, you need to use the `coord_sf()` coordinate system.
164+
We're going to customize our boundary plot by setting the size, color, and fill
165+
for our plot. When plotting `sf` objects with `ggplot2`, you need to use the
166+
`coord_sf()` coordinate system.
156167

157168
```{r plot-shapefile}
158169
ggplot() +
@@ -206,8 +217,9 @@ st_crs(point_HARV)
206217
st_bbox(point_HARV)
207218
```
208219

209-
To see the number of objects in each file, we can look at the output from when we read these objects into R.
210-
`lines_HARV` contains 13 features (all lines) and `point_HARV` contains only one point.
220+
To see the number of objects in each file, we can look at the output from when
221+
we read these objects into R. `lines_HARV` contains 13 features (all lines) and
222+
`point_HARV` contains only one point.
211223

212224

213225

@@ -221,7 +233,8 @@ To see the number of objects in each file, we can look at the output from when w
221233

222234
- Shapefile metadata include geometry type, CRS, and extent.
223235
- Load spatial objects into R with the `st_read()` function.
224-
- Spatial objects can be plotted directly with `ggplot` using the `geom_sf()` function. No need to convert to a dataframe.
236+
- Spatial objects can be plotted directly with `ggplot` using the `geom_sf()`
237+
function. No need to convert to a dataframe.
225238

226239
::::::::::::::::::::::::::::::::::::::::::::::::::
227240

0 commit comments

Comments
 (0)