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
120
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.
121
+
study area or area of interest. It is often more efficient to crop the raster
122
+
to the extent of our study area to reduce file sizes as we process our data.
123
+
Cropping a raster can also be useful when creating pretty maps so that the
124
+
raster layer matches the extent of the desired vector layers.
120
125
121
126
## Crop a Raster Using Vector Extent
122
127
@@ -125,7 +130,10 @@ spatial object. To do this, we need to specify the raster to be cropped and the
125
130
spatial object that will be used to crop the raster. R will use the `extent` of
126
131
the spatial object as the cropping boundary.
127
132
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.
133
+
To illustrate this, we will crop the Canopy Height Model (CHM) to only include
134
+
the area of interest (AOI). Let's start by plotting the full extent of the CHM
135
+
data and overlay where the AOI falls within it. The boundaries of the AOI will
136
+
be colored blue, and we use `fill = NA` to make the area transparent.
129
137
130
138
```{r crop-by-vector-extent}
131
139
ggplot() +
@@ -144,12 +152,12 @@ that falls within the boundaries of the AOI.
144
152
CHM_HARV_Cropped <- crop(x = CHM_HARV, y = aoi_boundary_HARV)
145
153
```
146
154
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:
155
+
Now we can plot the cropped CHM data, along with a boundary box showing the
156
+
full CHM extent. However, remember, since this is raster data, we need to
157
+
convert to a data frame in order to plot using `ggplot`. To get the boundary
158
+
box from CHM, the `st_bbox()` will extract the 4 corners of the rectangle that
159
+
encompass all the features contained in this object. The `st_as_sfc()` converts
160
+
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.
291
+
Now we can plot this cropped data. We will show the AOI boundary on the same
292
+
plot for scale.
282
293
283
294
```{r show-manual-crop-area}
284
295
ggplot() +
@@ -292,38 +303,41 @@ ggplot() +
292
303
## Extract Raster Pixels Values Using Vector Polygons
293
304
294
305
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).
306
+
for example, plot locations that we are sampling on the ground. We can extract
307
+
all pixel values within 20m of our x,y point of interest. These can then be
308
+
summarized into some value of interest (e.g. mean, maximum, total).
296
309
297
-

310
+
{alt='Image shows raster information extraction using 20m polygon boundary.'}
311
+
Image Source: National Ecological Observatory Network (NEON)
298
312
299
313
To do this in R, we use the `extract()` function. The `extract()` function
300
314
requires:
301
315
302
316
- The raster that we wish to extract values from,
303
317
- The vector layer containing the polygons that we wish to use as a boundary or
304
318
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.) .
319
+
- we can tell it to store the output values in a data frame using
320
+
`raw = FALSE` (this is optional).
307
321
308
322
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.
323
+
`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.
361
+
of summary statistic we are interested in using the `fun =` argument. Let's
362
+
extract a mean height value for our AOI.
351
363
352
364
```{r summarize-extract}
353
-
mean_tree_height_AOI <- extract(x = CHM_HARV, y = aoi_boundary_HARV, fun = mean)
365
+
mean_tree_height_AOI <- extract(x = CHM_HARV, y = aoi_boundary_HARV,
366
+
fun = mean)
354
367
355
368
mean_tree_height_AOI
356
369
```
@@ -361,23 +374,22 @@ canopy height model is 22.43 meters.
361
374
## Extract Data using x,y Locations
362
375
363
376
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.
368
-
369
-

377
+
surrounding individual point locations using the `st_buffer()` function. To do
378
+
this we define the summary argument (`fun = mean`) and the buffer distance
379
+
(`dist = 20`) which represents the radius of a circular region around each
380
+
point. By default, the units of the buffer are the same units as the data's
381
+
CRS. All pixels that are touched by the buffer region are included in the
382
+
extract.
370
383
371
-
Source: National Ecological Observatory Network (NEON).
384
+
{alt='Image shows raster information extraction using 20m buffer region.'}
385
+
Image Source: National Ecological Observatory Network (NEON)
372
386
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.
387
+
Let's put this into practice by figuring out the mean tree height in the 20m
388
+
around the tower location (`point_HARV`).
376
389
377
390
```{r extract-point-to-buffer}
378
391
mean_tree_height_tower <- extract(x = CHM_HARV,
379
-
y = point_HARV,
380
-
buffer = 20,
392
+
y = st_buffer(point_HARV, dist = 20),
381
393
fun = mean)
382
394
383
395
mean_tree_height_tower
@@ -387,11 +399,10 @@ mean_tree_height_tower
387
399
388
400
## Challenge: Extract Raster Height Values For Plot Locations
389
401
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.
402
+
1) Use the plot locations object (`plot_locations_sp_HARV`) to extract an
403
+
average tree height for the area within 20m of each vegetation plot location
404
+
in the study area. Because there are multiple plot locations, there will be
405
+
multiple averages returned.
395
406
396
407
2) Create a plot showing the mean tree height of each area.
0 commit comments