t.rast.neighbors.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rast.neighbors
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Performs a neighborhood analysis for each map in a space time
  9. # raster dataset.
  10. # COPYRIGHT: (C) 2013 by the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (version 2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Performs a neighborhood analysis for each map in a space time raster dataset.
  19. #% keywords: temporal
  20. #% keywords: aggregation
  21. #%end
  22. #%option G_OPT_STRDS_INPUT
  23. #%end
  24. #%option G_OPT_STRDS_OUTPUT
  25. #%end
  26. #%option G_OPT_T_WHERE
  27. #%end
  28. #%option
  29. #% key: size
  30. #% type: integer
  31. #% description: Neighborhood size
  32. #% required: no
  33. #% multiple: no
  34. #% answer: 3
  35. #%end
  36. #%option
  37. #% key: method
  38. #% type: string
  39. #% description: Aggregate operation to be performed on the raster maps
  40. #% required: yes
  41. #% multiple: no
  42. #% options: average,median,mode,minimum,maximum,range,stddev,sum,count,variance,diversity,interspersion,quart1,quart3,perc90,quantile
  43. #% answer: average
  44. #%end
  45. #%option G_OPT_R_BASE
  46. #%end
  47. #%option
  48. #% key: nprocs
  49. #% type: integer
  50. #% description: Number of r.neighbor processes to run in parallel
  51. #% required: no
  52. #% multiple: no
  53. #% answer: 1
  54. #%end
  55. #%flag
  56. #% key: n
  57. #% description: Register Null maps
  58. #%end
  59. import copy
  60. import grass.script as grass
  61. import grass.temporal as tgis
  62. import grass.pygrass.modules as pymod
  63. ############################################################################
  64. def main():
  65. # Get the options
  66. input = options["input"]
  67. output = options["output"]
  68. where = options["where"]
  69. size = options["size"]
  70. base = options["base"]
  71. register_null = flags["n"]
  72. method = options["method"]
  73. nprocs = options["nprocs"]
  74. # Make sure the temporal database exists
  75. tgis.init()
  76. # We need a database interface
  77. dbif = tgis.SQLDatabaseInterfaceConnection()
  78. dbif.connect()
  79. overwrite = grass.overwrite()
  80. sp = tgis.open_old_space_time_dataset(input, "strds", dbif)
  81. maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif)
  82. if not maps:
  83. dbif.close()
  84. grass.warning(_("Space time raster dataset <%s> is empty") % sp.get_id())
  85. return
  86. new_sp = tgis.check_new_space_time_dataset(input, "strds", dbif=dbif,
  87. overwrite=overwrite)
  88. # Configure the r.neighbor module
  89. neighbor_module = pymod.Module("r.neighbors", input="dummy",
  90. output="dummy", run_=False,
  91. finish_=False, size=int(size),
  92. method=method, overwrite=overwrite,
  93. quiet=True)
  94. # The module queue for parallel execution
  95. process_queue = pymod.ParallelModuleQueue(int(nprocs))
  96. count = 0
  97. num_maps = len(maps)
  98. new_maps = []
  99. # run r.neighbors all selected maps
  100. for map in maps:
  101. count += 1
  102. map_name = "%s_%i" % (base, count)
  103. new_map = tgis.open_new_map_dataset(map_name, None, type="raster",
  104. temporal_extent=map.get_temporal_extent(),
  105. overwrite=overwrite, dbif=dbif)
  106. new_maps.append(new_map)
  107. mod = copy.deepcopy(neighbor_module)
  108. mod(input=map.get_id(), output=new_map.get_id())
  109. print(mod.get_bash())
  110. process_queue.put(mod)
  111. # Wait for unfinished processes
  112. process_queue.wait()
  113. # Open the new space time raster dataset
  114. ttype, stype, title, descr = sp.get_initial_values()
  115. new_sp = tgis.open_new_space_time_dataset(output, "strds", ttype, title,
  116. descr, stype, dbif, overwrite)
  117. num_maps = len(new_maps)
  118. # collect empty maps to remove them
  119. empty_maps = []
  120. # Register the maps in the database
  121. count = 0
  122. for map in new_maps:
  123. count += 1
  124. if count%10 == 0:
  125. grass.percent(count, num_maps, 1)
  126. # Do not register empty maps
  127. map.load()
  128. if map.metadata.get_min() is None and \
  129. map.metadata.get_max() is None:
  130. if not register_null:
  131. empty_maps.append(map)
  132. continue
  133. # Insert map in temporal database
  134. map.insert(dbif)
  135. new_sp.register_map(map, dbif)
  136. # Update the spatio-temporal extent and the metadata table entries
  137. new_sp.update_from_registered_maps(dbif)
  138. grass.percent(1, 1, 1)
  139. # Remove empty maps
  140. if len(empty_maps) > 0:
  141. names = ""
  142. count = 0
  143. for map in empty_maps:
  144. if count == 0:
  145. count += 1
  146. names += "%s" % (map.get_name())
  147. else:
  148. names += ",%s" % (map.get_name())
  149. grass.run_command("g.remove", rast=names, quiet=True)
  150. dbif.close()
  151. ############################################################################
  152. if __name__ == "__main__":
  153. options, flags = grass.parser()
  154. main()