File: exact_extract.Rd

package info (click to toggle)
r-cran-exactextractr 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,152 kB
  • sloc: cpp: 7,238; sh: 14; makefile: 2
file content (555 lines) | stat: -rw-r--r-- 20,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/exact_extract.R
\name{exact_extract}
\alias{exact_extract}
\alias{exact_extract,Raster,sf-method}
\alias{exact_extract,Raster,SpatialPolygonsDataFrame-method}
\alias{exact_extract,Raster,SpatialPolygons-method}
\alias{exact_extract,Raster,sfc_MULTIPOLYGON-method}
\alias{exact_extract,Raster,sfc_POLYGON-method}
\alias{exact_extract,Raster,sfc_GEOMETRY-method}
\alias{exact_extract,Raster,sfc_GEOMETRYCOLLECTION-method}
\alias{exact_extract,SpatRaster,sf-method}
\alias{exact_extract,SpatRaster,SpatialPolygonsDataFrame-method}
\alias{exact_extract,SpatRaster,SpatialPolygons-method}
\alias{exact_extract,SpatRaster,sfc_MULTIPOLYGON-method}
\alias{exact_extract,SpatRaster,sfc_POLYGON-method}
\alias{exact_extract,SpatRaster,sfc_GEOMETRY-method}
\alias{exact_extract,SpatRaster,sfc_GEOMETRYCOLLECTION-method}
\title{Extract or summarize values from rasters}
\usage{
\S4method{exact_extract}{Raster,sf}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{Raster,SpatialPolygonsDataFrame}(x, y, ...)

\S4method{exact_extract}{Raster,SpatialPolygons}(x, y, ...)

\S4method{exact_extract}{Raster,sfc_MULTIPOLYGON}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{Raster,sfc_POLYGON}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{Raster,sfc_GEOMETRY}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{Raster,sfc_GEOMETRYCOLLECTION}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{SpatRaster,sf}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{SpatRaster,SpatialPolygonsDataFrame}(x, y, ...)

\S4method{exact_extract}{SpatRaster,SpatialPolygons}(x, y, ...)

\S4method{exact_extract}{SpatRaster,sfc_MULTIPOLYGON}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{SpatRaster,sfc_POLYGON}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{SpatRaster,sfc_GEOMETRY}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)

