abstract_dataset.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. class AbstractDataset(object):
  39. """!This is the base class for all datasets
  40. (raster, vector, raster3d, strds, stvds, str3ds)"""
  41. def reset(self, ident):
  42. """!Reset the internal structure and set the identifier
  43. @param ident: The identifier of the dataset
  44. """
  45. raise ImplementationError("This method must be implemented in the subclasses")
  46. def get_type(self):
  47. """!Return the type of this class"""
  48. raise ImplementationError("This method must be implemented in the subclasses")
  49. def get_new_instance(self, ident):
  50. """!Return a new instance with the type of this class
  51. @param ident: The identifier of the dataset
  52. """
  53. raise ImplementationError("This method must be implemented in the subclasses")
  54. def spatial_overlapping(self, dataset):
  55. """!Return True if the spatial extents are overlapping"""
  56. raise ImplementationError("This method must be implemented in the subclasses")
  57. def spatial_relation(self, dataset):
  58. """Return the spatial relationship between self and dataset"""
  59. raise ImplementationError("This method must be implemented in the subclasses")
  60. def print_info(self):
  61. """!Print information about this class in human readable style"""
  62. raise ImplementationError("This method must be implemented in the subclasses")
  63. def print_shell_info(self):
  64. """!Print information about this class in shell style"""
  65. raise ImplementationError("This method must be implemented in the subclasses")
  66. def print_self(self):
  67. """!Print the content of the internal structure to stdout"""
  68. raise ImplementationError("This method must be implemented in the subclasses")
  69. def set_id(self, ident):
  70. self.base.set_id(ident)
  71. if self.is_time_absolute():
  72. self.absolute_time.set_id(ident)
  73. if self.is_time_relative():
  74. self.relative_time.set_id(ident)
  75. self.spatial_extent.set_id(ident)
  76. self.metadata.set_id(ident)
  77. def get_id(self):
  78. """!Return the unique identifier of the dataset"""
  79. return self.base.get_id()
  80. def get_name(self):
  81. """!Return the name"""
  82. return self.base.get_name()
  83. def get_mapset(self):
  84. """!Return the mapset"""
  85. return self.base.get_mapset()
  86. def get_valid_time(self):
  87. """!Returns a tuple of the start, the end valid time,
  88. this can be either datetime or double values
  89. @return A tuple of (start_time, end_time)
  90. """
  91. start = None
  92. end = None
  93. if self.is_time_absolute():
  94. start = self.absolute_time.get_start_time()
  95. end = self.absolute_time.get_end_time()
  96. if self.is_time_relative():
  97. start = self.relative_time.get_start_time()
  98. end = self.relative_time.get_end_time()
  99. return (start, end)
  100. def get_absolute_time(self):
  101. """!Returns a tuple of the start, the end
  102. valid time and the timezone of the map
  103. @return A tuple of (start_time, end_time, timezone)
  104. """
  105. start = self.absolute_time.get_start_time()
  106. end = self.absolute_time.get_end_time()
  107. tz = self.absolute_time.get_timezone()
  108. return (start, end, tz)
  109. def get_relative_time(self):
  110. """!Returns the relative time interval (start_time, end_time, unit)
  111. or None if not present"""
  112. start = self.relative_time.get_start_time()
  113. end = self.relative_time.get_end_time()
  114. unit = self.relative_time.get_unit()
  115. return (start, end, unit)
  116. def get_relative_time_unit(self):
  117. """!Returns the relative time unit or None if not present"""
  118. unit = self.relative_time.get_unit()
  119. return unit
  120. def check_relative_time_unit(self, unit):
  121. """!Check if unit is of type years, months, days, hours,
  122. minutes or seconds
  123. Return True if success or False otherwise
  124. """
  125. # Check unit
  126. units = ["years", "months", "days", "hours", "minutes", "seconds"]
  127. if unit not in units:
  128. return False
  129. return True
  130. def get_temporal_type(self):
  131. """!Return the temporal type of this dataset"""
  132. return self.base.get_ttype()
  133. def get_spatial_extent(self):
  134. """!Return a tuple of spatial extent
  135. (north, south, east, west, top, bottom) """
  136. return self.spatial_extent.get_spatial_extent()
  137. def select(self, dbif=None):
  138. """!Select temporal dataset entry from database and fill
  139. up the internal structure"""
  140. dbif, connect = init_dbif(dbif)
  141. self.base.select(dbif)
  142. if self.is_time_absolute():
  143. self.absolute_time.select(dbif)
  144. if self.is_time_relative():
  145. self.relative_time.select(dbif)
  146. self.spatial_extent.select(dbif)
  147. self.metadata.select(dbif)
  148. if connect:
  149. dbif.close()
  150. def is_in_db(self, dbif=None):
  151. """!Check if the temporal dataset entry is in the database
  152. @param dbif: The database interface to be used
  153. """
  154. return self.base.is_in_db(dbif)
  155. def delete(self):
  156. """!Delete temporal dataset entry from database if it exists"""
  157. raise ImplementationError("This method must be implemented in the subclasses")
  158. def insert(self, dbif=None, execute=True):
  159. """!Insert temporal dataset entry into
  160. database from the internal structure
  161. @param dbif: The database interface to be used
  162. @param execute: If True the SQL statements will be executed.
  163. If False the prepared SQL statements are returned
  164. and must be executed by the caller.
  165. """
  166. dbif, connect = init_dbif(dbif)
  167. # Build the INSERT SQL statement
  168. statement = self.base.get_insert_statement_mogrified(dbif)
  169. if self.is_time_absolute():
  170. statement += self.absolute_time.get_insert_statement_mogrified(
  171. dbif)
  172. if self.is_time_relative():
  173. statement += self.relative_time.get_insert_statement_mogrified(
  174. dbif)
  175. statement += self.spatial_extent.get_insert_statement_mogrified(dbif)
  176. statement += self.metadata.get_insert_statement_mogrified(dbif)
  177. if execute:
  178. dbif.execute_transaction(statement)
  179. if connect:
  180. dbif.close()
  181. return ""
  182. if connect:
  183. dbif.close()
  184. return statement
  185. def update(self, dbif=None, execute=True):
  186. """!Update temporal dataset entry of database from the internal structure
  187. excluding None variables
  188. @param dbif: The database interface to be used
  189. @param execute: If True the SQL statements will be executed.
  190. If False the prepared SQL statements are returned
  191. and must be executed by the caller.
  192. """
  193. dbif, connect = init_dbif(dbif)
  194. # Build the UPDATE SQL statement
  195. statement = self.base.get_update_statement_mogrified(dbif)
  196. if self.is_time_absolute():
  197. statement += self.absolute_time.get_update_statement_mogrified(
  198. dbif)
  199. if self.is_time_relative():
  200. statement += self.relative_time.get_update_statement_mogrified(
  201. dbif)
  202. statement += self.spatial_extent.get_update_statement_mogrified(dbif)
  203. statement += self.metadata.get_update_statement_mogrified(dbif)
  204. if execute:
  205. dbif.execute_transaction(statement)
  206. if connect:
  207. dbif.close()
  208. return ""
  209. if connect:
  210. dbif.close()
  211. return statement
  212. def update_all(self, dbif=None, execute=True):
  213. """!Update temporal dataset entry of database from the internal structure
  214. and include None variables.
  215. @param dbif: The database interface to be used
  216. @param execute: If True the SQL statements will be executed.
  217. If False the prepared SQL statements are returned
  218. and must be executed by the caller.
  219. """
  220. dbif, connect = init_dbif(dbif)
  221. # Build the UPDATE SQL statement
  222. statement = self.base.get_update_all_statement_mogrified(dbif)
  223. if self.is_time_absolute():
  224. statement += self.absolute_time.get_update_all_statement_mogrified(
  225. dbif)
  226. if self.is_time_relative():
  227. statement += self.relative_time.get_update_all_statement_mogrified(
  228. dbif)
  229. statement += self.spatial_extent.get_update_all_statement_mogrified(
  230. dbif)
  231. statement += self.metadata.get_update_all_statement_mogrified(dbif)
  232. if execute:
  233. dbif.execute_transaction(statement)
  234. if connect:
  235. dbif.close()
  236. return ""
  237. if connect:
  238. dbif.close()
  239. return statement
  240. def set_time_to_absolute(self):
  241. self.base.set_ttype("absolute")
  242. def set_time_to_relative(self):
  243. self.base.set_ttype("relative")
  244. def is_time_absolute(self):
  245. if "temporal_type" in self.base.D:
  246. return self.base.get_ttype() == "absolute"
  247. else:
  248. return None
  249. def is_time_relative(self):
  250. if "temporal_type" in self.base.D:
  251. return self.base.get_ttype() == "relative"
  252. else:
  253. return None
  254. def temporal_relation(self, map):
  255. """!Return the temporal relation of this and the provided temporal map"""
  256. if self.is_time_absolute() and map.is_time_absolute():
  257. return self.absolute_time.temporal_relation(map.absolute_time)
  258. if self.is_time_relative() and map.is_time_relative():
  259. return self.relative_time.temporal_relation(map.relative_time)
  260. return None
  261. ###############################################################################
  262. if __name__ == "__main__":
  263. import doctest
  264. doctest.testmod()