list_stds.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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):
  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. :return: A dictionary with the rows of the SQL query for each
  32. available mapset
  33. .. code-block:: python
  34. >>> import grass.temporal as tgis
  35. >>> tgis.init()
  36. >>> name = "list_stds_test"
  37. >>> sp = tgis.open_new_stds(name=name, type="strds",
  38. ... temporaltype="absolute", title="title", descr="descr", semantic="mean", dbif=None, overwrite=True)
  39. >>> mapset = tgis.get_current_mapset()
  40. >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name")
  41. >>> rows = stds_list[mapset]
  42. >>> for row in rows:
  43. ... if row["name"] == name:
  44. ... print True
  45. True
  46. >>> stds_list = tgis.get_dataset_list("strds", "absolute", columns="name,mapset", where="mapset = '%s'"%(mapset))
  47. >>> rows = stds_list[mapset]
  48. >>> for row in rows:
  49. ... if row["name"] == name and row["mapset"] == mapset:
  50. ... print True
  51. True
  52. >>> check = sp.delete()
  53. """
  54. id = None
  55. sp = dataset_factory(type, id)
  56. dbif = SQLDatabaseInterfaceConnection()
  57. dbif.connect()
  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. return result
  81. ###############################################################################
  82. def list_maps_of_stds(type, input, columns, order, where, separator,
  83. method, no_header=False, gran=None):
  84. """ List the maps of a space time dataset using diffetent methods
  85. :param type: The type of the maps raster, raster3d or vector
  86. :param input: Name of a space time raster dataset
  87. :param columns: A comma separated list of columns to be printed to stdout
  88. :param order: A comma separated list of columns to order the
  89. maps by category
  90. :param where: A where statement for selected listing without "WHERE"
  91. e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
  92. :param separator: The field separator character between the columns
  93. :param method: String identifier to select a method out of cols,
  94. comma,delta or deltagaps
  95. - "cols" Print preselected columns specified by columns
  96. - "comma" Print the map ids ("name@mapset") as comma separated string
  97. - "delta" Print the map ids ("name@mapset") with start time,
  98. end time, relative length of intervals and the relative
  99. distance to the begin
  100. - "deltagaps" Same as "delta" with additional listing of gaps.
  101. Gaps can be simply identified as the id is "None"
  102. - "gran" List map using the granularity of the space time dataset,
  103. columns are identical to deltagaps
  104. :param no_header: Supress the printing of column names
  105. :param gran: The user defined granule to be used if method=gran is
  106. set, in case gran=None the granule of the space time
  107. dataset is used
  108. """
  109. dbif, connected = init_dbif(None)
  110. msgr = get_tgis_message_interface()
  111. sp = open_old_stds(input, type, dbif)
  112. if separator is None or separator == "":
  113. separator = "\t"
  114. # This method expects a list of objects for gap detection
  115. if method == "delta" or method == "deltagaps" or method == "gran":
  116. if type == "stvds":
  117. columns = "id,name,layer,mapset,start_time,end_time"
  118. else:
  119. columns = "id,name,mapset,start_time,end_time"
  120. if method == "deltagaps":
  121. maps = sp.get_registered_maps_as_objects_with_gaps(where=where,
  122. dbif=dbif)
  123. elif method == "delta":
  124. maps = sp.get_registered_maps_as_objects(where=where,
  125. order="start_time",
  126. dbif=dbif)
  127. elif method == "gran":
  128. if gran is not None and gran != "":
  129. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran,
  130. dbif=dbif)
  131. else:
  132. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  133. if no_header is False:
  134. string = ""
  135. string += "%s%s" % ("id", separator)
  136. string += "%s%s" % ("name", separator)
  137. if type == "stvds":
  138. string += "%s%s" % ("layer", separator)
  139. string += "%s%s" % ("mapset", separator)
  140. string += "%s%s" % ("start_time", separator)
  141. string += "%s%s" % ("end_time", separator)
  142. string += "%s%s" % ("interval_length", separator)
  143. string += "%s" % ("distance_from_begin")
  144. print string
  145. if maps and len(maps) > 0:
  146. if isinstance(maps[0], list):
  147. if len(maps[0]) > 0:
  148. first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
  149. else:
  150. msgr.warning(_("Empty map list"))
  151. return
  152. else:
  153. first_time, dummy = maps[0].get_temporal_extent_as_tuple()
  154. for mymap in maps:
  155. if isinstance(mymap, list):
  156. if len(mymap) > 0:
  157. map = mymap[0]
  158. else:
  159. msgr.fatal(_("Empty entry in map list, this should not happen"))
  160. else:
  161. map = mymap
  162. start, end = map.get_temporal_extent_as_tuple()
  163. if end:
  164. delta = end - start
  165. else:
  166. delta = None
  167. delta_first = start - first_time
  168. if map.is_time_absolute():
  169. if end:
  170. delta = time_delta_to_relative_time(delta)
  171. delta_first = time_delta_to_relative_time(delta_first)
  172. string = ""
  173. string += "%s%s" % (map.get_id(), separator)
  174. string += "%s%s" % (map.get_name(), separator)
  175. if type == "stvds":
  176. string += "%s%s" % (map.get_layer(), separator)
  177. string += "%s%s" % (map.get_mapset(), separator)
  178. string += "%s%s" % (start, separator)
  179. string += "%s%s" % (end, separator)
  180. string += "%s%s" % (delta, separator)
  181. string += "%s" % (delta_first)
  182. print string
  183. else:
  184. # In comma separated mode only map ids are needed
  185. if method == "comma":
  186. columns = "id"
  187. rows = sp.get_registered_maps(columns, where, order, dbif)
  188. if rows:
  189. if method == "comma":
  190. string = ""
  191. count = 0
  192. for row in rows:
  193. if count == 0:
  194. string += row["id"]
  195. else:
  196. string += ",%s" % row["id"]
  197. count += 1
  198. print string
  199. elif method == "cols":
  200. # Print the column names if requested
  201. if no_header is False:
  202. output = ""
  203. count = 0
  204. collist = columns.split(",")
  205. for key in collist:
  206. if count > 0:
  207. output += separator + str(key)
  208. else:
  209. output += str(key)
  210. count += 1
  211. print output
  212. for row in rows:
  213. output = ""
  214. count = 0
  215. for col in row:
  216. if count > 0:
  217. output += separator + str(col)
  218. else:
  219. output += str(col)
  220. count += 1
  221. print output
  222. if connected:
  223. dbif.close()
  224. ###############################################################################
  225. if __name__ == "__main__":
  226. import doctest
  227. doctest.testmod()