\S4method{exact_extract}{SpatRaster,sfc_GEOMETRYCOLLECTION}(
  x,
  y,
  fun = NULL,
  ...,
  weights = NULL,
  append_cols = NULL,
  coverage_area = FALSE,
  default_value = NA_real_,
  default_weight = NA_real_,
  include_area = FALSE,
  include_cell = FALSE,
  include_cols = NULL,
  include_xy = FALSE,
  force_df = FALSE,
  full_colnames = FALSE,
  stack_apply = FALSE,
  summarize_df = FALSE,
  quantiles = NULL,
  progress = TRUE,
  max_cells_in_memory = 3e+07,
  grid_compat_tol = 0.001,
  colname_fun = NULL
)
}
\arguments{
\item{x}{a \code{RasterLayer}, \code{RasterStack}, \code{RasterBrick}, or \code{SpatRaster}}

\item{y}{a \code{sf}, \code{sfc}, \code{SpatialPolygonsDataFrame}, or \code{SpatialPolygons}
object with polygonal geometries}

\item{fun}{an optional function or character vector, as described below}

\item{...}{additional arguments to pass to \code{fun}}

\item{weights}{a weighting raster to be used with the \code{weighted_mean}
and \code{weighted_sum} summary operations or a user-defined
summary function. When \code{weights} is set to \code{'area'}, the
cell areas of \code{x} will be calculated and used as weights.}

\item{append_cols}{when \code{fun} is not \code{NULL}, an optional character vector
of columns from \code{y} to be included in returned data frame.}

\item{coverage_area}{if \code{TRUE}, output pixel \code{coverage_area}
instead of \code{coverage_fraction}}

\item{default_value}{an optional value to use instead of \code{NA} in \code{x}}

\item{default_weight}{an optional value to use instead of \code{NA} in \code{weights}}

\item{include_area}{if \code{TRUE}, and \code{fun} is \code{NULL}, augment
the data frame for each feature with a column
for the cell area. If the units of the raster CRS are
degrees, the area in square meters will be calculated
based on a spherical approximation of Earth. Otherwise,
a Cartesian area will be calculated (and will be the
same for all pixels.) If \code{TRUE} and \code{fun} is
not \code{NULL}, add \code{area} to the data frame passed
to \code{fun} for each feature.}

\item{include_cell}{if \code{TRUE}, and \code{fun} is \code{NULL}, augment
the data frame for each feature with a column
for the cell index (\code{cell}). If \code{TRUE} and
\code{fun} is not \code{NULL}, add \code{cell} to the
data frame passed to \code{fun} for each feature.}

\item{include_cols}{an optional character vector of column names in
\code{y} to be added to the data frame for each
feature that is either returned (when \code{fun} is
\code{NULL}) or passed to \code{fun}.}

\item{include_xy}{if \code{TRUE}, and \code{fun} is \code{NULL}, augment
the returned data frame for each feature with columns
for cell center coordinates (\code{x} and \code{y}). If
\code{TRUE} and \code{fun} is not \code{NULL}, add
\code{x} and \code{y} to the data frame passed to \code{fun}
for each feature.}

\item{force_df}{always return a data frame instead of a vector, even if
\code{x} has only one layer and \code{fun} has length 1}

\item{full_colnames}{include the names of \code{x} and \code{weights} in
the names of the data frame for each feature, even if
\code{x} or \code{weights} has only one layer.
This is useful when the results of multiple
calls to \code{exact_extract} are combined with
\code{cbind}.}

\item{stack_apply}{if \code{TRUE}, apply \code{fun} independently to
each layer or \code{x} (and its corresponding layer
of \code{weights}, if provided.) The number of
layers in \code{x} and \code{weights} must equal
each other or \code{1}, in which case the
single layer raster will be recycled.
If \code{FALSE}, apply \code{fun} to all layers of
\code{x} (and \code{weights}) simultaneously.}

\item{summarize_df}{pass values, coverage fraction/area, and weights to
\code{fun} as a single data frame instead of
separate arguments.}

\item{quantiles}{quantiles to be computed when \code{fun = 'quantile'}}

\item{progress}{if \code{TRUE}, display a progress bar during processing}

\item{max_cells_in_memory}{the maximum number of raster cells to load at
a given time when using a named summary operation
for \code{fun} (as opposed to a function defined using
R code). If a polygon covers more than \code{max_cells_in_memory}
raster cells, it will be processed in multiple chunks.}

\item{grid_compat_tol}{require value and weight grids to align within
\code{grid_compat_tol} times the smaller of the two
grid resolutions.}

\item{colname_fun}{an optional function used to construct column names.
Should accept arguments \code{values} (name of value layer),
\code{weights} (name of weight layer), \code{fun_name} (value of
\code{fun}), \code{fun_value} (value associated with \code{fun}, for
\verb{fun \%in\% c('quantile', 'frac', 'weighted_frac)}
\code{nvalues} (number of value layers), \code{weights}
(number of weight layers)}
}
\value{
a vector, data frame, or list of data frames, depending on the type
of \code{x} and the value of \code{fun} (see Details)
}
\description{
Extracts the values of cells in a raster (\code{RasterLayer}, \code{RasterStack}
\code{RasterBrick}, or \code{SpatRaster}) that are covered by polygons in a
simple feature collection (\code{sf} or \code{sfc}) or \code{SpatialPolygonsDataFrame}.
Returns either a summary of the extracted values or the extracted values
themselves.
}
\details{
\code{exact_extract} extracts the values of cells in a raster that are covered
by polygonal features in a simple feature collection (\code{sf} or \code{sfc}) or
\code{SpatialPolygonDataFrame}, as well as the fraction or area of each cell that
is covered by the feature. Pixels covered by all parts of the polygon are
considered. If an (invalid) multipart polygon covers the same pixels more
than once, the pixel may have a coverage fraction greater than one.

The function can either return pixel values directly to the caller, or can
return the result of a predefined summary operation or user-defined R
function applied to the values. These three approaches are described in the
subsections below.
\subsection{Returning extracted values directly}{

If \code{fun} is not specified, \code{exact_extract} will return a list with
one data frame for each feature in the input feature collection. The data
frame will contain a column with cell values from each layer in the input
raster (and optional weighting raster) and a column indicating
the fraction or area of the cell that is covered by the polygon.

If the input rasters have only one layer, the value and weight columns in the
data frame will be named \code{values} or \code{weights}. When the input rasters have
more than one layer, the columns will be named according to \code{names(x)} and
\code{names(weights)}. The column containing pixel coverage will be called
\code{coverage_fraction} when \code{coverage_area = FALSE}, or \code{coverage_area} when
\code{coverage_area = TRUE}. Additional columns can be added to the returned data
frames with the \code{include_area}, \code{include_cell}, and \code{include_xy} arguments.

If the output data frames for multiple features are to be combined (e.g.,
with \code{rbind}), it may be useful to include identifying column(s) from the
input features in the returned data frames using \code{include_cols}.
}

\subsection{Predefined summary operations}{

Often the individual pixel values are not needed; only one or more summary
statistics (e.g., mean, sum) is required for each feature. Common summary
statistics can be calculated by \code{exact_extract} directly using a predefined
summary operation. Where possible, this approach is advantageous because it
allows the package to calculate the statistics incrementally, avoiding the
need to store all pixel values in memory at the same time. This allows the
package to process arbitrarily large data with a small amount of memory. (The
\code{max_pixels_in_memory} argument can be used to fine-tune the amount of memory
made available to \code{exact_extract}.)

To summarize pixel values using a predefined summary option, \code{fun} should be
set to a character vector of one or more operation names. If the input raster
has a single layer and a single summary operation is specified,
\code{exact_extract} will return a vector with the result of the summary operation
for each feature in the input. If the input raster has multiple layers, or if
multiple summary operations are specified, \code{exact_extract} will return a data
frame with a row for each feature and a column for each summary operation /
layer combination. (The \code{force_df} option can be used to always return a data
frame instead of a vector.)

The following summary operations are supported:
\itemize{
\item \code{min} - the minimum non-\code{NA} value in any raster cell wholly or
partially covered by the polygon
\item \code{max} - the maximum non-\code{NA} value in any raster cell wholly or
partially covered by the polygon
\item \code{count} - the sum of fractions of raster cells with non-\code{NA}
values covered by the polygon
\item \code{sum}   - the sum of non-\code{NA} raster cell values, multiplied by
the fraction of the cell that is covered by the polygon
\item \code{mean} - the mean cell value, weighted by the fraction of each cell
that is covered by the polygon
\item \code{median} - the median cell value, weighted by the fraction of each cell
that is covered by the polygon
\item \code{quantile} - arbitrary quantile(s) of cell values, specified in
\code{quantiles}, weighted by the fraction of each cell that is
covered by the polygon
\item \code{mode} - the most common cell value, weighted by the fraction of
each cell that is covered by the polygon. Where multiple
values occupy the same maximum number of weighted cells,
the largest value will be returned.
\item \code{majority} - synonym for \code{mode}
\item \code{minority} - the least common cell value, weighted by the fraction
of each cell that is covered by the polygon. Where
multiple values occupy the same minimum number of
weighted cells, the smallest value will be returned.
\item \code{variety} - the number of distinct values in cells that are wholly or
partially covered by the polygon.
\item \code{variance} - the population variance of cell values, weighted by the
fraction of each cell that is covered by the polygon.
\item \code{stdev} - the population standard deviation of cell values, weighted by
the fraction of each cell that is covered by the polygon.
\item \code{coefficient_of_variation} - the population coefficient of variation of
cell values, weighted by the fraction of each
cell that is covered by the polygon.
\item \code{weighted_mean} - the mean cell value, weighted by the product of
the fraction of each cell covered by the polygon
and the value of a second weighting raster provided
as \code{weights}
\item \code{weighted_sum} - the sum of defined raster cell values, multiplied by
the fraction of each cell that is covered by the polygon
and the value of a second weighting raster provided
as \code{weights}
\item \code{weighted_stdev} - the population standard deviation of cell values,
weighted by the product of the fraction of each cell
covered by the polygon and the value of a second
weighting raster provided as \code{weights}
\item \code{weighted_variance} - the population variance of cell values, weighted by
the product of the fraction of each cell covered by
the polygon and the value of a second weighting
raster provided as \code{weights}
\item \code{frac} - returns one column for each possible value of \code{x}, with the
the fraction of defined raster cells that are equal to that
value.
\item \code{weighted_frac} - returns one column for each possible value of \code{x},
with the fraction of defined cells that are equal
to that value, weighted by \code{weights.}
}

In all of the summary operations, \code{NA} values in the the primary raster (\code{x})
raster are ignored (i.e., \code{na.rm = TRUE}.) If \code{NA} values occur in the
weighting raster, the result of the weighted operation will be \code{NA}. \code{NA}
values in both \code{x} and \code{weights} can be replaced on-the-fly using the
\code{default_value} and \code{default_weight} arguments.
}

\subsection{User-defined summary functions}{

If no predefined summary operation is suitable, a user-defined R function may
be provided as \code{fun}. The function will be called once for each feature and
must return either a single value or a data frame. The results of the
function for each feature will be combined and returned by \code{exact_extract}.

The simplest way to write a summary function is to set
argument \code{summarize_df = TRUE}. (For backwards compatibility, this is not the
default.) In this mode, the summary function takes the signature
\verb{function(df, ...)} where \code{df} is the same data frame that would be returned
by \code{exact_extract} with \code{fun = NULL}.

With \code{summarize_df = FALSE}, the function must have the signature
\verb{function(values, coverage_fractions, ...)} when weights are not used, and
\verb{function(values, coverage_fractions, weights, ...)} when weights are used.
If the value and weight rasters each have a single layer, the function arguments
will be vectors; if either has multiple layers, the function arguments will
be data frames, with column names taken from the names of the value/weight
rasters. Values brought in through the \code{include_xy}, \code{include_area},
\code{include_cell}, and \code{include_cols} arguments will be added to the \code{values}
data frame. For most applications, it is simpler to set \code{summarize_df = TRUE}
and work with all inputs in a single data frame.
}
}
\examples{
rast <- raster::raster(matrix(1:100, ncol=10), xmn=0, ymn=0, xmx=10, ymx=10)
poly <- sf::st_as_sfc('POLYGON ((2 2, 7 6, 4 9, 2 2))')

# named summary operation on RasterLayer, returns vector
exact_extract(rast, poly, 'mean')

# two named summary operations on RasterLayer, returns data frame
exact_extract(rast, poly, c('min', 'max'))

# named summary operation on RasterStack, returns data frame
stk <- raster::stack(list(a=rast, b=sqrt(rast)))
exact_extract(stk, poly, 'mean')

# named weighted summary operation, returns vector
weights <- raster::raster(matrix(runif(100), ncol=10), xmn=0, ymn=0, xmx=10, ymx=10)
exact_extract(rast, poly, 'weighted_mean', weights=weights)

# custom summary function, returns vector
exact_extract(rast, poly, function(value, cov_frac) length(value[cov_frac > 0.9]))

}