exactextract
provides a fast and accurate algorithm for summarizing values in the portion of a raster dataset that is covered by a polygon, often referred to as zonal statistics. Unlike other zonal statistics implementations, it takes into account raster cells that are partially covered by the polygon.
Accurate zonal statistics calculation requires determining the fraction of each raster cell that is covered by the polygon. In a naive solution to the problem, each raster cell can be expressed as a polygon whose intersection with the input polygon is computed using polygon clipping routines such as those offered in JTS, GEOS, CGAL, or other libraries. However, polygon clipping algorithms are relatively expensive, and the performance of this approach is typically unacceptable unless raster resolution and polygon complexity are low.
To achieve better performance, most zonal statistics implementations sacrifice accuracy by assuming that each cell of the raster is either wholly inside or outside of the polygon. This inside/outside determination can take various forms, for example:
exactextract
exactextract
computes the portion of each cell that is covered by a polygon using an algorithm that proceeds as follows:
0
or 1
). A point-in-polygon test is used to determine which, and the 0
or 1
value is then propagated outward using a flood fill algorithm. Depending on the structure of the polygon, a handful of point-in-polygon tests may be necessary.exactextract
can compute statistics against two rasters simultaneously, with a second raster containing weighting values.
The weighting raster does not need to have the same resolution and extent as the value raster, but the resolutions of the two rasters must be integer multiple of each other, and any difference between the grid origin points must be an integer multiple of the smallest cell size.
exactextract
requires the following:
It can be built as follows on Linux as follows:
git clone https://github.com/isciences/exactextract
cd exactextract
mkdir cmake-build-release
cd cmake-build-release
cmake -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install
There are three options available to control what gets compiled. They are each ON by default.
- BUILD_CLI
will build main program (which requires GDAL)
- BUILD_TEST
will build the catch_test suite
- BUILD_DOC
will build the doxygen documentation if doxygen is available
To build just the library and test suite, you can use these options as follows to turn off the CLI (which means GDAL isn't required) and disable the documentation build. The tests and library are built, the tests run, and the library installed if the tests were run successfully:
git clone https://github.com/isciences/exactextract
cd exactextract
mkdir cmake-build-release
cd cmake-build-release
cmake -DBUILD_CLI:=OFF -DBUILD_DOC:=OFF -DCMAKE_BUILD_TYPE=Release ..
make
./catch_tests && sudo make install
exactextract
exactextract
provides a simple command-line interface that uses GDAL to read a vector data source and one or more raster files, perform zonal statistics, and write output to a CSV, netCDF, or other tabular formats supported by GDAL.
In addition to the command-line executable, an R package (exactextractr
) allows some functionality of exactextract
to be used with R sf
and raster
objects.
Command line documentation can be accessed by exactextract -h
.
A minimal usage is as follows, in which we want to compute a mean temperature for each country:
exactextract \
-r "temp:temperature_2018.tif" \
-p countries.shp \
-f country_name \
-s "mean(temp)" \
-o mean_temperature.csv
In this example, exactextract
will summarize temperatures stored in temperature_2018.tif
over the country boundaries stored in countries.shp
.
* The -r
argument provides the location for of the raster input and specifies that we'd like to refer to it later on using the name temp
.
The location may be specified as a filename or any other location understood by GDAL.
For example, a single variable within a netCDF file can be accessed using -r temp:NETCDF:outputs.nc:tmp2m
.
In files with more than one band, the band number (1-indexed) can be specified using square brackets, e.g., -r temp:temperature.tif[4]
.
* The -p
argument provides the location for the polygon input.
As with the -r
argument, this can be a file name or some other location understood by GDAL, such as a PostGIS vector source (-p "PG:dbname=basins[public.basins_lev05]"
).
* The -f
argument indicates that we'd like the field country_name
from the shapefile to be included as a field in the output file.
* The -s
argument instructs exactextract
to compute the mean of the raster we refer to as temp
for each polygon.
These values will be stored as a field called temp_mean
in the output file.
* The -o
argument indicates the location of the output file.
The format of the output file is inferred by GDAL using the file extension.
With reasonable real-world inputs, the processing time of exactextract
is roughly divided evenly between (a) I/O (reading raster cells, which may require decompression) and (b) computing the area of each raster cell that is covered by each polygon.
In common usage, we might want to perform many calculations in which one or both of these steps can be reused, such as:
The following more advanced usage shows how exactextract
might be called to perform multiple calculations at once, reusing work where possible:
exactextract \
-r "temp_2016:temperature_2016.tif" \
-r "temp_2017:temperature_2017.tif" \
-r "temp_2018:temperature_2018.tif" \
-p countries.shp \
-f country_name \
-s "min(temp_2016)" \
-s "mean(temp_2016)" \
-s "max(temp_2016)" \
-s "min(temp_2017)" \
-s "mean(temp_2017)" \
-s "max(temp_2017)" \
-s "min(temp_2017)" \
-s "mean(temp_2017)" \
-s "max(temp_2017)" \
-o temp_summary.csv
In this case, the output temp_summary.csv
file would contain the fields min_temp_2016
, mean_temp_2016
, etc. Each raster would be read only a single time, and each polygon/raster overlay would be performed a single time, because the three input rasters have the same extent and resolution.
Another more advanced usage of exactextract
involves calculations in which the values of one raster are weighted by the values of a second raster.
For example, we may wish to calculate both a standard and population-weighted mean temperature for each country:
exactextract \
-r "temp:temperature_2018.tif" \
-r "pop:world_population.tif" \
-p countries.shp \
-f country_name \
-s "mean(temp)" \
-s "pop_weighted_mean=weighted_mean(temp,pop)" \
-o mean_temperature.csv
This also demonstrates the ability to control the name of a stat's output column by prefixing the stat name with an output column name.
Further details on weighted statistics are provided in the section below.
The statistics supported by exactextract
are summarized in the table below.
A formula is provided for each statistic, in which
xi represents the value of the ith raster cell,
ci represents the fraction of the ith raster cell that is covered by the polygon, and
wi represents the weight of the ith raster cell.
Values in the "example result" column refer to the value and weighting rasters shown below.
In these images, values of the "value raster" range from 1 to 4, and values of the "weighting raster" range from 5 to 8.
The area covered by the polygon is shaded purple.
| Example Value Raster | Example Weighting Raster |
| -------------------- | ------------------------ |
| |
|
| Name | Formula | Description | Typical Application | Example Result | | -------------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------- | -------------------- |--------------- | | count | Σci | Sum of all cell coverage fractions. | | 0.5 + 0 + 1 + 0.25 = 1.75 | | sum | Σxici | Sum of values of raster cells that intersect the polygon, with each raster value weighted by its coverage fraction. | Total population | 0.5×1 + 0×2 + 1.0×3 + 0.25×4 = 4.5 | | mean | (Σxici)/(Σci) | Mean value of cells that intersect the polygon, weighted by the percent of the cell that is covered. | Average temperature | 4.5/1.75 = 2.57 | | weighted_sum | Σxiciwi | Sum of raster cells covered by the polygon, with each raster value weighted by its coverage fraction and weighting raster value. | Total crop production lost | 0.5×1×5 + 0×2×6 + 1.0×3×7 + 0.25×4×8 = 31.5 | weighted_mean | (Σxiciwi)/(Σciwi) | Mean value of cells that intersect the polygon, weighted by the product over the coverage fraction and the weighting raster. | Population-weighted average temperature | 31.5 / (0.5×5 + 0×6 + 1.0×7 + 0.25×8) = 2.74 | min | - | Minimum value of cells that intersect the polygon, not taking coverage fractions or weighting raster values into account. | Minimum elevation | 1 | | max | - | Maximum value of cells that intersect the polygon, not taking coverage fractions or weighting raster values into account. | Maximum temperature | 4 | | minority | - | The raster value occupying the least number of cells, taking into account cell coverage fractions but not weighting raster values. | Least common land cover type | - | | majority | - | The raster value occupying the greatest number of cells, taking into account cell coverage fractions but not weighting raster values. | Most common land cover type | - | | variety | - | The number of distinct raster values in cells wholly or partially covered by the polygon. | Number of land cover types | - | | variance | (Σci(xi - x̅)2)/(Σci) | Population variance of cell values that intersect the polygon, taking into account coverage fraction. | - | 1.10 | | stdev | √variance | Population standard deviation of cell values that intersect the polygon, taking into account coverage fraction. | - | 1.05 | | coefficient_of_variation | stdev / mean | Population coefficient of variation of cell values that intersect the polygon, taking into account coverage fraction. | - | 0.41 |
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.