univar_statistics.py 7.9 KB

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