list_stds.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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) 2012-2013 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_stds 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. msgr = get_tgis_message_interface()
  47. sp = open_old_space_time_dataset(input, type, dbif)
  48. if separator is None or separator == "":
  49. separator = "\t"
  50. # This method expects a list of objects for gap detection
  51. if method == "delta" or method == "deltagaps" or method == "gran":
  52. if type == "stvds":
  53. columns = "id,name,layer,mapset,start_time,end_time"
  54. else:
  55. columns = "id,name,mapset,start_time,end_time"
  56. if method == "deltagaps":
  57. maps = sp.get_registered_maps_as_objects_with_gaps(where=where, dbif=dbif)
  58. elif method == "delta":
  59. maps = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif)
  60. elif method == "gran":
  61. if gran is not None and gran != "":
  62. maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran, dbif=dbif)
  63. else:
  64. maps = sp.get_registered_maps_as_objects_by_granularity(dbif=dbif)
  65. if header:
  66. string = ""
  67. string += "%s%s" % ("id", separator)
  68. string += "%s%s" % ("name", separator)
  69. if type == "stvds":
  70. string += "%s%s" % ("layer", separator)
  71. string += "%s%s" % ("mapset", separator)
  72. string += "%s%s" % ("start_time", separator)
  73. string += "%s%s" % ("end_time", separator)
  74. string += "%s%s" % ("interval_length", separator)
  75. string += "%s" % ("distance_from_begin")
  76. print string
  77. if maps and len(maps) > 0:
  78. if isinstance(maps[0], list):
  79. if len(maps[0]) > 0:
  80. first_time, dummy = maps[0][0].get_temporal_extent_as_tuple()
  81. else:
  82. msgr.warning(_("Empty map list."))
  83. return
  84. else:
  85. first_time, dummy = maps[0].get_temporal_extent_as_tuple()
  86. for mymap in maps:
  87. if isinstance(mymap, list):
  88. if len(mymap) > 0:
  89. map = mymap[0]
  90. else:
  91. msgr.fatal(_("Empty entry in map list, this should not happen."))
  92. else:
  93. map = mymap
  94. start, end = map.get_temporal_extent_as_tuple()
  95. if end:
  96. delta = end - start
  97. else:
  98. delta = None
  99. delta_first = start - first_time
  100. if map.is_time_absolute():
  101. if end:
  102. delta = time_delta_to_relative_time(delta)
  103. delta_first = time_delta_to_relative_time(delta_first)
  104. string = ""
  105. string += "%s%s" % (map.get_id(), separator)
  106. string += "%s%s" % (map.get_name(), separator)
  107. if type == "stvds":
  108. string += "%s%s" % (map.get_layer(), separator)
  109. string += "%s%s" % (map.get_mapset(), separator)
  110. string += "%s%s" % (start, separator)
  111. string += "%s%s" % (end, separator)
  112. string += "%s%s" % (delta, separator)
  113. string += "%s" % (delta_first)
  114. print string
  115. else:
  116. # In comma separated mode only map ids are needed
  117. if method == "comma":
  118. columns = "id"
  119. rows = sp.get_registered_maps(columns, where, order, dbif)
  120. if rows:
  121. if method == "comma":
  122. string = ""
  123. count = 0
  124. for row in rows:
  125. if count == 0:
  126. string += row["id"]
  127. else:
  128. string += ",%s" % row["id"]
  129. count += 1
  130. print string
  131. elif method == "cols":
  132. # Print the column names if requested
  133. if header:
  134. output = ""
  135. count = 0
  136. collist = columns.split(",")
  137. for key in collist:
  138. if count > 0:
  139. output += separator + str(key)
  140. else:
  141. output += str(key)
  142. count += 1
  143. print output
  144. for row in rows:
  145. output = ""
  146. count = 0
  147. for col in row:
  148. if count > 0:
  149. output += separator + str(col)
  150. else:
  151. output += str(col)
  152. count += 1
  153. print output
  154. if connected:
  155. dbif.close()