v.db.univar.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  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(
  82. "db.univar",
  83. table=table,
  84. column=column,
  85. database=database,
  86. driver=driver,
  87. perc=perc,
  88. where=where,
  89. flags=passflags,
  90. )
  91. except CalledModuleError:
  92. sys.exit(1)
  93. if __name__ == "__main__":
  94. options, flags = gscript.parser()
  95. nuldev = open(os.devnull, "w")
  96. main()