list.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.register_maps_in_space_time_dataset(type, name, maps)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets import *
  17. ###############################################################################
  18. def list_maps_of_stds(type, input, columns, order, where, separator, method, header, gran=None):
  19. """! List the maps of a space time dataset using diffetent methods
  20. @param type The type of the maps raster, raster3d or vector
  21. @param input Name of a space time raster dataset
  22. @param columns A comma separated list of columns to be printed to stdout
  23. @param order A comma separated list of columns to order the
  24. space time dataset by category
  25. @param where A where statement for selected listing without "WHERE"
  26. e.g: start_time < "2001-01-01" and end_time > "2001-01-01"
  27. @param separator The field separator character between the columns
  28. @param method String identifier to select a method out of cols,
  29. comma,delta or deltagaps
  30. - "cols" Print preselected columns specified by columns
  31. - "comma" Print the map ids ("name@mapset") as comma separated string
  32. - "delta" Print the map ids ("name@mapset") with start time,
  33. end time, relative length of intervals and the relative
  34. distance to the begin
  35. - "deltagaps" Same as "delta" with additional listing of gaps.
  36. Gaps can be simply identified as the id is "None"
  37. - "gran" List map using the granularity of the space time dataset,
  38. columns are identical to deltagaps
  39. @param header Set True to print column names
  40. @param gran The user defined granule to be used if method=gran is set, in case gran=None the
  41. granule of the space time dataset is used
  42. """
  43. mapset = core.gisenv()["MAPSET"]
  44. if input.find("@") >= 0:
  45. id = input
  46. else:
  47. id = input + "@" + mapset
  48. dbif, connected = init_dbif(None)
  49. sp = dataset_factory(type, id)
  50. if not sp.is_in_db(dbif=dbif):
  51. core.fatal(_("Dataset <%s> not found in temporal database") % (id))
  52. sp.select(dbif=dbif)
  53. if separator is None or separator == "":
  54. separator = "\t"
  55. # This method expects a list of objects for gap detection
  56. if method == "delta" or method == "deltagaps" or method == "gran":
  57. if type == "stvds":
  58. columns = "id,name,layer,mapset,start_time,end_time"
  59. else:
  60. columns = "id,name,mapset,start_time,end_time"
  61. if method == "deltagaps":
  62. maps = sp.get_registered_maps_as_objects_with_gaps(where=where, dbif=dbif)
  63. elif method == "delta":
  64. maps = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
  65. elif method == "gran":
  66. if gran is not None and gran != "":
  67. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran, dbif=dbif)
  68. else:
  69. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  70. if header:
  71. string = ""
  72. string += "%s%s" % ("id", separator)
  73. string += "%s%s" % ("name", separator)
  74. if type == "stvds":
  75. string += "%s%s" % ("layer", separator)
  76. string += "%s%s" % ("mapset", separator)
  77. string += "%s%s" % ("start_time", separator)
  78. string += "%s%s" % ("end_time", separator)
  79. string += "%s%s" % ("interval_length", separator)
  80. string += "%s" % ("distance_from_begin")
  81. print string
  82. if maps and len(maps) > 0:
  83. if isinstance(maps[0], list):
  84. if len(maps[0]) > 0:
  85. first_time, dummy = maps[0][0].get_valid_time()
  86. else:
  87. core.warning(_("Empty map list."))
  88. return
  89. else:
  90. first_time, dummy = maps[0].get_valid_time()
  91. for mymap in maps:
  92. if isinstance(mymap, list):
  93. if len(mymap) > 0:
  94. map = mymap[0]
  95. else:
  96. core.fatal(_("Empty entry in map list, this should not happen."))
  97. else:
  98. map = mymap
  99. start, end = map.get_valid_time()
  100. if end:
  101. delta = end - start
  102. else:
  103. delta = None
  104. delta_first = start - first_time
  105. if map.is_time_absolute():
  106. if end:
  107. delta = time_delta_to_relative_time(delta)
  108. delta_first = time_delta_to_relative_time(delta_first)
  109. string = ""
  110. string += "%s%s" % (map.get_id(), separator)
  111. string += "%s%s" % (map.get_name(), separator)
  112. if type == "stvds":
  113. string += "%s%s" % (map.get_layer(), separator)
  114. string += "%s%s" % (map.get_mapset(), separator)
  115. string += "%s%s" % (start, separator)
  116. string += "%s%s" % (end, separator)
  117. string += "%s%s" % (delta, separator)
  118. string += "%s" % (delta_first)
  119. print string
  120. else:
  121. # In comma separated mode only map ids are needed
  122. if method == "comma":
  123. columns = "id"
  124. rows = sp.get_registered_maps(columns, where, order, dbif)
  125. if rows:
  126. if method == "comma":
  127. string = ""
  128. count = 0
  129. for row in rows:
  130. if count == 0:
  131. string += row["id"]
  132. else:
  133. string += ",%s" % row["id"]
  134. count += 1
  135. print string
  136. elif method == "cols":
  137. # Print the column names if requested
  138. if header:
  139. output = ""
  140. count = 0
  141. collist = columns.split(",")
  142. for key in collist:
  143. if count > 0:
  144. output += separator + str(key)
  145. else:
  146. output += str(key)
  147. count += 1
  148. print output
  149. for row in rows:
  150. output = ""
  151. count = 0
  152. for col in row:
  153. if count > 0:
  154. output += separator + str(col)
  155. else:
  156. output += str(col)
  157. count += 1
  158. print output
  159. if connected:
  160. dbif.close()