v.db.univar.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.db.univar (formerly called v.univar.sh)
  5. # AUTHOR(S): Michael Barton, Arizona State University
  6. # Converted to Python by Glynn Clements
  7. # Sync'ed to r.univar by Markus Metz
  8. # PURPOSE: Calculates univariate statistics from a GRASS vector map attribute column.
  9. # Based on r.univar.sh by Markus Neteler
  10. # COPYRIGHT: (C) 2005, 2007, 2008 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Calculates univariate statistics on selected table column for a GRASS vector map.
  19. #% keywords: vector
  20. #% keywords: statistics
  21. #% keywords: attribute table
  22. #%end
  23. #%option G_OPT_V_MAP
  24. #% required: yes
  25. #%end
  26. #%option G_OPT_V_FIELD
  27. #%end
  28. #%option G_OPT_DB_COLUMN
  29. #% description: Name of attribute column on which to calculate statistics (must be numeric)
  30. #% required: yes
  31. #%end
  32. #%option G_OPT_DB_WHERE
  33. #%end
  34. #%option
  35. #% key: percentile
  36. #% type: double
  37. #% description: Percentile to calculate (requires extended statistics flag)
  38. #% required : no
  39. #% answer: 90
  40. #% options: 0-100
  41. #% multiple: yes
  42. #%end
  43. #%flag
  44. #% key: e
  45. #% description: Extended statistics (quartiles and 90th percentile)
  46. #%end
  47. #%flag
  48. #% key: g
  49. #% description: Print stats in shell script style
  50. #%end
  51. import sys
  52. import os
  53. import grass.script as grass
  54. def main():
  55. global tmp
  56. tmp = grass.tempfile()
  57. vector = options['map']
  58. layer = options['layer']
  59. column = options['column']
  60. where = options['where']
  61. perc = options['percentile']
  62. extend = flags['e']
  63. shellstyle = flags['g']
  64. fi = grass.vector_db(vector, stderr = nuldev)[int(layer)]
  65. table = fi['table']
  66. database = fi['database']
  67. driver = fi['driver']
  68. passflags = None
  69. if flags['e']:
  70. passflags = 'e'
  71. if flags['g']:
  72. if not passflags:
  73. passflags = 'g'
  74. else:
  75. passflags = passflags + 'g'
  76. grass.run_command('db.univar', table = table, column = column,
  77. database = database, driver = driver,
  78. perc = perc, where = where, flags = passflags)
  79. if __name__ == "__main__":
  80. options, flags = grass.parser()
  81. nuldev = file(os.devnull, 'w')
  82. main()