aggregation.py 4.5 KB

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