list.py 6.9 KB

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