r.univar.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <h2>DESCRIPTION</h2>
  2. <em>r.univar</em> calculates the univariate statistics of one or several raster
  3. map(s). This includes the number of cells counted, minimum and maximum cell
  4. values, range, arithmetic mean, population variance, standard deviation, and
  5. coefficient of variation. Statistics are calculated separately for every
  6. category/zone found in the <b>zones</b> input map if given.
  7. If the <b>-e</b> extended statistics flag is given the 1st quartile, median,
  8. 3rd quartile, and given <b>percentile</b> are calculated.
  9. If the <b>-g</b> flag is given the results are presented in a format suitable
  10. for use in a shell script.
  11. If the <b>-t</b> flag is given the results are presented in tabular format
  12. with the given field separator. The table can immediately be converted to a
  13. vector attribute table which can then be linked to a vector, e.g. the vector
  14. that was rasterized to create the <b>zones</b> input raster.
  15. <p>
  16. When multiple input maps are given to <em>r.univar</em>, the overall statistics
  17. are calculated. This is useful for a time series of the same variable, as well as
  18. for the case of a segmented/tiled dataset. Allowing multiple raster maps to be
  19. specified saves the user from using a temporary raster map for the result of
  20. <em>r.series</em> or <em>r.patch</em>.
  21. <h2>NOTES</h2>
  22. As with most GRASS raster modules, <em>r.univar</em> operates on the raster
  23. array defined by the current region settings, not the original extent and
  24. resolution of the input map. See <em><a href="g.region.html">g.region</a></em>.
  25. <p>
  26. This module can use large amounts of system memory when the <b>-e</b>
  27. extended statistics flag is used with a very large region setting. If the
  28. region is too large the module should exit gracefully with a memory allocation
  29. error. Basic statistics can be calculated using any size input region.
  30. <p>
  31. Without a <b>zones</b> input raster, the <em>r.quantile</em> module will
  32. be significantly more efficient for calculating percentiles with large maps.
  33. <h2>EXAMPLE</h2>
  34. Calculate the raster statistics for zones within a vector map coverage
  35. and upload the results for mean, min and max back to the vector map:
  36. <div class="code"><pre>
  37. #### set the raster region to match the map
  38. g.region vect=fields res=10 -ap
  39. #### create rasterized version of vector map
  40. v.to.rast in=fields out=fields.10m use=cat type=area labelcolumn=label
  41. r.colors fields.10m color=random
  42. #### perform analysis
  43. r.univar -t map=elevation.10m zones=fields.10m | \
  44. cut -f1,5,6,8 -d'|' > fields_stats.txt
  45. #### populate vector DB with stats
  46. # create working copy of vector map
  47. g.copy vect=fields,fields_stats
  48. # create new attribute columns to hold output
  49. v.db.addcol map=fields_stats \
  50. columns='mean_elev DOUBLE PRECISION, min_elev DOUBLE PRECISION, max_elev DOUBLE PRECISION'
  51. # create SQL command file, and execute it
  52. sed -e '1d' fields_stats.txt | awk -F'|' \
  53. '{print "UPDATE fields_stats SET min_elev = "$2", max_elev = "$3", \
  54. mean_elev = "$4" WHERE cat = "$1";"}' \
  55. > fields_stats_sqlcmd.txt
  56. db.execute input=fields_stats_sqlcmd.txt
  57. <!--
  58. ###### alternate method with db.in.ogr: (needs work) ######
  59. #### convert text file table to a database table
  60. # not safe for commas in the label
  61. tr '|' ',' &lt; fields_stats.txt > fields_stats.csv
  62. echo '"Integer","String","Real","Real","Real"' > fields_stats.csvt
  63. # import table
  64. db.in.ogr dsn=fields_stats.csv output=fields_data
  65. # view table
  66. db.select fields_data
  67. # remove temporary files
  68. rm fields_stats.csv fields_stats.csvt fields_stats.txt
  69. #### populate vector DB with stats
  70. # create working copy of vector map
  71. g.copy vect=fields,fields_stats
  72. # create new attribute columns to hold output
  73. v.db.addcol map=fields_stats \
  74. columns='mean_elev DOUBLE PRECISION, min_elev DOUBLE PRECISION, max_elev DOUBLE PRECISION'
  75. # perform DB step (broken)
  76. ## how to automatically collate by key column, ie copy between tables?
  77. ## SELECT INTO? JOIN?
  78. echo "INSERT INTO fields_stats (mean_elev,min_elev,max_elev) SELECT mean,min,max FROM fields_data" | db.execute
  79. -->
  80. #### view completed table
  81. v.db.select fields_stats
  82. </pre></div>
  83. <h2>TODO</h2>
  84. <i>mode, skewness, kurtosis</i>
  85. <h2>SEE ALSO</h2>
  86. <em>
  87. <a href="g.region.html">g.region</a>,
  88. <a href="r3.univar.html">r3.univar</a>,
  89. <a href="r.mode.html">r.mode</a>,
  90. <a href="r.quantile.html">r.quantile</a>,
  91. <a href="r.series.html">r.series</a>,
  92. <a href="r.stats.html">r.stats</a>,
  93. <a href="r.statistics.html">r.statistics</a>,
  94. <a href="v.rast.stats.html">v.rast.stats</a>,
  95. <a href="v.univar.html">v.univar</a>
  96. </em>
  97. <h2>AUTHORS</h2>
  98. Hamish Bowman, Otago University, New Zealand<br>
  99. Extended statistics by Martin Landa<br>
  100. Multiple input map support by Ivan Shmakov<br>
  101. Zonal loop by Markus Metz
  102. <p><i>Last changed: $Date$</i>