list_stds.py 10.0 KB

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