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):
  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",
  39. ... 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",
  48. ... 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 = SQLDatabaseInterfaceConnection()
  59. dbif.connect()
  60. mapsets = get_available_temporal_mapsets()
  61. result = {}
  62. for mapset in mapsets.keys():
  63. if temporal_type == "absolute":
  64. table = sp.get_type() + "_view_abs_time"
  65. else:
  66. table = sp.get_type() + "_view_rel_time"
  67. if columns and columns.find("all") == -1:
  68. sql = "SELECT " + str(columns) + " FROM " + table
  69. else:
  70. sql = "SELECT * FROM " + table
  71. if where:
  72. sql += " WHERE " + where
  73. sql += " AND mapset = '%s'" % (mapset)
  74. else:
  75. sql += " WHERE mapset = '%s'" % (mapset)
  76. if order:
  77. sql += " ORDER BY " + order
  78. dbif.execute(sql, mapset=mapset)
  79. rows = dbif.fetchall(mapset=mapset)
  80. if rows:
  81. result[mapset] = rows
  82. return result
  83. ###############################################################################
  84. def list_maps_of_stds(type, input, columns, order, where, separator,
  85. method, no_header=False, gran=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. - "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 easily 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. :param outpath: The path to file where to save output
  112. """
  113. dbif, connected = init_dbif(None)
  114. msgr = get_tgis_message_interface()
  115. sp = open_old_stds(input, type, dbif)
  116. if separator is None or separator == "":
  117. separator = "\t"
  118. if outpath:
  119. outfile = open(outpath, 'w')
  120. # This method expects a list of objects for gap detection
  121. if method == "delta" or method == "deltagaps" or method == "gran":
  122. if type == "stvds":
  123. columns = "id,name,layer,mapset,start_time,end_time"
  124. else:
  125. columns = "id,name,mapset,start_time,end_time"
  126. if method == "deltagaps":
  127. maps = sp.get_registered_maps_as_objects_with_gaps(where=where,
  128. dbif=dbif)
  129. elif method == "delta":
  130. maps = sp.get_registered_maps_as_objects(where=where,
  131. order="start_time",
  132. dbif=dbif)
  133. elif method == "gran":
  134. if gran is not None and gran != "":
  135. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran,
  136. dbif=dbif)
  137. else:
  138. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  139. if no_header is False:
  140. string = ""
  141. string += "%s%s" % ("id", separator)
  142. string += "%s%s" % ("name", separator)
  143. if type == "stvds":
  144. string += "%s%s" % ("layer", separator)
  145. string += "%s%s" % ("mapset", separator)
  146. string += "%s%s" % ("start_time", separator)
  147. string += "%s%s" % ("end_time", separator)
  148. string += "%s%s" % ("interval_length", separator)
  149. string += "%s" % ("distance_from_begin")
  150. if outpath:
  151. outfile.write('{st}\n'.format(st=string))
  152. else:
  153. print string
  154. if maps and len(maps) > 0:
  155. if isinstance(maps[0], list):
  156. if len(maps[0]) > 0:
  157. first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
  158. else:
  159. msgr.warning(_("Empty map list"))
  160. return
  161. else:
  162. first_time, dummy = maps[0].get_temporal_extent_as_tuple()
  163. for mymap in maps:
  164. if isinstance(mymap, list):
  165. if len(mymap) > 0:
  166. map = mymap[0]
  167. else:
  168. msgr.fatal(_("Empty entry in map list, this should not happen"))
  169. else:
  170. map = mymap
  171. start, end = map.get_temporal_extent_as_tuple()
  172. if end:
  173. delta = end - start
  174. else:
  175. delta = None
  176. delta_first = start - first_time
  177. if map.is_time_absolute():
  178. if end:
  179. delta = time_delta_to_relative_time(delta)
  180. delta_first = time_delta_to_relative_time(delta_first)
  181. string = ""
  182. string += "%s%s" % (map.get_id(), separator)
  183. string += "%s%s" % (map.get_name(), separator)
  184. if type == "stvds":
  185. string += "%s%s" % (map.get_layer(), separator)
  186. string += "%s%s" % (map.get_mapset(), separator)
  187. string += "%s%s" % (start, separator)
  188. string += "%s%s" % (end, separator)
  189. string += "%s%s" % (delta, separator)
  190. string += "%s" % (delta_first)
  191. if outpath:
  192. outfile.write('{st}\n'.format(st=string))
  193. else:
  194. print string
  195. else:
  196. # In comma separated mode only map ids are needed
  197. if method == "comma":
  198. columns = "id"
  199. rows = sp.get_registered_maps(columns, where, order, dbif)
  200. if rows:
  201. if method == "comma":
  202. string = ""
  203. count = 0
  204. for row in rows:
  205. if count == 0:
  206. string += row["id"]
  207. else:
  208. string += ",%s" % row["id"]
  209. count += 1
  210. if outpath:
  211. outfile.write('{st}\n'.format(st=string))
  212. else:
  213. print string
  214. elif method == "cols":
  215. # Print the column names if requested
  216. if no_header is False:
  217. output = ""
  218. count = 0
  219. collist = columns.split(",")
  220. for key in collist:
  221. if count > 0:
  222. output += separator + str(key)
  223. else:
  224. output += str(key)
  225. count += 1
  226. if outpath:
  227. outfile.write('{st}\n'.format(st=output))
  228. else:
  229. print output
  230. for row in rows:
  231. output = ""
  232. count = 0
  233. for col in row:
  234. if count > 0:
  235. output += separator + str(col)
  236. else:
  237. output += str(col)
  238. count += 1
  239. if outpath:
  240. outfile.write('{st}\n'.format(st=output))
  241. else:
  242. print output
  243. if outpath:
  244. outfile.close()
  245. if connected:
  246. dbif.close()
  247. ###############################################################################
  248. if __name__ == "__main__":
  249. import doctest
  250. doctest.testmod()