aggregation.py 5.2 KB

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