abstract_map_dataset.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. # -*- coding: utf-8 -*-
  2. """!@package grass.temporal
  3. @brief GRASS Python scripting module (temporal GIS functions)
  4. Temporal GIS related functions to be used in temporal GIS Python library package.
  5. Usage:
  6. @code
  7. import grass.temporal as tgis
  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 abstract_dataset import *
  17. from datetime_math import *
  18. ###############################################################################
  19. class abstract_map_dataset(abstract_dataset):
  20. """!This is the base class for all maps (raster, vector, raster3d)
  21. providing additional function to set the valid time and the spatial extent.
  22. """
  23. def get_new_stds_instance(self, ident):
  24. """!Return a new space time dataset instance in which maps are stored with the type of this class
  25. @param ident: The identifier of the dataset
  26. """
  27. raise IOError("This method must be implemented in the subclasses")
  28. def get_stds_register(self):
  29. """!Return the space time dataset register table name in which stds are listed in which this map is registered"""
  30. raise IOError("This method must be implemented in the subclasses")
  31. def set_stds_register(self, name):
  32. """!Set the space time dataset register table name.
  33. This table stores all space time datasets in which this map is registered.
  34. @param ident: The name of the register table
  35. """
  36. raise IOError("This method must be implemented in the subclasses")
  37. def get_timestamp_module_name(self):
  38. """!Return the name of the C-module to set the time stamp in the file system"""
  39. raise IOError("This method must be implemented in the subclasses")
  40. def load(self):
  41. """!Load the content of this object from map files"""
  42. raise IOError("This method must be implemented in the subclasses")
  43. def check_resolution_with_current_region(self):
  44. """!Check if the raster or voxel resolution is finer than the current resolution
  45. Return "finer" in case the raster/voxel resolution is finer than the current region
  46. Return "coarser" in case the raster/voxel resolution is coarser than the current region
  47. Vector maps are alwyas finer than the current region
  48. """
  49. raise IOError("This method must be implemented in the subclasses")
  50. def get_map_id(self):
  51. """!Return the map id. The map id is the unique map identifier in grass and must not be equal to the
  52. primary key identifier (id) of the map in the database. Since vector maps may have layer information,
  53. the unique id is a combination of name, layer and mapset.
  54. Use get_map_id() every time your need to access the grass map in the file system but not to identify
  55. map information in the temporal database.
  56. """
  57. return self.base.get_map_id()
  58. def build_id(self, name, mapset, layer=None):
  59. """!Convenient method to build the unique identifier
  60. Existing layer and mapset definitions in the name string will be reused
  61. @param return the id of the vector map as name(:layer)@mapset while layer is optional
  62. """
  63. # Check if the name includes any mapset
  64. if name.find("@") >= 0:
  65. name, mapset = name.split("@")[0]
  66. # Check for layer number in map name
  67. if name.find(":") >= 0:
  68. name, layer = name.split(":")
  69. if layer:
  70. return "%s:%s@%s"%(name, layer, mapset)
  71. else:
  72. return "%s@%s"%(name, mapset)
  73. def get_layer(self):
  74. """!Return the layer of the map or None in case no layer is defined"""
  75. return self.base.get_layer()
  76. def print_info(self):
  77. """!Print information about this class in human readable style"""
  78. if self.get_type() == "raster":
  79. # 1 2 3 4 5 6 7
  80. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  81. print ""
  82. print " +-------------------- Raster Dataset ----------------------------------------+"
  83. if self.get_type() == "raster3d":
  84. # 1 2 3 4 5 6 7
  85. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  86. print ""
  87. print " +-------------------- Raster3d Dataset --------------------------------------+"
  88. if self.get_type() == "vector":
  89. # 1 2 3 4 5 6 7
  90. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  91. print ""
  92. print " +-------------------- Vector Dataset ----------------------------------------+"
  93. print " | |"
  94. self.base.print_info()
  95. if self.is_time_absolute():
  96. self.absolute_time.print_info()
  97. if self.is_time_relative():
  98. self.relative_time.print_info()
  99. self.spatial_extent.print_info()
  100. self.metadata.print_info()
  101. datasets = self.get_registered_datasets()
  102. count = 0
  103. string = ""
  104. if datasets:
  105. for ds in datasets:
  106. if count == 0:
  107. string += ds["id"]
  108. else:
  109. string += ",%s" % ds["id"]
  110. count += 1
  111. if count > 2:
  112. string += " | ............................ "
  113. print " | Registered datasets ........ " + string
  114. print " +----------------------------------------------------------------------------+"
  115. def print_shell_info(self):
  116. """!Print information about this class in shell style"""
  117. self.base.print_shell_info()
  118. if self.is_time_absolute():
  119. self.absolute_time.print_shell_info()
  120. if self.is_time_relative():
  121. self.relative_time.print_shell_info()
  122. self.spatial_extent.print_shell_info()
  123. self.metadata.print_shell_info()
  124. datasets = self.get_registered_datasets()
  125. count = 0
  126. string = ""
  127. for ds in datasets:
  128. if count == 0:
  129. string += ds["id"]
  130. else:
  131. string += ",%s" % ds["id"]
  132. count += 1
  133. print "registered_datasets=" + string
  134. def set_absolute_time(self, start_time, end_time=None, timezone=None):
  135. """!Set the absolute time interval with start time and end time
  136. @param start_time: a datetime object specifying the start time of the map
  137. @param end_time: a datetime object specifying the end time of the map
  138. @param timezone: Thee timezone of the map
  139. """
  140. if start_time and not isinstance(start_time, datetime) :
  141. if self.get_layer():
  142. core.fatal(_("Start time must be of type datetime for %s map <%s> with layer: %s") % (self.get_type(), self.get_map_id(), self.get_layer()))
  143. else:
  144. core.fatal(_("Start time must be of type datetime for %s map <%s>") % (self.get_type(), self.get_map_id()))
  145. if end_time and not isinstance(end_time, datetime) :
  146. if self.get_layer():
  147. core.fatal(_("End time must be of type datetime for %s map <%s> with layer: %s") % (self.get_type(), self.get_map_id(), self.get_layer()))
  148. else:
  149. core.fatal(_("End time must be of type datetime for %s map <%s>") % (self.get_type(), self.get_map_id()))
  150. if start_time and end_time:
  151. if start_time > end_time:
  152. if self.get_layer():
  153. core.fatal(_("End time must be greater than start time for %s map <%s> with layer: %s") % (self.get_type(), self.get_map_id(), self.get_layer()))
  154. else:
  155. core.fatal(_("End time must be greater than start time for %s map <%s>") % (self.get_type(), self.get_map_id()))
  156. else:
  157. # Do not create an interval in case start and end time are equal
  158. if start_time == end_time:
  159. end_time = None
  160. self.base.set_ttype("absolute")
  161. self.absolute_time.set_start_time(start_time)
  162. self.absolute_time.set_end_time(end_time)
  163. self.absolute_time.set_timezone(timezone)
  164. def update_absolute_time(self, start_time, end_time=None, timezone=None, dbif = None):
  165. """!Update the absolute time
  166. This method should always be used to set the absolute time. Do not use insert() or update()
  167. to the the time. This update functions assures that the *.timestamp commands are invoked.
  168. @param start_time: a datetime object specifying the start time of the map
  169. @param end_time: a datetime object specifying the end time of the map
  170. @param timezone: Thee timezone of the map
  171. """
  172. dbif, connect = init_dbif(dbif)
  173. self.set_absolute_time(start_time, end_time, timezone)
  174. self.absolute_time.update_all(dbif)
  175. self.base.update(dbif)
  176. if connect == True:
  177. dbif.close()
  178. self.write_absolute_time_to_file()
  179. def write_absolute_time_to_file(self):
  180. """!Start the grass timestamp module to set the time in the file system"""
  181. start_time, end_time, unit = self.get_absolute_time()
  182. start = datetime_to_grass_datetime_string(start_time)
  183. if end_time:
  184. end = datetime_to_grass_datetime_string(end_time)
  185. start += " / %s"%(end)
  186. core.run_command(self.get_timestamp_module_name(), map=self.get_map_id(), date=start)
  187. def set_relative_time(self, start_time, end_time, unit):
  188. """!Set the relative time interval
  189. @param start_time: A double value
  190. @param end_time: A double value
  191. @param unit: The unit of the relative time. Supported units: years, months, days, hours, minutes, seconds
  192. Return True for success and False otherwise
  193. """
  194. if not self.check_relative_time_unit(unit):
  195. if self.get_layer():
  196. core.error(_("Unsupported relative time unit type for %s map <%s> with layer %s: %s") % (self.get_type(), self.get_id(), self.get_layer(), unit))
  197. else:
  198. core.error(_("Unsupported relative time unit type for %s map <%s>: %s") % (self.get_type(), self.get_id(), unit))
  199. return False
  200. if start_time != None and end_time != None:
  201. if int(start_time) > int(end_time):
  202. if self.get_layer():
  203. core.error(_("End time must be greater than start time for %s map <%s> with layer %s") % (self.get_type(), self.get_id(), self.get_layer()))
  204. else:
  205. core.error(_("End time must be greater than start time for %s map <%s>") % (self.get_type(), self.get_id()))
  206. return False
  207. else:
  208. # Do not create an interval in case start and end time are equal
  209. if start_time == end_time:
  210. end_time = None
  211. self.base.set_ttype("relative")
  212. self.relative_time.set_unit(unit)
  213. self.relative_time.set_start_time(int(start_time))
  214. if end_time != None:
  215. self.relative_time.set_end_time(int(end_time))
  216. else:
  217. self.relative_time.set_end_time(None)
  218. return True
  219. def update_relative_time(self, start_time, end_time, unit, dbif = None):
  220. """!Update the relative time interval
  221. This method should always be used to set the absolute time. Do not use insert() or update()
  222. to the the time. This update functions assures that the *.timestamp commands are invoked.
  223. @param start_time: A double value
  224. @param end_time: A double value
  225. @param dbif: The database interface to be used
  226. """
  227. dbif, connect = init_dbif(dbif)
  228. if self.set_relative_time(start_time, end_time, unit):
  229. self.relative_time.update_all(dbif)
  230. self.base.update(dbif)
  231. dbif.connection.commit()
  232. if connect == True:
  233. dbif.close()
  234. self.write_relative_time_to_file()
  235. def write_relative_time_to_file(self):
  236. """!Start the grass timestamp module to set the time in the file system"""
  237. start_time, end_time, unit = self.get_relative_time()
  238. start = "%i %s"%(int(start_time), unit)
  239. if end_time != None:
  240. end = "%i %s"%(int(end_time), unit)
  241. start += " / %s"%(end)
  242. core.run_command(self.get_timestamp_module_name(), map=self.get_map_id(), date=start)
  243. def set_spatial_extent(self, north, south, east, west, top=0, bottom=0):
  244. """!Set the spatial extent of the map
  245. @param north: The northern edge
  246. @param south: The southern edge
  247. @param east: The eastern edge
  248. @param west: The western edge
  249. @param top: The top edge
  250. @param bottom: The bottom edge
  251. """
  252. self.spatial_extent.set_spatial_extent(north, south, east, west, top, bottom)
  253. def check_valid_time(self):
  254. """!Check for correct valid time"""
  255. if self.is_time_absolute():
  256. start, end, tz = self.get_absolute_time()
  257. else:
  258. start, end, unit = self.get_relative_time()
  259. if start != None:
  260. if end != None:
  261. if start >= end:
  262. if self.get_layer():
  263. core.error(_("Map <%s> with layer %s has incorrect time interval, start time is greater than end time") % (self.get_map_id(), self.get_layer()))
  264. else:
  265. core.error(_("Map <%s> has incorrect time interval, start time is greater than end time") % (self.get_map_id()))
  266. return False
  267. else:
  268. core.error(_("Map <%s> has incorrect start time") % (self.get_map_id()))
  269. return False
  270. return True
  271. def delete(self, dbif=None, update=True, execute = True):
  272. """!Delete a map entry from database if it exists
  273. Remove dependent entries:
  274. * Remove the map entry in each space time dataset in which this map is registered
  275. * Remove the space time dataset register table
  276. @param dbif: The database interface to be used
  277. @param update: Call for each unregister statement the update from registered maps
  278. of the space time dataset. This can slow down the un-registration process significantly.
  279. @param execute: If True the SQL DELETE and DROP table statements will be executed.
  280. If False the prepared SQL statements are returned and must be executed by the caller.
  281. @return The SQL statements if execute == False, else an empty string, None in case of a failure
  282. """
  283. dbif, connect = init_dbif(dbif)
  284. statement = ""
  285. if self.is_in_db(dbif):
  286. # SELECT all needed information from the database
  287. self.metadata.select(dbif)
  288. # First we unregister from all dependent space time datasets
  289. statement += self.unregister(dbif=dbif, update=update, execute=False)
  290. # Remove the strds register table
  291. if self.get_stds_register():
  292. statement += "DROP TABLE " + self.get_stds_register() + ";\n"
  293. core.verbose(_("Delete %s dataset <%s> from temporal database") % (self.get_type(), self.get_id()))
  294. # Delete yourself from the database, trigger functions will take care of dependencies
  295. statement += self.base.get_delete_statement()
  296. if execute == True:
  297. execute_transaction(statement, dbif)
  298. # Remove the timestamp from the file system
  299. if self.get_type() == "vect":
  300. if self.get_layer():
  301. core.run_command(self.get_timestamp_module_name(), map=self.get_map_id(), layer=self.get_layer(), date="none")
  302. else:
  303. core.run_command(self.get_timestamp_module_name(), map=self.get_map_id(), date="none")
  304. else:
  305. core.run_command(self.get_timestamp_module_name(), map=self.get_map_id(), date="none")
  306. self.reset(None)
  307. if connect == True:
  308. dbif.close()
  309. if execute:
  310. return ""
  311. return statement
  312. def unregister(self, dbif=None, update=True, execute=True):
  313. """! Remove the map entry in each space time dataset in which this map is registered
  314. @param dbif: The database interface to be used
  315. @param update: Call for each unregister statement the update from registered maps
  316. of the space time dataset. This can slow down the un-registration process significantly.
  317. @param execute: If True the SQL DELETE and DROP table statements will be executed.
  318. If False the prepared SQL statements are returned and must be executed by the caller.
  319. @return The SQL statements if execute == False, else an empty string
  320. """
  321. if self.get_layer():
  322. core.verbose(_("Unregister %s map <%s> with layer %s from space time datasets") % \
  323. (self.get_type(), self.get_map_id(), self.get_layer()))
  324. else:
  325. core.verbose(_("Unregister %s map <%s> from space time datasets") % (self.get_type(), self.get_map_id()))
  326. statement = ""
  327. dbif, connect = init_dbif(dbif)
  328. # Get all datasets in which this map is registered
  329. rows = self.get_registered_datasets(dbif)
  330. # For each stds in which the map is registered
  331. if rows:
  332. count = 0
  333. num_sps = len(rows)
  334. for row in rows:
  335. core.percent(count, num_sps, 1)
  336. count += 1
  337. # Create a space time dataset object to remove the map
  338. # from its register
  339. stds = self.get_new_stds_instance(row["id"])
  340. stds.metadata.select(dbif)
  341. statement += stds.unregister_map(self, dbif, False)
  342. # Take care to update the space time dataset after
  343. # the map has been unregistered
  344. if update == True and execute == True:
  345. stds.update_from_registered_maps(dbif)
  346. core.percent(1, 1, 1)
  347. if execute == True:
  348. execute_transaction(statement, dbif)
  349. if connect == True:
  350. dbif.close()
  351. if execute:
  352. return ""
  353. return statement
  354. def get_registered_datasets(self, dbif=None):
  355. """!Return all space time dataset ids in which this map is registered as
  356. dictionary like rows with column "id" or None if this map is not registered in any
  357. space time dataset.
  358. @param dbif: The database interface to be used
  359. """
  360. dbif, connect = init_dbif(dbif)
  361. rows = None
  362. try:
  363. if self.get_stds_register() != None:
  364. # Select all stds tables in which this map is registered
  365. sql = "SELECT id FROM " + self.get_stds_register()
  366. dbif.cursor.execute(sql)
  367. rows = dbif.cursor.fetchall()
  368. except:
  369. core.error(_("Unable to select space time dataset register table <%s>") % (self.get_stds_register()))
  370. if connect == True:
  371. dbif.close()
  372. return rows