list_stds.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.register_maps_in_space_time_dataset(type, name, maps)
  8. ...
  9. @endcode
  10. (C) 2012-2013 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets import *
  17. from factory import *
  18. from open_stds import *
  19. ###############################################################################
  20. def get_dataset_list(type, temporal_type, columns=None, where=None, order=None):
  21. """! Return a list of time stamped maps or space time datasets of a specific temporal type
  22. that are registred in the temporal database
  23. This method returns a dictionary, the keys are the available mapsets,
  24. the values are the rows from the SQL database query.
  25. @param type The type of the datasets (strds, str3ds, stvds, rast, rast3d, vect)
  26. @param temporal_type The temporal type of the datasets (absolute, relative)
  27. @param columns A comma separated list of columns that will be selected
  28. @param where A where statement for selected listing without "WHERE"
  29. @param order A comma separated list of columns to order the
  30. datasets by category
  31. @return A dictionary with the rows of the SQL query for each available mapset
  32. >>> import grass.temporal as tgis
  33. >>> tgis.init()
  34. >>> name = "list_stds_test"
  35. >>> sp = tgis.open_new_space_time_dataset(name=name, type="strds",
  36. ... temporaltype="absolute", title="title", descr="descr", semantic="mean", dbif=None, overwrite=True)
  37. >>> mapset = tgis.get_current_mapset()
  38. >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name")
  39. >>> rows = stds_list[mapset]
  40. >>> for row in rows:
  41. ... if row["name"] == name:
  42. ... print True
  43. True
  44. >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name,mapset", where="mapset = '%s'"%(mapset))
  45. >>> rows = stds_list[mapset]
  46. >>> for row in rows:
  47. ... if row["name"] == name and row["mapset"] == mapset:
  48. ... print True
  49. True
  50. >>> check = sp.delete()
  51. """
  52. id = None
  53. sp = dataset_factory(type, id)
  54. dbif = SQLDatabaseInterfaceConnection()
  55. dbif.connect()
  56. mapsets = get_tgis_c_library_interface().available_mapsets()
  57. result = {}
  58. for mapset in mapsets:
  59. if temporal_type == "absolute":
  60. table = sp.get_type() + "_view_abs_time"
  61. else:
  62. table = sp.get_type() + "_view_rel_time"
  63. if columns and columns.find("all") == -1:
  64. sql = "SELECT " + str(columns) + " FROM " + table
  65. else:
  66. sql = "SELECT * FROM " + table
  67. if where:
  68. sql += " WHERE " + where
  69. sql += " AND mapset = '%s'"%(mapset)
  70. else:
  71. sql += " WHERE mapset = '%s'"%(mapset)
  72. if order:
  73. sql += " ORDER BY " + order
  74. dbif.cursor.execute(sql)
  75. rows = dbif.cursor.fetchall()
  76. if rows:
  77. result[mapset] = rows
  78. return result
  79. ###############################################################################
  80. def list_maps_of_stds(type, input, columns, order, where, separator, method, header, gran=None):
  81. """! List the maps of a space time dataset using diffetent methods
  82. @param type The type of the maps raster, raster3d or vector
  83. @param input Name of a space time raster dataset
  84. @param columns A comma separated list of columns to be printed to stdout
  85. @param order A comma separated list of columns to order the
  86. maps by category
  87. @param where A where statement for selected listing without "WHERE"
  88. e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
  89. @param separator The field separator character between the columns
  90. @param method String identifier to select a method out of cols,
  91. comma,delta or deltagaps
  92. - "cols" Print preselected columns specified by columns
  93. - "comma" Print the map ids ("name@mapset") as comma separated string
  94. - "delta" Print the map ids ("name@mapset") with start time,
  95. end time, relative length of intervals and the relative
  96. distance to the begin
  97. - "deltagaps" Same as "delta" with additional listing of gaps.
  98. Gaps can be simply identified as the id is "None"
  99. - "gran" List map using the granularity of the space time dataset,
  100. columns are identical to deltagaps
  101. @param header Set True to print column names
  102. @param gran The user defined granule to be used if method=gran is set, in case gran=None the
  103. granule of the space time dataset is used
  104. """
  105. dbif, connected = init_dbif(None)
  106. msgr = get_tgis_message_interface()
  107. sp = open_old_space_time_dataset(input, type, dbif)
  108. if separator is None or separator == "":
  109. separator = "\t"
  110. # This method expects a list of objects for gap detection
  111. if method == "delta" or method == "deltagaps" or method == "gran":
  112. if type == "stvds":
  113. columns = "id,name,layer,mapset,start_time,end_time"
  114. else:
  115. columns = "id,name,mapset,start_time,end_time"
  116. if method == "deltagaps":
  117. maps = sp.get_registered_maps_as_objects_with_gaps(where=where, dbif=dbif)
  118. elif method == "delta":
  119. maps = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
  120. elif method == "gran":
  121. if gran is not None and gran != "":
  122. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran, dbif=dbif)
  123. else:
  124. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  125. if header:
  126. string = ""
  127. string += "%s%s" % ("id", separator)
  128. string += "%s%s" % ("name", separator)
  129. if type == "stvds":
  130. string += "%s%s" % ("layer", separator)
  131. string += "%s%s" % ("mapset", separator)
  132. string += "%s%s" % ("start_time", separator)
  133. string += "%s%s" % ("end_time", separator)
  134. string += "%s%s" % ("interval_length", separator)
  135. string += "%s" % ("distance_from_begin")
  136. print string
  137. if maps and len(maps) > 0:
  138. if isinstance(maps[0], list):
  139. if len(maps[0]) > 0:
  140. first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
  141. else:
  142. msgr.warning(_("Empty map list."))
  143. return
  144. else:
  145. first_time, dummy = maps[0].get_temporal_extent_as_tuple()
  146. for mymap in maps:
  147. if isinstance(mymap, list):
  148. if len(mymap) > 0:
  149. map = mymap[0]
  150. else:
  151. msgr.fatal(_("Empty entry in map list, this should not happen."))
  152. else:
  153. map = mymap
  154. start, end = map.get_temporal_extent_as_tuple()
  155. if end:
  156. delta = end - start
  157. else:
  158. delta = None
  159. delta_first = start - first_time
  160. if map.is_time_absolute():
  161. if end:
  162. delta = time_delta_to_relative_time(delta)
  163. delta_first = time_delta_to_relative_time(delta_first)
  164. string = ""
  165. string += "%s%s" % (map.get_id(), separator)
  166. string += "%s%s" % (map.get_name(), separator)
  167. if type == "stvds":
  168. string += "%s%s" % (map.get_layer(), separator)
  169. string += "%s%s" % (map.get_mapset(), separator)
  170. string += "%s%s" % (start, separator)
  171. string += "%s%s" % (end, separator)
  172. string += "%s%s" % (delta, separator)
  173. string += "%s" % (delta_first)
  174. print string
  175. else:
  176. # In comma separated mode only map ids are needed
  177. if method == "comma":
  178. columns = "id"
  179. rows = sp.get_registered_maps(columns, where, order, dbif)
  180. if rows:
  181. if method == "comma":
  182. string = ""
  183. count = 0
  184. for row in rows:
  185. if count == 0:
  186. string += row["id"]
  187. else:
  188. string += ",%s" % row["id"]
  189. count += 1
  190. print string
  191. elif method == "cols":
  192. # Print the column names if requested
  193. if header:
  194. output = ""
  195. count = 0
  196. collist = columns.split(",")
  197. for key in collist:
  198. if count > 0:
  199. output += separator + str(key)
  200. else:
  201. output += str(key)
  202. count += 1
  203. print output
  204. for row in rows:
  205. output = ""
  206. count = 0
  207. for col in row:
  208. if count > 0:
  209. output += separator + str(col)
  210. else:
  211. output += str(col)
  212. count += 1
  213. print output
  214. if connected:
  215. dbif.close()
  216. ###############################################################################
  217. if __name__ == "__main__":
  218. import doctest
  219. doctest.testmod()