univar_statistics.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.print_gridded_dataset_univar_statistics(
  8. type, input, where, extended, header, fs)
  9. ...
  10. @endcode
  11. (C) 2008-2011 by the GRASS Development Team
  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. @author Soeren Gebbert
  16. """
  17. from space_time_datasets import *
  18. from factory import *
  19. ###############################################################################
  20. def print_gridded_dataset_univar_statistics(type, input, where, extended, header, fs):
  21. """!Print univariate statistics for a space time raster or raster3d dataset
  22. @param type Must be "strds" or "str3ds"
  23. @param input The name of the space time dataset
  24. @param where A temporal database where statement
  25. @param extended If True compute extended statistics
  26. @param header If True print column names as header
  27. @param fs Field separator
  28. """
  29. # We need a database interface
  30. dbif = SQLDatabaseInterfaceConnection()
  31. dbif.connect()
  32. mapset = core.gisenv()["MAPSET"]
  33. if input.find("@") >= 0:
  34. id = input
  35. else:
  36. id = input + "@" + mapset
  37. sp = dataset_factory(type, id)
  38. if sp.is_in_db(dbif) == False:
  39. dbif.close()
  40. core.fatal(_("Space time %s dataset <%s> not found") % (
  41. sp.get_new_map_instance(None).get_type(), id))
  42. sp.select(dbif)
  43. rows = sp.get_registered_maps(
  44. "id,start_time,end_time", where, "start_time", dbif)
  45. if not rows:
  46. dbif.close()
  47. core.fatal(_("Space time %s dataset <%s> is empty") % (
  48. sp.get_new_map_instance(None).get_type(), id))
  49. if header == True:
  50. print "id" + fs + "start" + fs + "end" + fs + "mean" + fs + \
  51. "min" + fs + "max" + fs,
  52. print "mean_of_abs" + fs + "stddev" + fs + "variance" + fs,
  53. if extended == True:
  54. print "coeff_var" + fs + "sum" + fs + \
  55. "null_cells" + fs + "cells" + fs,
  56. print "first_quartile" + fs + "median" + fs + \
  57. "third_quartile" + fs + "percentile_90"
  58. else:
  59. print "coeff_var" + fs + "sum" + fs + "null_cells" + fs + "cells"
  60. for row in rows:
  61. id = row["id"]
  62. start = row["start_time"]
  63. end = row["end_time"]
  64. flag = "g"
  65. if extended == True:
  66. flag += "e"
  67. if type == "strds":
  68. stats = core.parse_command("r.univar", map=id, flags=flag)
  69. elif type == "str3ds":
  70. stats = core.parse_command("r3.univar", map=id, flags=flag)
  71. print str(id) + fs + str(start) + fs + str(end),
  72. print fs + str(stats["mean"]) + fs + str(stats["min"]) + \
  73. fs + str(stats["max"]) + fs + str(stats["mean_of_abs"]),
  74. print fs + str(stats["stddev"]) + fs + str(stats["variance"]) + \
  75. fs + str(stats["coeff_var"]) + fs + str(stats["sum"]),
  76. if extended == True:
  77. print fs + str(stats["null_cells"]) + fs + str(
  78. stats["cells"]) + fs,
  79. print str(stats["first_quartile"]) + fs + str(stats["median"]) + \
  80. fs + str(stats["third_quartile"]) + \
  81. fs + str(stats["percentile_90"])
  82. else:
  83. print fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  84. dbif.close()
  85. ###############################################################################
  86. def print_vector_dataset_univar_statistics(input, twhere, layer, type, column,
  87. where, extended, header, fs):
  88. """!Print univariate statistics for a space time vector dataset
  89. @param input The name of the space time dataset
  90. @param twhere A temporal database where statement
  91. @param layer The layer number used in case no layer is present
  92. in the temporal dataset
  93. @param type options: point,line,boundary,centroid,area
  94. @param column The name of the attribute column
  95. @param where A temporal database where statement
  96. @param extended If True compute extended statistics
  97. @param header If True print column names as header
  98. @param fs Field separator
  99. """
  100. # We need a database interface
  101. dbif = SQLDatabaseInterfaceConnection()
  102. dbif.connect()
  103. mapset = core.gisenv()["MAPSET"]
  104. if input.find("@") >= 0:
  105. id = input
  106. else:
  107. id = input + "@" + mapset
  108. sp = dataset_factory("stvds", id)
  109. if sp.is_in_db(dbif) == False:
  110. dbif.close()
  111. core.fatal(_("Space time %s dataset <%s> not found") % (
  112. sp.get_new_map_instance(None).get_type(), id))
  113. sp.select(dbif)
  114. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  115. twhere, "start_time", dbif)
  116. if not rows:
  117. dbif.close()
  118. core.fatal(_("Space time %s dataset <%s> is empty") % (
  119. sp.get_new_map_instance(None).get_type(), id))
  120. string = ""
  121. if header == True:
  122. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  123. fs + "nmissing" + fs + "nnull" + fs
  124. string += "min" + fs + "max" + fs + "range"
  125. if type == "point" or type == "centroid":
  126. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  127. fs + "population_variance" + fs
  128. string += "population_coeff_variation" + fs + \
  129. "sample_stddev" + fs + "sample_variance" + fs
  130. string += "kurtosis" + fs + "skewness"
  131. if extended == True:
  132. string += fs + "first_quartile" + fs + "median" + fs + \
  133. "third_quartile" + fs + "percentile_90"
  134. print string
  135. for row in rows:
  136. id = row["name"] + "@" + row["mapset"]
  137. start = row["start_time"]
  138. end = row["end_time"]
  139. mylayer = row["layer"]
  140. flags = "g"
  141. if extended == True:
  142. flags += "e"
  143. if not mylayer:
  144. mylayer = layer
  145. stats = core.parse_command("v.univar", map=id, where=where,
  146. column=column, layer=mylayer,
  147. type=type, flags=flags)
  148. string = ""
  149. if stats:
  150. string += str(id) + fs + str(start) + fs + str(end)
  151. string += fs + str(stats["n"]) + fs + str(stats[
  152. "nmissing"]) + fs + str(stats["nnull"])
  153. if "min" in stats:
  154. string += fs + str(stats["min"]) + fs + str(
  155. stats["max"]) + fs + str(stats["range"])
  156. else:
  157. string += fs + fs + fs
  158. if type == "point" or type == "centroid":
  159. if "mean" in stats:
  160. string += fs + str(stats["mean"]) + fs + \
  161. str(stats["mean_abs"]) + fs + \
  162. str(stats["population_stddev"]) + fs + \
  163. str(stats["population_variance"])
  164. string += fs + str(stats["population_coeff_variation"]) + \
  165. fs + str(stats["sample_stddev"]) + fs + \
  166. str(stats["sample_variance"])
  167. string += fs + str(stats["kurtosis"]) + fs + \
  168. str(stats["skewness"])
  169. else:
  170. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  171. if extended == True:
  172. if "first_quartile" in stats:
  173. string += fs + str(stats["first_quartile"]) + fs + \
  174. str(stats["median"]) + fs + \
  175. str(stats["third_quartile"]) + fs + \
  176. str(stats["percentile_90"])
  177. else:
  178. string += fs + fs + fs + fs
  179. print string
  180. dbif.close()