extract.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. """
  2. Extract functions for space time raster, 3d raster and vector datasets
  3. (C) 2012-2013 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Soeren Gebbert
  8. """
  9. from grass.script.utils import get_num_suffix
  10. from .space_time_datasets import *
  11. from .open_stds import *
  12. from .datetime_math import create_suffix_from_datetime
  13. from .datetime_math import create_time_suffix
  14. from .datetime_math import create_numeric_suffic
  15. from multiprocessing import Process
  16. import grass.script as gscript
  17. from grass.exceptions import CalledModuleError
  18. ############################################################################
  19. def extract_dataset(input, output, type, where, expression, base, time_suffix,
  20. nprocs=1, register_null=False, layer=1,
  21. vtype="point,line,boundary,centroid,area,face", ):
  22. """Extract a subset of a space time raster, raster3d or vector dataset
  23. A mapcalc expression can be provided to process the temporal extracted
  24. maps.
  25. Mapcalc expressions are supported for raster and raster3d maps.
  26. :param input: The name of the input space time raster/raster3d dataset
  27. :param output: The name of the extracted new space time raster/raster3d
  28. dataset
  29. :param type: The type of the dataset: "raster", "raster3d" or vector
  30. :param where: The temporal SQL WHERE statement for subset extraction
  31. :param expression: The r(3).mapcalc expression or the v.extract where
  32. statement
  33. :param base: The base name of the new created maps in case a mapclac
  34. expression is provided
  35. :param time_suffix: string to choose which suffix to use: gran, time, num%*
  36. (where * are digits)
  37. :param nprocs: The number of parallel processes to be used for mapcalc
  38. processing
  39. :param register_null: Set this number True to register empty maps
  40. (only raster and raster3d maps)
  41. :param layer: The vector layer number to be used when no timestamped
  42. layer is present, default is 1
  43. :param vtype: The feature type to be extracted for vector maps, default
  44. is point,line,boundary,centroid,area and face
  45. """
  46. # Check the parameters
  47. msgr = get_tgis_message_interface()
  48. if expression and not base:
  49. msgr.fatal(_("You need to specify the base name of new created maps"))
  50. mapset = get_current_mapset()
  51. dbif = SQLDatabaseInterfaceConnection()
  52. dbif.connect()
  53. sp = open_old_stds(input, type, dbif)
  54. # Check the new stds
  55. new_sp = check_new_stds(output, type, dbif, gscript.overwrite())
  56. if type == "vector":
  57. rows = sp.get_registered_maps(
  58. "id,name,mapset,layer", where, "start_time", dbif)
  59. else:
  60. rows = sp.get_registered_maps("id", where, "start_time", dbif)
  61. new_maps = {}
  62. if rows:
  63. num_rows = len(rows)
  64. msgr.percent(0, num_rows, 1)
  65. # Run the mapcalc expression
  66. if expression:
  67. count = 0
  68. proc_count = 0
  69. proc_list = []
  70. for row in rows:
  71. count += 1
  72. if count % 10 == 0:
  73. msgr.percent(count, num_rows, 1)
  74. if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran':
  75. old_map = sp.get_new_map_instance(row["id"])
  76. old_map.select(dbif)
  77. suffix = create_suffix_from_datetime(old_map.temporal_extent.get_start_time(),
  78. sp.get_granularity())
  79. map_name = "{ba}_{su}".format(ba=base, su=suffix)
  80. elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time':
  81. old_map = sp.get_new_map_instance(row["id"])
  82. old_map.select(dbif)
  83. suffix = create_time_suffix(old_map)
  84. map_name = "{ba}_{su}".format(ba=base, su=suffix)
  85. else:
  86. map_name = create_numeric_suffic(base, count, time_suffix)
  87. # We need to modify the r(3).mapcalc expression
  88. if type != "vector":
  89. expr = expression
  90. expr = expr.replace(sp.base.get_map_id(), row["id"])
  91. expr = expr.replace(sp.base.get_name(), row["id"])
  92. expr = "%s = %s" % (map_name, expr)
  93. # We need to build the id
  94. map_id = AbstractMapDataset.build_id(map_name, mapset)
  95. else:
  96. map_id = AbstractMapDataset.build_id(map_name, mapset,
  97. row["layer"])
  98. new_map = sp.get_new_map_instance(map_id)
  99. # Check if new map is in the temporal database
  100. if new_map.is_in_db(dbif):
  101. if gscript.overwrite():
  102. # Remove the existing temporal database entry
  103. new_map.delete(dbif)
  104. new_map = sp.get_new_map_instance(map_id)
  105. else:
  106. msgr.error(_("Map <%s> is already in temporal database"
  107. ", use overwrite flag to overwrite") %
  108. (new_map.get_map_id()))
  109. continue
  110. # Add process to the process list
  111. if type == "raster":
  112. msgr.verbose(_("Applying r.mapcalc expression: \"%s\"")
  113. % expr)
  114. proc_list.append(Process(target=run_mapcalc2d,
  115. args=(expr,)))
  116. elif type == "raster3d":
  117. msgr.verbose(_("Applying r3.mapcalc expression: \"%s\"")
  118. % expr)
  119. proc_list.append(Process(target=run_mapcalc3d,
  120. args=(expr,)))
  121. elif type == "vector":
  122. msgr.verbose(_("Applying v.extract where statement: \"%s\"")
  123. % expression)
  124. if row["layer"]:
  125. proc_list.append(Process(target=run_vector_extraction,
  126. args=(row["name"] + "@" +
  127. row["mapset"], map_name,
  128. row["layer"], vtype,
  129. expression)))
  130. else:
  131. proc_list.append(Process(target=run_vector_extraction,
  132. args=(row["name"] + "@" +
  133. row["mapset"], map_name,
  134. layer, vtype,
  135. expression)))
  136. proc_list[proc_count].start()
  137. proc_count += 1
  138. # Join processes if the maximum number of processes are
  139. # reached or the end of the loop is reached
  140. if proc_count == nprocs or count == num_rows:
  141. proc_count = 0
  142. exitcodes = 0
  143. for proc in proc_list:
  144. proc.join()
  145. exitcodes += proc.exitcode
  146. if exitcodes != 0:
  147. dbif.close()
  148. msgr.fatal(_("Error in computation process"))
  149. # Empty process list
  150. proc_list = []
  151. # Store the new maps
  152. new_maps[row["id"]] = new_map
  153. msgr.percent(0, num_rows, 1)
  154. temporal_type, semantic_type, title, description = sp.get_initial_values()
  155. new_sp = open_new_stds(output, type, sp.get_temporal_type(), title,
  156. description, semantic_type, dbif,
  157. gscript.overwrite())
  158. # collect empty maps to remove them
  159. empty_maps = []
  160. # Register the maps in the database
  161. count = 0
  162. for row in rows:
  163. count += 1
  164. if count % 10 == 0:
  165. msgr.percent(count, num_rows, 1)
  166. old_map = sp.get_new_map_instance(row["id"])
  167. old_map.select(dbif)
  168. if expression:
  169. # Register the new maps
  170. if row["id"] in new_maps:
  171. new_map = new_maps[row["id"]]
  172. # Read the raster map data
  173. new_map.load()
  174. # In case of a empty map continue, do not register empty
  175. # maps
  176. if type == "raster" or type == "raster3d":
  177. if new_map.metadata.get_min() is None and \
  178. new_map.metadata.get_max() is None:
  179. if not register_null:
  180. empty_maps.append(new_map)
  181. continue
  182. elif type == "vector":
  183. if new_map.metadata.get_number_of_primitives() == 0 or \
  184. new_map.metadata.get_number_of_primitives() is None:
  185. if not register_null:
  186. empty_maps.append(new_map)
  187. continue
  188. # Set the time stamp
  189. new_map.set_temporal_extent(old_map.get_temporal_extent())
  190. # Insert map in temporal database
  191. new_map.insert(dbif)
  192. new_sp.register_map(new_map, dbif)
  193. else:
  194. new_sp.register_map(old_map, dbif)
  195. # Update the spatio-temporal extent and the metadata table entries
  196. new_sp.update_from_registered_maps(dbif)
  197. msgr.percent(num_rows, num_rows, 1)
  198. # Remove empty maps
  199. if len(empty_maps) > 0:
  200. names = ""
  201. count = 0
  202. for map in empty_maps:
  203. if count == 0:
  204. names += "%s" % (map.get_name())
  205. else:
  206. names += ",%s" % (map.get_name())
  207. count += 1
  208. if type == "raster":
  209. gscript.run_command("g.remove", flags='f', type='raster',
  210. name=names, quiet=True)
  211. elif type == "raster3d":
  212. gscript.run_command("g.remove", flags='f', type='raster_3d',
  213. name=names, quiet=True)
  214. elif type == "vector":
  215. gscript.run_command("g.remove", flags='f', type='vector',
  216. name=names, quiet=True)
  217. dbif.close()
  218. ###############################################################################
  219. def run_mapcalc2d(expr):
  220. """Helper function to run r.mapcalc in parallel"""
  221. try:
  222. gscript.run_command("r.mapcalc", expression=expr,
  223. overwrite=gscript.overwrite(), quiet=True)
  224. except CalledModuleError:
  225. exit(1)
  226. def run_mapcalc3d(expr):
  227. """Helper function to run r3.mapcalc in parallel"""
  228. try:
  229. gscript.run_command("r3.mapcalc", expression=expr,
  230. overwrite=gscript.overwrite(), quiet=True)
  231. except CalledModuleError:
  232. exit(1)
  233. def run_vector_extraction(input, output, layer, type, where):
  234. """Helper function to run r.mapcalc in parallel"""
  235. try:
  236. gscript.run_command("v.extract", input=input, output=output,
  237. layer=layer, type=type, where=where,
  238. overwrite=gscript.overwrite(), quiet=True)
  239. except CalledModuleError:
  240. exit(1)