aggregation.py 3.5 KB

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