aggregation.py 4.8 KB

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