abstract_dataset.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. >>> import grass.temporal as tgis
  7. >>> ad = abstract_dataset()
  8. >>> ad.reset(ident="soil@PERMANENT")
  9. Traceback (most recent call last):
  10. File "/usr/lib/python2.7/doctest.py", line 1289, in __run
  11. compileflags, 1) in test.globs
  12. File "<doctest __main__[2]>", line 1, in <module>
  13. ad.reset(ident="soil@PERMANENT")
  14. File "abstract_dataset.py", line 53, in reset
  15. raise ImplementationError("This method must be implemented in the subclasses")
  16. ImplementationError: 'This method must be implemented in the subclasses'
  17. (C) 2008-2011 by the GRASS Development Team
  18. This program is free software under the GNU General Public
  19. License (>=v2). Read the file COPYING that comes with GRASS
  20. for details.
  21. @author Soeren Gebbert
  22. """
  23. import uuid
  24. import copy
  25. from temporal_extent import *
  26. from spatial_extent import *
  27. from metadata import *
  28. class ImplementationError(Exception):
  29. """!Exception raised for the calling of methods that should be implemented in
  30. sub classes.
  31. """
  32. def __init__(self, msg):
  33. self.msg = msg
  34. def __str__(self):
  35. return repr(self.msg)
  36. class abstract_dataset(object):
  37. """!This is the base class for all datasets (raster, vector, raster3d, strds, stvds, str3ds)"""
  38. def reset(self, ident):
  39. """!Reset the internal structure and set the identifier
  40. @param ident: The identifier of the dataset
  41. """
  42. raise ImplementationError("This method must be implemented in the subclasses")
  43. def get_type(self):
  44. """!Return the type of this class"""
  45. raise ImplementationError("This method must be implemented in the subclasses")
  46. def get_new_instance(self, ident):
  47. """!Return a new instance with the type of this class
  48. @param ident: The identifier of the dataset
  49. """
  50. raise ImplementationError("This method must be implemented in the subclasses")
  51. def spatial_overlapping(self, dataset):
  52. """!Return True if the spatial extents are overlapping"""
  53. raise ImplementationError("This method must be implemented in the subclasses")
  54. def spatial_relation(self, dataset):
  55. """Return the spatial relationship between self and dataset"""
  56. raise ImplementationError("This method must be implemented in the subclasses")
  57. def print_info(self):
  58. """!Print information about this class in human readable style"""
  59. raise ImplementationError("This method must be implemented in the subclasses")
  60. def print_shell_info(self):
  61. """!Print information about this class in shell style"""
  62. raise ImplementationError("This method must be implemented in the subclasses")
  63. def print_self(self):
  64. """!Print the content of the internal structure to stdout"""
  65. self.base.print_self()
  66. if self.is_time_absolute():
  67. self.absolute_time.print_self()
  68. if self.is_time_relative():
  69. self.relative_time.print_self()
  70. self.spatial_extent.print_self()
  71. self.metadata.print_self()
  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 start, the end valid time, this can be either datetime or double values
  91. @return A tuple of (start_time, end_time)
  92. """
  93. start = None
  94. end = None
  95. if self.is_time_absolute():
  96. start = self.absolute_time.get_start_time()
  97. end = self.absolute_time.get_end_time()
  98. if self.is_time_relative():
  99. start = self.relative_time.get_start_time()
  100. end = self.relative_time.get_end_time()
  101. return (start, end)
  102. def get_absolute_time(self):
  103. """!Returns a tuple of the start, the end valid time and the timezone of the map
  104. @return A tuple of (start_time, end_time, timezone)
  105. """
  106. start = self.absolute_time.get_start_time()
  107. end = self.absolute_time.get_end_time()
  108. tz = self.absolute_time.get_timezone()
  109. return (start, end, tz)
  110. def get_relative_time(self):
  111. """!Returns the relative time interval (start_time, end_time, unit) 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, minutes or seconds
  122. Return True if success or False otherwise
  123. """
  124. # Check unit
  125. units = ["years", "months", "days", "hours", "minutes", "seconds"]
  126. if unit not in units:
  127. return False
  128. return True
  129. def get_temporal_type(self):
  130. """!Return the temporal type of this dataset"""
  131. return self.base.get_ttype()
  132. def get_spatial_extent(self):
  133. """!Return a tuple of spatial extent (north, south, east, west, top, bottom) """
  134. return self.spatial_extent.get_spatial_extent()
  135. def select(self, dbif=None):
  136. """!Select temporal dataset entry from database and fill up the internal structure"""
  137. dbif, connect = init_dbif(dbif)
  138. self.base.select(dbif)
  139. if self.is_time_absolute():
  140. self.absolute_time.select(dbif)
  141. if self.is_time_relative():
  142. self.relative_time.select(dbif)
  143. self.spatial_extent.select(dbif)
  144. self.metadata.select(dbif)
  145. if connect:
  146. dbif.close()
  147. def is_in_db(self, dbif=None):
  148. """!Check if the temporal dataset entry is in the database
  149. @param dbif: The database interface to be used
  150. """
  151. return self.base.is_in_db(dbif)
  152. def delete(self):
  153. """!Delete temporal dataset entry from database if it exists"""
  154. raise ImplementationError("This method must be implemented in the subclasses")
  155. def insert(self, dbif=None, execute=True):
  156. """!Insert temporal dataset entry into database from the internal structure
  157. @param dbif: The database interface to be used
  158. @param execute: If True the SQL statements will be executed.
  159. If False the prepared SQL statements are returned and must be executed by the caller.
  160. """
  161. dbif, connect = init_dbif(dbif)
  162. # Build the INSERT SQL statement
  163. statement = self.base.get_insert_statement_mogrified(dbif)
  164. if self.is_time_absolute():
  165. statement += self.absolute_time.get_insert_statement_mogrified(
  166. dbif)
  167. if self.is_time_relative():
  168. statement += self.relative_time.get_insert_statement_mogrified(
  169. dbif)
  170. statement += self.spatial_extent.get_insert_statement_mogrified(dbif)
  171. statement += self.metadata.get_insert_statement_mogrified(dbif)
  172. if execute == True:
  173. dbif.execute_transaction(statement)
  174. if connect:
  175. dbif.close()
  176. return ""
  177. if connect:
  178. dbif.close()
  179. return statement
  180. def update(self, dbif=None, execute=True):
  181. """!Update temporal dataset entry of database from the internal structure
  182. excluding None variables
  183. @param dbif: The database interface to be used
  184. @param execute: If True the SQL statements will be executed.
  185. If False the prepared SQL statements are returned and must be executed by the caller.
  186. """
  187. dbif, connect = init_dbif(dbif)
  188. # Build the UPDATE SQL statement
  189. statement = self.base.get_update_statement_mogrified(dbif)
  190. if self.is_time_absolute():
  191. statement += self.absolute_time.get_update_statement_mogrified(
  192. dbif)
  193. if self.is_time_relative():
  194. statement += self.relative_time.get_update_statement_mogrified(
  195. dbif)
  196. statement += self.spatial_extent.get_update_statement_mogrified(dbif)
  197. statement += self.metadata.get_update_statement_mogrified(dbif)
  198. if execute == True:
  199. dbif.execute_transaction(statement)
  200. if connect:
  201. dbif.close()
  202. return ""
  203. if connect:
  204. dbif.close()
  205. return statement
  206. def update_all(self, dbif=None, execute=True):
  207. """!Update temporal dataset entry of database from the internal structure
  208. and include None variables.
  209. @param dbif: The database interface to be used
  210. @param execute: If True the SQL statements will be executed.
  211. If False the prepared SQL statements are returned and must be executed by the caller.
  212. """
  213. dbif, connect = init_dbif(dbif)
  214. # Build the UPDATE SQL statement
  215. statement = self.base.get_update_all_statement_mogrified(dbif)
  216. if self.is_time_absolute():
  217. statement += self.absolute_time.get_update_all_statement_mogrified(
  218. dbif)
  219. if self.is_time_relative():
  220. statement += self.relative_time.get_update_all_statement_mogrified(
  221. dbif)
  222. statement += self.spatial_extent.get_update_all_statement_mogrified(
  223. dbif)
  224. statement += self.metadata.get_update_all_statement_mogrified(dbif)
  225. if execute == True:
  226. dbif.execute_transaction(statement)
  227. if connect:
  228. dbif.close()
  229. return ""
  230. if connect:
  231. dbif.close()
  232. return statement
  233. def set_time_to_absolute(self):
  234. self.base.set_ttype("absolute")
  235. def set_time_to_relative(self):
  236. self.base.set_ttype("relative")
  237. def is_time_absolute(self):
  238. if "temporal_type" in self.base.D:
  239. return self.base.get_ttype() == "absolute"
  240. else:
  241. return None
  242. def is_time_relative(self):
  243. if "temporal_type" in self.base.D:
  244. return self.base.get_ttype() == "relative"
  245. else:
  246. return None
  247. def temporal_relation(self, map):
  248. """!Return the temporal relation of this and the provided temporal map"""
  249. if self.is_time_absolute() and map.is_time_absolute():
  250. return self.absolute_time.temporal_relation(map.absolute_time)
  251. if self.is_time_relative() and map.is_time_relative():
  252. return self.relative_time.temporal_relation(map.relative_time)
  253. return None
  254. ###############################################################################
  255. if __name__ == "__main__":
  256. import doctest
  257. doctest.testmod()