r.univar - Calculates univariate statistics from the non-null cells of a raster map.
Statistics include number of cells counted, minimum and maximum cell values, range, arithmetic mean, population variance, standard deviation, coefficient of variation, and sum.
raster, statistics, univariate statistics, zonal statistics
r.univar [-getr] map=name[,name,...] [zones=name] [output=name] [percentile=float[,float,...]] [separator=character] [--overwrite] [--help] [--verbose] [--quiet] [--ui]
Print the stats in shell script style
Calculate extended statistics
Table output format instead of standard output format
Use the native resolution and extent of the raster map, instead of the current region
Allow output files to overwrite existing files
Print usage summary
Verbose module output
Quiet module output
Force launching GUI dialog
Name of raster map(s)
Raster map used for zoning, must be of type CELL
Name for output file (if omitted or "-" output to stdout)
Percentile to calculate (requires extended statistics flag)
Special characters: pipe, comma, space, tab, newline
r.univar calculates the univariate statistics of one or several raster map(s). This includes the number of cells counted, minimum and maximum cell values, range, arithmetic mean, population variance, standard deviation, coefficient of variation, and sum. Statistics are calculated separately for every category/zone found in the zones input map if given. If the -e extended statistics flag is given the 1st quartile, median, 3rd quartile, and given percentile are calculated. If the -g flag is given the results are presented in a format suitable for use in a shell script. If the -t flag is given the results are presented in tabular format with the given field separator. The table can immediately be converted to a vector attribute table which can then be linked to a vector, e.g. the vector that was rasterized to create the zones input raster.
When multiple input maps are given to r.univar, the overall statistics are calculated. This is useful for a time series of the same variable, as well as for the case of a segmented/tiled dataset. Allowing multiple raster maps to be specified saves the user from using a temporary raster map for the result of r.series or r.patch.
As with most GRASS raster modules, r.univar operates on the raster array defined by the current region settings, not the original extent and resolution of the input map. See g.region, but also the wiki page on the computational region to understand the impact of the region settings on the calculations.
This module can use large amounts of system memory when the -e extended statistics flag is used with a very large region setting. If the region is too large the module should exit gracefully with a memory allocation error. Basic statistics can be calculated using any size input region. Extended statistics can be calculated using r.stats.quantile.
Without a zones input raster, the r.quantile module will be significantly more efficient for calculating percentiles with large maps.
For calculating univariate statistics from a raster map based on vector polygon map and uploads statistics to new attribute columns, see v.rast.stats.
In this example, the raster map elevation in the North Carolina sample dataset is used to calculate univariate statistics:
g.region raster=elevation -p
# standard output, along with extended statistics
r.univar -e elevation percentile=98
total null and non-null cells: 2025000
total null cells: 0
Of the non-null cells:
mean of absolute values: 110.375
standard deviation: 20.3153
variation coefficient: 18.4057 %
1st quartile: 94.79
median (even number of cells): 108.88
3rd quartile: 126.792
98th percentile: 147.727
# script style output, along with extended statistics
r.univar -ge elevation percentile=98
In this example, the raster polygon map basins in the North Carolina sample dataset is used to calculate raster statistics for zones for elevation raster map:
g.region raster=basins -p
This will set and print computational region in the format:
projection: 99 (Lambert Conformal Conic)
ellipsoid: a=6378137 es=0.006694380022900787
Check basin's IDs using:
This will print them in the format:
Visualization of them underlying elevation map can be created as:
r.colors map=elevation color=grey
r.colors map=basins color=bgyr
d.legend raster=basins use=2,4,6,8,10,12,14,16,18,20,22,24,26,28,30
Figure: Zones (basins, opacity: 60%) with underlying elevation map for North Carolina sample dataset.
Then statistics for elevation can be calculated separately for every zone, i.e. basin found in the zones parameter:
r.univar -t map=elevation zones=basins separator=comma \
This will print information in the format:
Comma Separated Values (CSV) file is best viewed through a spreadsheet program such as Microsoft Excel, Libre/Open Office Calc or Google Docs:
Figure: Raster statistics for zones (basins, North Carolina sample dataset) viewed through Libre/Open Office Calc.
To be implemented mode, skewness, kurtosis.
g.region, r3.univar, r.mode, r.quantile, r.series, r.stats, r.stats.quantile, r.stats.zonal, r.statistics, v.rast.stats, v.univar
Hamish Bowman, Otago University, New Zealand
Extended statistics by Martin Landa
Multiple input map support by Ivan Shmakov
Zonal loop by Markus Metz
Last changed: $Date: 2016-12-11 19:03:35 +0100 (Sun, 11 Dec 2016) $
Available at: r.univar source code (history)
Main index | Raster index | Topics index | Keywords index | Graphical index | Full index
2003-2017 GRASS Development Team, GRASS GIS 7.2.1 Reference Manual