aggregation.py 5.4 KB

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