list_stds.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 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. outpath=None):
  87. """ List the maps of a space time dataset using different methods
  88. :param type: The type of the maps raster, raster3d or vector
  89. :param input: Name of a space time raster dataset
  90. :param columns: A comma separated list of columns to be printed to stdout
  91. :param order: A comma separated list of columns to order the
  92. maps by category
  93. :param where: A where statement for selected listing without "WHERE"
  94. e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
  95. :param separator: The field separator character between the columns
  96. :param method: String identifier to select a method out of cols,
  97. comma,delta or deltagaps
  98. :param dbif: The database interface to be used
  99. - "cols" Print preselected columns specified by columns
  100. - "comma" Print the map ids ("name@mapset") as comma separated string
  101. - "delta" Print the map ids ("name@mapset") with start time,
  102. end time, relative length of intervals and the relative
  103. distance to the begin
  104. - "deltagaps" Same as "delta" with additional listing of gaps.
  105. Gaps can be easily identified as the id is "None"
  106. - "gran" List map using the granularity of the space time dataset,
  107. columns are identical to deltagaps
  108. :param no_header: Supress the printing of column names
  109. :param gran: The user defined granule to be used if method=gran is
  110. set, in case gran=None the granule of the space time
  111. dataset is used
  112. :param outpath: The path to file where to save output
  113. """
  114. dbif, connected = init_dbif(dbif)
  115. msgr = get_tgis_message_interface()
  116. sp = open_old_stds(input, type, dbif)
  117. if separator is None or separator == "":
  118. separator = "\t"
  119. if outpath:
  120. outfile = open(outpath, 'w')
  121. # This method expects a list of objects for gap detection
  122. if method == "delta" or method == "deltagaps" or method == "gran":
  123. if type == "stvds":
  124. columns = "id,name,layer,mapset,start_time,end_time"
  125. else:
  126. columns = "id,name,mapset,start_time,end_time"
  127. if method == "deltagaps":
  128. maps = sp.get_registered_maps_as_objects_with_gaps(where=where,
  129. dbif=dbif)
  130. elif method == "delta":
  131. maps = sp.get_registered_maps_as_objects(where=where,
  132. order="start_time",
  133. dbif=dbif)
  134. elif method == "gran":
  135. if gran is not None and gran != "":
  136. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran,
  137. dbif=dbif)
  138. else:
  139. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  140. if no_header is False:
  141. string = ""
  142. string += "%s%s" % ("id", separator)
  143. string += "%s%s" % ("name", separator)
  144. if type == "stvds":
  145. string += "%s%s" % ("layer", separator)
  146. string += "%s%s" % ("mapset", separator)
  147. string += "%s%s" % ("start_time", separator)
  148. string += "%s%s" % ("end_time", separator)
  149. string += "%s%s" % ("interval_length", separator)
  150. string += "%s" % ("distance_from_begin")
  151. if outpath:
  152. outfile.write('{st}\n'.format(st=string))
  153. else:
  154. print string
  155. if maps and len(maps) > 0:
  156. if isinstance(maps[0], list):
  157. if len(maps[0]) > 0:
  158. first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
  159. else:
  160. msgr.warning(_("Empty map list"))
  161. return
  162. else:
  163. first_time, dummy = maps[0].get_temporal_extent_as_tuple()
  164. for mymap in maps:
  165. if isinstance(mymap, list):
  166. if len(mymap) > 0:
  167. map = mymap[0]
  168. else:
  169. msgr.fatal(_("Empty entry in map list, this should not happen"))
  170. else:
  171. map = mymap
  172. start, end = map.get_temporal_extent_as_tuple()
  173. if end:
  174. delta = end - start
  175. else:
  176. delta = None
  177. delta_first = start - first_time
  178. if map.is_time_absolute():
  179. if end:
  180. delta = time_delta_to_relative_time(delta)
  181. delta_first = time_delta_to_relative_time(delta_first)
  182. string = ""
  183. string += "%s%s" % (map.get_id(), separator)
  184. string += "%s%s" % (map.get_name(), separator)
  185. if type == "stvds":
  186. string += "%s%s" % (map.get_layer(), separator)
  187. string += "%s%s" % (map.get_mapset(), separator)
  188. string += "%s%s" % (start, separator)
  189. string += "%s%s" % (end, separator)
  190. string += "%s%s" % (delta, separator)
  191. string += "%s" % (delta_first)
  192. if outpath:
  193. outfile.write('{st}\n'.format(st=string))
  194. else:
  195. print string
  196. else:
  197. # In comma separated mode only map ids are needed
  198. if method == "comma":
  199. columns = "id"
  200. rows = sp.get_registered_maps(columns, where, order, dbif)
  201. if rows:
  202. if method == "comma":
  203. string = ""
  204. count = 0
  205. for row in rows:
  206. if count == 0:
  207. string += row["id"]
  208. else:
  209. string += ",%s" % row["id"]
  210. count += 1
  211. if outpath:
  212. outfile.write('{st}\n'.format(st=string))
  213. else:
  214. print string
  215. elif method == "cols":
  216. # Print the column names if requested
  217. if no_header is False:
  218. output = ""
  219. count = 0
  220. collist = columns.split(",")
  221. for key in collist:
  222. if count > 0:
  223. output += separator + str(key)
  224. else:
  225. output += str(key)
  226. count += 1
  227. if outpath:
  228. outfile.write('{st}\n'.format(st=output))
  229. else:
  230. print output
  231. for row in rows:
  232. output = ""
  233. count = 0
  234. for col in row:
  235. if count > 0:
  236. output += separator + str(col)
  237. else:
  238. output += str(col)
  239. count += 1
  240. if outpath:
  241. outfile.write('{st}\n'.format(st=output))
  242. else:
  243. print output
  244. if connected:
  245. dbif.close()
  246. ###############################################################################
  247. if __name__ == "__main__":
  248. import doctest
  249. doctest.testmod()