abstract_dataset.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. >>> ad = AbstractDataset()
  9. >>> ad.reset(ident="soil@PERMANENT")
  10. Traceback (most recent call last):
  11. File "/usr/lib/python2.7/doctest.py", line 1289, in __run
  12. compileflags, 1) in test.globs
  13. File "<doctest __main__[2]>", line 1, in <module>
  14. ad.reset(ident="soil@PERMANENT")
  15. File "AbstractDataset.py", line 53, in reset
  16. raise ImplementationError("This method must be implemented in the subclasses")
  17. ImplementationError: 'This method must be implemented in the subclasses'
  18. @endcode
  19. (C) 2011-2012 by the GRASS Development Team
  20. This program is free software under the GNU General Public
  21. License (>=v2). Read the file COPYING that comes with GRASS
  22. for details.
  23. @author Soeren Gebbert
  24. """
  25. import uuid
  26. import copy
  27. from temporal_extent import *
  28. from spatial_extent import *
  29. from metadata import *
  30. class ImplementationError(Exception):
  31. """!Exception raised for the calling of methods that should be implemented in
  32. sub classes.
  33. """
  34. def __init__(self, msg):
  35. self.msg = msg
  36. def __str__(self):
  37. return repr(self.msg)
  38. ###############################################################################
  39. class AbstractDataset(object):
  40. """!This is the base class for all datasets
  41. (raster, vector, raster3d, strds, stvds, str3ds)"""
  42. def __init__(self):
  43. pass
  44. def reset(self, ident):
  45. """!Reset the internal structure and set the identifier
  46. @param ident: The identifier of the dataset
  47. """
  48. raise ImplementationError("This method must be implemented in the subclasses")
  49. def get_type(self):
  50. """!Return the type of this class"""
  51. raise ImplementationError("This method must be implemented in the subclasses")
  52. def get_new_instance(self, ident):
  53. """!Return a new instance with the type of this class
  54. @param ident: The identifier of the dataset
  55. """
  56. raise ImplementationError("This method must be implemented in the subclasses")
  57. def spatial_overlapping(self, dataset):
  58. """!Return True if the spatial extents are overlapping"""
  59. raise ImplementationError("This method must be implemented in the subclasses")
  60. def spatial_relation(self, dataset):
  61. """Return the spatial relationship between self and dataset"""
  62. raise ImplementationError("This method must be implemented in the subclasses")
  63. def print_info(self):
  64. """!Print information about this class in human readable style"""
  65. raise ImplementationError("This method must be implemented in the subclasses")
  66. def print_shell_info(self):
  67. """!Print information about this class in shell style"""
  68. raise ImplementationError("This method must be implemented in the subclasses")
  69. def print_self(self):
  70. """!Print the content of the internal structure to stdout"""
  71. raise ImplementationError("This method must be implemented in the subclasses")
  72. def set_id(self, ident):
  73. self.base.set_id(ident)
  74. if self.is_time_absolute():
  75. self.absolute_time.set_id(ident)
  76. if self.is_time_relative():
  77. self.relative_time.set_id(ident)
  78. self.spatial_extent.set_id(ident)
  79. self.metadata.set_id(ident)
  80. def get_id(self):
  81. """!Return the unique identifier of the dataset"""
  82. return self.base.get_id()
  83. def get_name(self):
  84. """!Return the name"""
  85. return self.base.get_name()
  86. def get_mapset(self):
  87. """!Return the mapset"""
  88. return self.base.get_mapset()
  89. def get_valid_time(self):
  90. """!Returns a tuple of the valid start and end time
  91. Start and end time can be either of type datetime or of type double
  92. depending on the temporal type
  93. @return A tuple of (start_time, end_time)
  94. """
  95. start = None
  96. end = None
  97. if self.is_time_absolute():
  98. start = self.absolute_time.get_start_time()
  99. end = self.absolute_time.get_end_time()
  100. if self.is_time_relative():
  101. start = self.relative_time.get_start_time()
  102. end = self.relative_time.get_end_time()
  103. return (start, end)
  104. def get_absolute_time(self):
  105. """!Returns a tuple of the start, the end
  106. valid time and the timezone of the map
  107. @return A tuple of (start_time, end_time, timezone)
  108. """
  109. start = self.absolute_time.get_start_time()
  110. end = self.absolute_time.get_end_time()
  111. tz = self.absolute_time.get_timezone()
  112. return (start, end, tz)
  113. def get_relative_time(self):
  114. """!Returns the valid relative time interval (start_time, end_time, unit)
  115. or None if not present"""
  116. start = self.relative_time.get_start_time()
  117. end = self.relative_time.get_end_time()
  118. unit = self.relative_time.get_unit()
  119. return (start, end, unit)
  120. def get_relative_time_unit(self):
  121. """!Returns the relative time unit or None if not present"""
  122. unit = self.relative_time.get_unit()
  123. return unit
  124. def check_relative_time_unit(self, unit):
  125. """!Check if unit is of type years, months, days, hours,
  126. minutes or seconds
  127. Return True if success or False otherwise
  128. """
  129. # Check unit
  130. units = ["years", "months", "days", "hours", "minutes", "seconds"]
  131. if unit not in units:
  132. return False
  133. return True
  134. def get_temporal_type(self):
  135. """!Return the temporal type of this dataset"""
  136. return self.base.get_ttype()
  137. def get_spatial_extent(self):
  138. """!Return a tuple of spatial extent
  139. (north, south, east, west, top, bottom) """
  140. return self.spatial_extent.get_spatial_extent()
  141. def select(self, dbif=None):
  142. """!Select temporal dataset entry from database and fill
  143. up the internal structure"""
  144. dbif, connect = init_dbif(dbif)
  145. self.base.select(dbif)
  146. if self.is_time_absolute():
  147. self.absolute_time.select(dbif)
  148. if self.is_time_relative():
  149. self.relative_time.select(dbif)
  150. self.spatial_extent.select(dbif)
  151. self.metadata.select(dbif)
  152. if connect:
  153. dbif.close()
  154. def is_in_db(self, dbif=None):
  155. """!Check if the temporal dataset entry is in the database
  156. @param dbif: The database interface to be used
  157. """
  158. return self.base.is_in_db(dbif)
  159. def delete(self):
  160. """!Delete temporal dataset entry from database if it exists"""
  161. raise ImplementationError("This method must be implemented in the subclasses")
  162. def insert(self, dbif=None, execute=True):
  163. """!Insert temporal dataset entry into
  164. database from the internal structure
  165. @param dbif: The database interface to be used
  166. @param execute: If True the SQL statements will be executed.
  167. If False the prepared SQL statements are returned
  168. and must be executed by the caller.
  169. """
  170. dbif, connect = init_dbif(dbif)
  171. # Build the INSERT SQL statement
  172. statement = self.base.get_insert_statement_mogrified(dbif)
  173. if self.is_time_absolute():
  174. statement += self.absolute_time.get_insert_statement_mogrified(
  175. dbif)
  176. if self.is_time_relative():
  177. statement += self.relative_time.get_insert_statement_mogrified(
  178. dbif)
  179. statement += self.spatial_extent.get_insert_statement_mogrified(dbif)
  180. statement += self.metadata.get_insert_statement_mogrified(dbif)
  181. if execute:
  182. dbif.execute_transaction(statement)
  183. if connect:
  184. dbif.close()
  185. return ""
  186. if connect:
  187. dbif.close()
  188. return statement
  189. def update(self, dbif=None, execute=True, ident=None):
  190. """!Update temporal dataset entry of database from the internal structure
  191. excluding None variables
  192. @param dbif: The database interface to be used
  193. @param execute: If True the SQL statements will be executed.
  194. If False the prepared SQL statements are returned
  195. and must be executed by the caller.
  196. @param ident: The identifier to be updated, useful for renaming
  197. """
  198. dbif, connect = init_dbif(dbif)
  199. # Build the UPDATE SQL statement
  200. statement = self.base.get_update_statement_mogrified(dbif, ident)
  201. if self.is_time_absolute():
  202. statement += self.absolute_time.get_update_statement_mogrified(
  203. dbif, ident)
  204. if self.is_time_relative():
  205. statement += self.relative_time.get_update_statement_mogrified(
  206. dbif, ident)
  207. statement += self.spatial_extent.get_update_statement_mogrified(dbif,
  208. ident)
  209. statement += self.metadata.get_update_statement_mogrified(dbif, ident)
  210. if execute:
  211. dbif.execute_transaction(statement)
  212. if connect:
  213. dbif.close()
  214. return ""
  215. if connect:
  216. dbif.close()
  217. return statement
  218. def update_all(self, dbif=None, execute=True, ident=None):
  219. """!Update temporal dataset entry of database from the internal structure
  220. and include None variables.
  221. @param dbif: The database interface to be used
  222. @param execute: If True the SQL statements will be executed.
  223. If False the prepared SQL statements are returned
  224. and must be executed by the caller.
  225. @param ident: The identifier to be updated, useful for renaming
  226. """
  227. dbif, connect = init_dbif(dbif)
  228. # Build the UPDATE SQL statement
  229. statement = self.base.get_update_all_statement_mogrified(dbif, ident)
  230. if self.is_time_absolute():
  231. statement += self.absolute_time.get_update_all_statement_mogrified(
  232. dbif, ident)
  233. if self.is_time_relative():
  234. statement += self.relative_time.get_update_all_statement_mogrified(
  235. dbif, ident)
  236. statement += self.spatial_extent.get_update_all_statement_mogrified(
  237. dbif, ident)
  238. statement += self.metadata.get_update_all_statement_mogrified(dbif, ident)
  239. if execute:
  240. dbif.execute_transaction(statement)
  241. if connect:
  242. dbif.close()
  243. return ""
  244. if connect:
  245. dbif.close()
  246. return statement
  247. def set_time_to_absolute(self):
  248. """!Set the temporal type to absolute"""
  249. self.base.set_ttype("absolute")
  250. def set_time_to_relative(self):
  251. """!Set the temporal type to relative"""
  252. self.base.set_ttype("relative")
  253. def is_time_absolute(self):
  254. """!Return True in case the temporal type is absolute
  255. @return True if temporal type is absolute, False otherwise
  256. """
  257. if "temporal_type" in self.base.D:
  258. return self.base.get_ttype() == "absolute"
  259. else:
  260. return None
  261. def is_time_relative(self):
  262. """!Return True in case the temporal type is relative
  263. @return True if temporal type is relative, False otherwise
  264. """
  265. if "temporal_type" in self.base.D:
  266. return self.base.get_ttype() == "relative"
  267. else:
  268. return None
  269. def temporal_relation(self, map):
  270. """!Return the temporal relation of this and the provided temporal map"""
  271. if self.is_time_absolute() and map.is_time_absolute():
  272. return self.absolute_time.temporal_relation(map.absolute_time)
  273. if self.is_time_relative() and map.is_time_relative():
  274. return self.relative_time.temporal_relation(map.relative_time)
  275. return None
  276. ###############################################################################
  277. class AbstractDatasetComparisonKeyStartTime(object):
  278. """!This comparison key can be used to sort lists of abstract datasets
  279. by start time
  280. Example:
  281. # Return all maps in a space time raster dataset as map objects
  282. map_list = strds.get_registered_maps_as_objects()
  283. # Sort the maps in the list by start time
  284. sorted_map_list = sorted(
  285. map_list, key=AbstractDatasetComparisonKeyStartTime)
  286. """
  287. def __init__(self, obj, *args):
  288. self.obj = obj
  289. def __lt__(self, other):
  290. startA, endA = self.obj.get_valid_time()
  291. startB, endB = other.obj.get_valid_time()
  292. return startA < startB
  293. def __gt__(self, other):
  294. startA, endA = self.obj.get_valid_time()
  295. startB, endB = other.obj.get_valid_time()
  296. return startA > startB
  297. def __eq__(self, other):
  298. startA, endA = self.obj.get_valid_time()
  299. startB, endB = other.obj.get_valid_time()
  300. return startA == startB
  301. def __le__(self, other):
  302. startA, endA = self.obj.get_valid_time()
  303. startB, endB = other.obj.get_valid_time()
  304. return startA <= startB
  305. def __ge__(self, other):
  306. startA, endA = self.obj.get_valid_time()
  307. startB, endB = other.obj.get_valid_time()
  308. return startA >= startB
  309. def __ne__(self, other):
  310. startA, endA = self.obj.get_valid_time()
  311. startB, endB = other.obj.get_valid_time()
  312. return startA != startB
  313. ###############################################################################
  314. class AbstractDatasetComparisonKeyEndTime(object):
  315. """!This comparison key can be used to sort lists of abstract datasets
  316. by end time
  317. Example:
  318. # Return all maps in a space time raster dataset as map objects
  319. map_list = strds.get_registered_maps_as_objects()
  320. # Sort the maps in the list by end time
  321. sorted_map_list = sorted(
  322. map_list, key=AbstractDatasetComparisonKeyEndTime)
  323. """
  324. def __init__(self, obj, *args):
  325. self.obj = obj
  326. def __lt__(self, other):
  327. startA, endA = self.obj.get_valid_time()
  328. startB, endB = other.obj.get_valid_time()
  329. return endA < endB
  330. def __gt__(self, other):
  331. startA, endA = self.obj.get_valid_time()
  332. startB, endB = other.obj.get_valid_time()
  333. return endA > endB
  334. def __eq__(self, other):
  335. startA, endA = self.obj.get_valid_time()
  336. startB, endB = other.obj.get_valid_time()
  337. return endA == endB
  338. def __le__(self, other):
  339. startA, endA = self.obj.get_valid_time()
  340. startB, endB = other.obj.get_valid_time()
  341. return endA <= endB
  342. def __ge__(self, other):
  343. startA, endA = self.obj.get_valid_time()
  344. startB, endB = other.obj.get_valid_time()
  345. return endA >= endB
  346. def __ne__(self, other):
  347. startA, endA = self.obj.get_valid_time()
  348. startB, endB = other.obj.get_valid_time()
  349. return endA != endB
  350. ###############################################################################
  351. if __name__ == "__main__":
  352. import doctest
  353. doctest.testmod()