list_stds.py 10 KB

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