aggregation.py 5.1 KB

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