univar_statistics.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) 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. 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. sp = open_old_space_time_dataset(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 header == True:
  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. string += str(id) + fs + str(start) + fs + str(end)
  62. string += fs + str(stats["mean"]) + fs + str(stats["min"])
  63. string += fs + str(stats["max"]) + fs + str(stats["mean_of_abs"])
  64. string += fs + str(stats["stddev"]) + fs + str(stats["variance"])
  65. string += fs + str(stats["coeff_var"]) + fs + str(stats["sum"])
  66. string += fs + str(stats["null_cells"]) + fs + str(stats["cells"])
  67. if extended == True:
  68. string += fs + str(stats["first_quartile"]) + fs + str(stats["median"])
  69. string += fs + str(stats["third_quartile"]) + fs + str(stats["percentile_90"])
  70. print string
  71. dbif.close()
  72. ###############################################################################
  73. def print_vector_dataset_univar_statistics(input, twhere, layer, type, column,
  74. where, extended, header, fs):
  75. """!Print univariate statistics for a space time vector dataset
  76. @param input The name of the space time dataset
  77. @param twhere A temporal database where statement
  78. @param layer The layer number used in case no layer is present
  79. in the temporal dataset
  80. @param type options: point,line,boundary,centroid,area
  81. @param column The name of the attribute column
  82. @param where A temporal database where statement
  83. @param extended If True compute extended statistics
  84. @param header If True print column names as header
  85. @param fs Field separator
  86. """
  87. # We need a database interface
  88. dbif = SQLDatabaseInterfaceConnection()
  89. dbif.connect()
  90. mapset = get_current_mapset()
  91. if input.find("@") >= 0:
  92. id = input
  93. else:
  94. id = input + "@" + mapset
  95. sp = dataset_factory("stvds", id)
  96. if sp.is_in_db(dbif) == False:
  97. dbif.close()
  98. core.fatal(_("Space time %(sp)s dataset <%(i)s> not found") % {
  99. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  100. sp.select(dbif)
  101. rows = sp.get_registered_maps("id,name,mapset,start_time,end_time,layer",
  102. twhere, "start_time", dbif)
  103. if not rows:
  104. dbif.close()
  105. core.fatal(_("Space time %(sp)s dataset <%(i)s> is empty") % {
  106. 'sp': sp.get_new_map_instance(None).get_type(), 'i': id})
  107. string = ""
  108. if header == True:
  109. string += "id" + fs + "start" + fs + "end" + fs + "n" + \
  110. fs + "nmissing" + fs + "nnull" + fs
  111. string += "min" + fs + "max" + fs + "range"
  112. if type == "point" or type == "centroid":
  113. string += fs + "mean" + fs + "mean_abs" + fs + "population_stddev" +\
  114. fs + "population_variance" + fs
  115. string += "population_coeff_variation" + fs + \
  116. "sample_stddev" + fs + "sample_variance" + fs
  117. string += "kurtosis" + fs + "skewness"
  118. if extended == True:
  119. string += fs + "first_quartile" + fs + "median" + fs + \
  120. "third_quartile" + fs + "percentile_90"
  121. print string
  122. for row in rows:
  123. id = row["name"] + "@" + row["mapset"]
  124. start = row["start_time"]
  125. end = row["end_time"]
  126. mylayer = row["layer"]
  127. flags = "g"
  128. if extended == True:
  129. flags += "e"
  130. if not mylayer:
  131. mylayer = layer
  132. stats = core.parse_command("v.univar", map=id, where=where,
  133. column=column, layer=mylayer,
  134. type=type, flags=flags)
  135. string = ""
  136. if stats:
  137. string += str(id) + fs + str(start) + fs + str(end)
  138. string += fs + str(stats["n"]) + fs + str(stats[
  139. "nmissing"]) + fs + str(stats["nnull"])
  140. if "min" in stats:
  141. string += fs + str(stats["min"]) + fs + str(
  142. stats["max"]) + fs + str(stats["range"])
  143. else:
  144. string += fs + fs + fs
  145. if type == "point" or type == "centroid":
  146. if "mean" in stats:
  147. string += fs + str(stats["mean"]) + fs + \
  148. str(stats["mean_abs"]) + fs + \
  149. str(stats["population_stddev"]) + fs + \
  150. str(stats["population_variance"])
  151. string += fs + str(stats["population_coeff_variation"]) + \
  152. fs + str(stats["sample_stddev"]) + fs + \
  153. str(stats["sample_variance"])
  154. string += fs + str(stats["kurtosis"]) + fs + \
  155. str(stats["skewness"])
  156. else:
  157. string += fs + fs + fs + fs + fs + fs + fs + fs + fs
  158. if extended == True:
  159. if "first_quartile" in stats:
  160. string += fs + str(stats["first_quartile"]) + fs + \
  161. str(stats["median"]) + fs + \
  162. str(stats["third_quartile"]) + fs + \
  163. str(stats["percentile_90"])
  164. else:
  165. string += fs + fs + fs + fs
  166. print string
  167. dbif.close()