aggregation.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.aggregate_raster_maps(dataset, mapset, inputs, base, start, end,
  8. count, method, register_null, dbif)
  9. ...
  10. @endcode
  11. (C) 2008-2011 by the GRASS Development Team
  12. This program is free software under the GNU General Public
  13. License (>=v2). Read the file COPYING that comes with GRASS
  14. for details.
  15. @author Soeren Gebbert
  16. """
  17. from space_time_datasets import *
  18. import grass.lib.gis as libgis
  19. ###############################################################################
  20. def collect_map_names(sp, dbif, start, end, sampling):
  21. """!Gather all maps from dataset using a specific sample method
  22. @param sp The space time raster dataset to select aps from
  23. @param dbif The temporal database interface to use
  24. @param start The start time of the sample interval, may be relative or
  25. absolute
  26. @param end The end time of the sample interval, may be relative or
  27. absolute
  28. @param sampling The sampling methods to use
  29. """
  30. use_start = False
  31. use_during = False
  32. use_overlap = False
  33. use_contain = False
  34. use_equal = False
  35. use_follows = False
  36. use_precedes = False
  37. # Initialize the methods
  38. if sampling:
  39. for name in sampling.split(","):
  40. if name == "start":
  41. use_start = True
  42. if name == "during":
  43. use_during = True
  44. if name == "overlap":
  45. use_overlap = True
  46. if name == "contain":
  47. use_contain = True
  48. if name == "equal":
  49. use_equal = True
  50. if name == "follows":
  51. use_follows = True
  52. if name == "precedes":
  53. use_precedes = True
  54. else:
  55. use_start = True
  56. if sp.get_map_time() != "interval":
  57. use_start = True
  58. use_during = False
  59. use_overlap = False
  60. use_contain = False
  61. use_equal = False
  62. use_follows = False
  63. use_precedes = False
  64. where = create_temporal_relation_sql_where_statement(start, end,
  65. use_start,
  66. use_during,
  67. use_overlap,
  68. use_contain,
  69. use_equal,
  70. use_follows,
  71. use_precedes)
  72. rows = sp.get_registered_maps("id", where, "start_time", dbif)
  73. if not rows:
  74. return None
  75. names = []
  76. for row in rows:
  77. names.append(row["id"])
  78. return names
  79. ###############################################################################
  80. def aggregate_raster_maps(inputs, base, start, end, count, method,
  81. register_null, dbif):
  82. """!Aggregate a list of raster input maps with r.series
  83. @param inputs The names of the raster maps to be aggregated
  84. @param base The basename of the new created raster maps
  85. @param start The start time of the sample interval, may be relative or
  86. absolute
  87. @param end The end time of the sample interval, may be relative or
  88. absolute
  89. @param count The number to be attached to the basename of the new
  90. created raster map
  91. @param method The aggreation method to be used by r.series
  92. @param register_null If true null maps will be registered in the space
  93. time raster dataset, if false not
  94. @param dbif The temporal database interface to use
  95. """
  96. msgr = get_tgis_message_interface()
  97. msgr.verbose(_("Aggregate %s raster maps") % (len(inputs)))
  98. output = "%s_%i" % (base, count)
  99. mapset = libgis.G_mapset()
  100. map_id = output + "@" + mapset
  101. new_map = RasterDataset(map_id)
  102. # Check if new map is in the temporal database
  103. if new_map.is_in_db(dbif):
  104. if core.overwrite() == True:
  105. # Remove the existing temporal database entry
  106. new_map.delete(dbif)
  107. new_map = RasterDataset(map_id)
  108. else:
  109. msgr.error(_("Raster map <%s> is already in temporal database, " \
  110. "use overwrite flag to overwrite"))
  111. return
  112. msgr.verbose(_("Compute aggregation of maps between %(st)s - %(end)s" % {
  113. 'st': str(start), 'end': str(end)}))
  114. # Create the r.series input file
  115. filename = core.tempfile(True)
  116. file = open(filename, 'w')
  117. for name in inputs:
  118. string = "%s\n" % (name)
  119. file.write(string)
  120. file.close()
  121. # Run r.series
  122. ret = core.run_command("r.series", flags="z", file=filename,
  123. output=output, overwrite=core.overwrite(),
  124. method=method)
  125. if ret != 0:
  126. dbif.close()
  127. core.fatal(_("Error while r.series computation"))
  128. # Read the raster map data
  129. new_map.load()
  130. # In case of a null map continue, do not register null maps
  131. if new_map.metadata.get_min() is None and new_map.metadata.get_max() is None:
  132. if not register_null:
  133. core.run_command("g.remove", rast=output)
  134. return None
  135. return new_map