aggregation.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. from grass.exceptions import CalledModuleError
  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, offset=0):
  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. @param offset Offset to be added to the map counter to create the map ids
  96. """
  97. msgr = get_tgis_message_interface()
  98. msgr.verbose(_("Aggregating %s raster maps") % (len(inputs)))
  99. output = "%s_%i" % (base, int(offset) + count)
  100. mapset = get_current_mapset()
  101. map_id = output + "@" + mapset
  102. new_map = RasterDataset(map_id)
  103. # Check if new map is in the temporal database
  104. if new_map.is_in_db(dbif):
  105. if core.overwrite() == True:
  106. # Remove the existing temporal database entry
  107. new_map.delete(dbif)
  108. new_map = RasterDataset(map_id)
  109. else:
  110. msgr.error(_("Raster map <%s> is already in temporal database, " \
  111. "use overwrite flag to overwrite"))
  112. return
  113. msgr.verbose(_("Computing aggregation of maps between %(st)s - %(end)s" % {
  114. 'st': str(start), 'end': str(end)}))
  115. # Create the r.series input file
  116. filename = core.tempfile(True)
  117. file = open(filename, 'w')
  118. for name in inputs:
  119. string = "%s\n" % (name)
  120. file.write(string)
  121. file.close()
  122. # Run r.series
  123. try:
  124. if len(inputs) > 1000:
  125. gscript.run_command("r.series", flags="z", file=filename,
  126. output=output, overwrite=gscript.overwrite(),
  127. method=method)
  128. else:
  129. gscript.run_command("r.series", file=filename,
  130. output=output, overwrite=gscript.overwrite(),
  131. method=method)
  132. except CalledModuleError:
  133. dbif.close()
  134. msgr.fatal(_("Error occurred in r.series computation"))
  135. # Read the raster map data
  136. new_map.load()
  137. # In case of a null map continue, do not register null maps
  138. if new_map.metadata.get_min() is None and new_map.metadata.get_max() is None:
  139. if not register_null:
  140. core.run_command("g.remove", type="rast", name=output, flags="f")
  141. return None
  142. return new_map