univar_statistics.py 7.9 KB

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