You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Frequent use cases of cropping a raster file include reducing file size and
115
119
creating maps. Sometimes we have a raster file that is much larger than our
116
-
study area or area of interest. It is often more efficient to crop the
117
-
raster to the extent of our study area to reduce file sizes as we process
118
-
our data. Cropping a raster can also be useful when creating pretty maps so
119
-
that the raster layer matches the extent of the desired vector layers.
120
+
study area or area of interest. It is often more efficient to crop the raster
121
+
to the extent of our study area to reduce file sizes as we process our data.
122
+
Cropping a raster can also be useful when creating pretty maps so that the
123
+
raster layer matches the extent of the desired vector layers.
120
124
121
125
## Crop a Raster Using Vector Extent
122
126
@@ -125,7 +129,10 @@ spatial object. To do this, we need to specify the raster to be cropped and the
125
129
spatial object that will be used to crop the raster. R will use the `extent` of
126
130
the spatial object as the cropping boundary.
127
131
128
-
To illustrate this, we will crop the Canopy Height Model (CHM) to only include the area of interest (AOI). Let's start by plotting the full extent of the CHM data and overlay where the AOI falls within it. The boundaries of the AOI will be colored blue, and we use `fill = NA` to make the area transparent.
132
+
To illustrate this, we will crop the Canopy Height Model (CHM) to only include
133
+
the area of interest (AOI). Let's start by plotting the full extent of the CHM
134
+
data and overlay where the AOI falls within it. The boundaries of the AOI will
135
+
be colored blue, and we use `fill = NA` to make the area transparent.
129
136
130
137
```{r crop-by-vector-extent}
131
138
ggplot() +
@@ -144,12 +151,12 @@ that falls within the boundaries of the AOI.
144
151
CHM_HARV_Cropped <- crop(x = CHM_HARV, y = aoi_boundary_HARV)
145
152
```
146
153
147
-
Now we can plot the cropped CHM data, along with a boundary box showing the full
148
-
CHM extent. However, remember, since this is raster data, we need to convert to
149
-
a data frame in order to plot using `ggplot`. To get the boundary box from CHM,
150
-
the `st_bbox()` will extract the 4 corners of the rectangle that encompass all
151
-
the features contained in this object. The `st_as_sfc()` converts these 4
152
-
coordinates into a polygon that we can plot:
154
+
Now we can plot the cropped CHM data, along with a boundary box showing the
155
+
full CHM extent. However, remember, since this is raster data, we need to
156
+
convert to a data frame in order to plot using `ggplot`. To get the boundary
157
+
box from CHM, the `st_bbox()` will extract the 4 corners of the rectangle that
158
+
encompass all the features contained in this object. The `st_as_sfc()` converts
159
+
these 4 coordinates into a polygon that we can plot:
Now we can plot this cropped data. We will show the AOI boundary on the same plot for scale.
289
+
Now we can plot this cropped data. We will show the AOI boundary on the same
290
+
plot for scale.
282
291
283
292
```{r show-manual-crop-area}
284
293
ggplot() +
@@ -292,38 +301,40 @@ ggplot() +
292
301
## Extract Raster Pixels Values Using Vector Polygons
293
302
294
303
Often we want to extract values from a raster layer for particular locations -
295
-
for example, plot locations that we are sampling on the ground. We can extract all pixel values within 20m of our x,y point of interest. These can then be summarized into some value of interest (e.g. mean, maximum, total).
304
+
for example, plot locations that we are sampling on the ground. We can extract
305
+
all pixel values within 20m of our x,y point of interest. These can then be
306
+
summarized into some value of interest (e.g. mean, maximum, total).
296
307
297
-

308
+
{alt = "Extract raster information using a polygon boundary. From https://www.neonscience.org/sites/default/files/images/spatialData/BufferSquare.png"}
298
309
299
310
To do this in R, we use the `extract()` function. The `extract()` function
300
311
requires:
301
312
302
313
- The raster that we wish to extract values from,
303
314
- The vector layer containing the polygons that we wish to use as a boundary or
304
315
boundaries,
305
-
- we can tell it to store the output values in a data frame using
306
-
`df = TRUE`. (This is optional, the default is to return a list, NOT a data frame.) .
316
+
- we can tell it to store the output values in a data frame using
317
+
`raw = FALSE` (this is optional).
307
318
308
319
We will begin by extracting all canopy height pixel values located within our
309
-
`aoi_boundary_HARV` polygon which surrounds the tower located at the NEON Harvard
310
-
Forest field site.
320
+
`aoi_boundary_HARV` polygon which surrounds the tower located at the NEON
We often want to extract summary values from a raster. We can tell R the type
348
-
of summary statistic we are interested in using the `fun =` argument. Let's extract
349
-
a mean height value for our AOI. Because we are extracting only a single number, we will
350
-
not use the `df = TRUE` argument.
358
+
of summary statistic we are interested in using the `fun =` argument. Let's
359
+
extract a mean height value for our AOI.
351
360
352
361
```{r summarize-extract}
353
-
mean_tree_height_AOI <- extract(x = CHM_HARV, y = aoi_boundary_HARV, fun = mean)
362
+
mean_tree_height_AOI <- extract(x = CHM_HARV, y = aoi_boundary_HARV,
363
+
fun = mean)
354
364
355
365
mean_tree_height_AOI
356
366
```
@@ -361,23 +371,23 @@ canopy height model is 22.43 meters.
361
371
## Extract Data using x,y Locations
362
372
363
373
We can also extract pixel values from a raster by defining a buffer or area
364
-
surrounding individual point locations using the `extract()` function. To do this
365
-
we define the summary argument (`fun = mean`) and the buffer distance (`buffer = 20`)
366
-
which represents the radius of a circular region around each point. By default, the units of the
367
-
buffer are the same units as the data's CRS. All pixels that are touched by the buffer region are included in the extract.
374
+
surrounding individual point locations using the `st_buffer()` function. To do
375
+
this we define the summary argument (`fun = mean`) and the buffer distance
376
+
(`dist = 20`) which represents the radius of a circular region around each
377
+
point. By default, the units of the buffer are the same units as the data's
378
+
CRS. All pixels that are touched by the buffer region are included in the
379
+
extract.
368
380
369
-

381
+
{alt = "Extract raster information using a buffer region. From: https://www.neonscience.org/sites/default/files/images/spatialData/BufferCircular.png"}
370
382
371
383
Source: National Ecological Observatory Network (NEON).
372
384
373
-
Let's put this into practice by figuring out the mean tree height in the
374
-
20m around the tower location (`point_HARV`). Because we are extracting only a single number, we
375
-
will not use the `df = TRUE` argument.
385
+
Let's put this into practice by figuring out the mean tree height in the 20m
386
+
around the tower location (`point_HARV`).
376
387
377
388
```{r extract-point-to-buffer}
378
389
mean_tree_height_tower <- extract(x = CHM_HARV,
379
-
y = point_HARV,
380
-
buffer = 20,
390
+
y = st_buffer(point_HARV, dist = 20),
381
391
fun = mean)
382
392
383
393
mean_tree_height_tower
@@ -387,11 +397,10 @@ mean_tree_height_tower
387
397
388
398
## Challenge: Extract Raster Height Values For Plot Locations
389
399
390
-
1) Use the plot locations object (`plot_locations_sp_HARV`)
391
-
to extract an average tree height for the
392
-
area within 20m of each vegetation plot location in the study area. Because there are
393
-
multiple plot locations, there will be multiple averages returned, so the `df = TRUE`
394
-
argument should be used.
400
+
1) Use the plot locations object (`plot_locations_sp_HARV`) to extract an
401
+
average tree height for the area within 20m of each vegetation plot location
402
+
in the study area. Because there are multiple plot locations, there will be
403
+
multiple averages returned.
395
404
396
405
2) Create a plot showing the mean tree height of each area.
0 commit comments