list.py 7.1 KB

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