extract.py 11 KB

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