extract.py 12 KB

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