list_stds.py 11 KB

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