1
1
---
2
- title : Open and Plot Shapefiles
2
+ title : Open and Plot Vector Layers
3
3
teaching : 20
4
4
exercises : 10
5
5
source : Rmd
@@ -12,7 +12,7 @@ source("setup.R")
12
12
::::::::::::::::::::::::::::::::::::::: objectives
13
13
14
14
- Know the difference between point, line, and polygon vector elements.
15
- - Load point, line, and polygon shapefiles into R.
15
+ - Load point, line, and polygon vector layers into R.
16
16
- Access the attributes of a spatial object in R.
17
17
18
18
::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -34,44 +34,44 @@ library(sf)
34
34
35
35
## Things You'll Need To Complete This Episode
36
36
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
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
39
episode.
40
40
41
41
42
42
::::::::::::::::::::::::::::::::::::::::::::::::::
43
43
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
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 loaded from ESRI's ` shapefile ` format into 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
51
into a single plot.
52
52
53
- ## Import Shapefiles
53
+ ## Import Vector Data
54
54
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
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
58
you have the ` sf ` library loaded.
59
59
60
60
``` {r load-sf, results="hide", eval=FALSE, message=FALSE}
61
61
library(sf)
62
62
```
63
63
64
- The shapefiles that we will import are:
64
+ The vector layers that we will import from ESRI's ` shapefile ` format are:
65
65
66
- - A polygon shapefile representing our field site boundary,
67
- - A line shapefile representing roads, and
68
- - A point shapefile representing the location of the [ Fisher flux tower] ( https://www.neonscience.org/data-collection/flux-tower-measurements )
66
+ - A polygon vector layer representing our field site boundary,
67
+ - A line vector layer representing roads, and
68
+ - A point vector layer representing the location of the [ Fisher flux tower] ( https://www.neonscience.org/data-collection/flux-tower-measurements )
69
69
located at the [ NEON Harvard Forest field site] ( https://www.neonscience.org/field-sites/field-sites-map/HARV ) .
70
70
71
- The first shapefile that we will open contains the boundary of our study area
71
+ The first vector layer that we will open contains the boundary of our study area
72
72
(or our Area Of Interest or AOI, hence the name ` aoiBoundary ` ). To import
73
- shapefiles we use the ` sf ` function ` st_read() ` . ` st_read() ` requires the file
74
- path to the shapefile.
73
+ a vector layer from an ESRI ` shapefile ` we use the ` sf ` function ` st_read() ` . ` st_read() `
74
+ requires the file path to the ESRI ` shapefile ` .
75
75
76
76
Let's import our AOI:
77
77
@@ -80,20 +80,20 @@ aoi_boundary_HARV <- st_read(
80
80
"data/NEON-DS-Site-Layout-Files/HARV/HarClip_UTMZ18.shp")
81
81
```
82
82
83
- ## Shapefile Metadata \& Attributes
83
+ ## Vector Layer Metadata \& Attributes
84
84
85
- When we import the ` HarClip_UTMZ18 ` shapefile layer into R (as our
85
+ When we import the ` HarClip_UTMZ18 ` vector layer from an ESRI ` shapefile ` into R (as our
86
86
` aoi_boundary_HARV ` object), the ` st_read() ` function automatically stores
87
87
information about the data. We are particularly interested in the geospatial
88
- metadata, describing the format, CRS, extent, and other components of the
89
- vector data, and the attributes which describe properties associated with each
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
90
individual vector object.
91
91
92
92
::::::::::::::::::::::::::::::::::::::::: callout
93
93
94
94
## Data Tip
95
95
96
- The [ Explore and Plot by Shapefile Attributes] ( 07-vector-shapefile-attributes-in-r/ )
96
+ The [ Explore and Plot by Vector Layer Attributes] ( 07-vector-shapefile-attributes-in-r/ )
97
97
episode provides more information on both metadata and attributes
98
98
and using attributes to subset and plot data.
99
99
@@ -102,46 +102,46 @@ and using attributes to subset and plot data.
102
102
103
103
## Spatial Metadata
104
104
105
- Key metadata for all shapefiles include :
105
+ Key metadata for all vector layers includes :
106
106
107
107
1 . ** Object Type:** the class of the imported object.
108
108
2 . ** Coordinate Reference System (CRS):** the projection of the data.
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 .
109
+ 3 . ** Extent:** the spatial extent (i.e. geographic area that the vector layer
110
+ covers) of the data . Note that the spatial extent for a vector layer
111
+ represents the combined extent for all individual objects in the vector layer .
112
112
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 :
113
+ We can view metadata of a vector layer using the ` st_geometry_type() ` , ` st_crs() ` and
114
+ ` st_bbox() ` functions. First, let's view the geometry type for our AOI
115
+ vector layer :
116
116
117
117
``` {r}
118
118
st_geometry_type(aoi_boundary_HARV)
119
119
```
120
120
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
121
+ Our ` aoi_boundary_HARV ` is a polygon spatial object. The 18 levels shown below our
122
+ output list the possible categories of the geometry type. Now let's check what
123
123
CRS this file data is in:
124
124
125
125
``` {r}
126
126
st_crs(aoi_boundary_HARV)
127
127
```
128
128
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
129
+ Our data in the CRS ** UTM zone 18N** . The CRS is critical to interpreting the
130
+ spatial object's extent values as it specifies units. To find the extent of our AOI, we
131
131
can use the ` st_bbox() ` function:
132
132
133
133
``` {r}
134
134
st_bbox(aoi_boundary_HARV)
135
135
```
136
136
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:
137
+ The spatial extent of a vector layer or R spatial object represents the geographic
138
+ "edge" or location that is the furthest north, south east and west. Thus it
139
+ represents the overall geographic coverage of the spatial object. Image Source:
140
140
National Ecological Observatory Network (NEON).
141
141
142
142
![ ] ( fig/dc-spatial-vector/spatial_extent.png ) {alt='Extent image'}
143
143
144
- Lastly, we can view all of the metadata and attributes for this shapefile
144
+ Lastly, we can view all of the metadata and attributes for this R spatial
145
145
object by printing it to the screen:
146
146
147
147
``` {r}
@@ -150,33 +150,33 @@ aoi_boundary_HARV
150
150
151
151
## Spatial Data Attributes
152
152
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
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
156
plot different features.
157
157
158
- ## Plot a Shapefile
158
+ ## Plot a vector layer
159
159
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
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
162
before plotting with ` ggplot ` .
163
163
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
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
166
` coord_sf() ` coordinate system.
167
167
168
168
``` {r plot-shapefile}
169
- ggplot() +
170
- geom_sf(data = aoi_boundary_HARV, size = 3, color = "black", fill = "cyan1") +
171
- ggtitle("AOI Boundary Plot") +
169
+ ggplot() +
170
+ geom_sf(data = aoi_boundary_HARV, size = 3, color = "black", fill = "cyan1") +
171
+ ggtitle("AOI Boundary Plot") +
172
172
coord_sf()
173
173
```
174
174
175
175
::::::::::::::::::::::::::::::::::::::: challenge
176
176
177
- ## Challenge: Import Line and Point Shapefiles
177
+ ## Challenge: Import Line and Point Vector Layers
178
178
179
- Using the steps above, import the HARV\_ roads and HARVtower\_ UTM18N layers into
179
+ Using the steps above, import the HARV\_ roads and HARVtower\_ UTM18N vector layers into
180
180
R. Call the HARV\_ roads object ` lines_HARV ` and the HARVtower\_ UTM18N
181
181
` point_HARV ` .
182
182
@@ -217,8 +217,8 @@ st_crs(point_HARV)
217
217
st_bbox(point_HARV)
218
218
```
219
219
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
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
222
` point_HARV ` contains only one point.
223
223
224
224
@@ -231,9 +231,9 @@ we read these objects into R. `lines_HARV` contains 13 features (all lines) and
231
231
232
232
:::::::::::::::::::::::::::::::::::::::: keypoints
233
233
234
- - Shapefile metadata include geometry type, CRS, and extent.
234
+ - Metadata for vector layers include geometry type, CRS, and extent.
235
235
- Load spatial objects into R with the ` st_read() ` function.
236
- - Spatial objects can be plotted directly with ` ggplot ` using the ` geom_sf() `
236
+ - Spatial objects can be plotted directly with ` ggplot ` using the ` geom_sf() `
237
237
function. No need to convert to a dataframe.
238
238
239
239
::::::::::::::::::::::::::::::::::::::::::::::::::
0 commit comments