aggregation.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. def collect_map_names(sp, dbif, start, end):
  18. where = " start_time >= \'%s\' and start_time < \'%s\'" % (start, end)
  19. print where
  20. rows = sp.get_registered_maps("id", where, "start_time", dbif)
  21. if not rows:
  22. return None
  23. names = []
  24. for row in rows:
  25. names.append(row["id"])
  26. return names
  27. def aggregate_raster_maps(dataset, mapset, inputs, base, start, end, count, method, register_null, dbif):
  28. core.verbose(_("Aggregate %s raster maps") %(len(inputs)))
  29. output = "%s_%i" % (base, count)
  30. map_id = output + "@" + mapset
  31. new_map = dataset.get_new_map_instance(map_id)
  32. # Check if new map is in the temporal database
  33. if new_map.is_in_db(dbif):
  34. if core.overwrite() == True:
  35. # Remove the existing temporal database entry
  36. new_map.delete(dbif)
  37. new_map = dataset.get_new_map_instance(map_id)
  38. else:
  39. core.error(_("Raster map <%s> is already in temporal database, use overwrite flag to overwrite"))
  40. return
  41. core.verbose(_("Compute aggregation of maps between %s - %s" % (str(start), str(end))))
  42. # Create the r.series input file
  43. filename = core.tempfile(True)
  44. file = open(filename, 'w')
  45. for name in inputs:
  46. string = "%s\n" % (name)
  47. file.write(string)
  48. file.close()
  49. # Run r.series
  50. ret = core.run_command("r.series", flags="z", file=filename, output=output, overwrite=core.overwrite(), method=method)
  51. if ret != 0:
  52. dbif.close()
  53. core.fatal(_("Error while r.series computation"))
  54. # Read the raster map data
  55. new_map.load()
  56. # In case of a null map continue, do not register null maps
  57. if new_map.metadata.get_min() == None and new_map.metadata.get_max() == None:
  58. if not register_null:
  59. core.run_command("g.remove", rast=output)
  60. return
  61. # Set the time stamp
  62. if dataset.is_time_absolute():
  63. new_map.set_absolute_time(start, end, None)
  64. else:
  65. new_map.set_relative_time(start, end)
  66. # Insert map in temporal database
  67. new_map.insert(dbif)
  68. dataset.register_map(new_map, dbif)