v.db.univar.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #% keyword: vector
  20. #% keyword: statistics
  21. #% keyword: 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 gscript
  54. from grass.exceptions import CalledModuleError
  55. def main():
  56. global tmp
  57. tmp = gscript.tempfile()
  58. vector = options['map']
  59. layer = options['layer']
  60. column = options['column']
  61. where = options['where']
  62. perc = options['percentile']
  63. if not gscript.find_file(vector, element='vector')['file']:
  64. gscript.fatal(_("Vector map <%s> not found") % vector)
  65. try:
  66. fi = gscript.vector_db(vector, stderr=nuldev)[int(layer)]
  67. except KeyError:
  68. gscript.fatal(_("No attribute table linked to layer <%s>") % layer)
  69. table = fi['table']
  70. database = fi['database']
  71. driver = fi['driver']
  72. passflags = None
  73. if flags['e']:
  74. passflags = 'e'
  75. if flags['g']:
  76. if not passflags:
  77. passflags = 'g'
  78. else:
  79. passflags = passflags + 'g'
  80. try:
  81. gscript.run_command('db.univar', table=table, column=column,
  82. database=database, driver=driver,
  83. perc=perc, where=where, flags=passflags)
  84. except CalledModuleError:
  85. sys.exit(1)
  86. if __name__ == "__main__":
  87. options, flags = gscript.parser()
  88. nuldev = open(os.devnull, 'w')
  89. main()