univar_statistics.py 7.8 KB

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