abstract_dataset.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. import uuid
  17. import copy
  18. from temporal_extent import *
  19. from spatial_extent import *
  20. from metadata import *
  21. class abstract_dataset(object):
  22. """This is the base class for all datasets (raster, vector, raster3d, strds, stvds, str3ds)"""
  23. def reset(self, ident):
  24. """Reset the internal structure and set the identifier
  25. @param ident: The identifier of the dataset
  26. """
  27. raise IOError("This method must be implemented in the subclasses")
  28. def get_type(self):
  29. """Return the type of this class"""
  30. raise IOError("This method must be implemented in the subclasses")
  31. def get_new_instance(self, ident):
  32. """Return a new instance with the type of this class
  33. @param ident: The identifier of the dataset
  34. """
  35. raise IOError("This method must be implemented in the subclasses")
  36. def get_id(self):
  37. return self.base.get_id()
  38. def get_valid_time(self):
  39. """Returns a tuple of the start, the end valid time, this can be either datetime or double values
  40. @return A tuple of (start_time, end_time)
  41. """
  42. start = None
  43. end = None
  44. if self.is_time_absolute():
  45. start = self.absolute_time.get_start_time()
  46. end = self.absolute_time.get_end_time()
  47. if self.is_time_relative():
  48. start = self.relative_time.get_start_time()
  49. end = self.relative_time.get_end_time()
  50. return (start, end)
  51. def get_absolute_time(self):
  52. """Returns a tuple of the start, the end valid time and the timezone of the map
  53. @return A tuple of (start_time, end_time, timezone)
  54. """
  55. start = self.absolute_time.get_start_time()
  56. end = self.absolute_time.get_end_time()
  57. tz = self.absolute_time.get_timezone()
  58. return (start, end, tz)
  59. def get_relative_time(self):
  60. """Returns the relative time interval (start_time, end_time) or None if not present"""
  61. start = self.relative_time.get_start_time()
  62. end = self.relative_time.get_end_time()
  63. return (start, end)
  64. def get_temporal_type(self):
  65. """Return the temporal type of this dataset"""
  66. return self.base.get_ttype()
  67. def get_spatial_extent(self):
  68. """Return a tuple of spatial extent (north, south, east, west, top, bottom) """
  69. north = self.spatial_extent.get_north()
  70. south = self.spatial_extent.get_south()
  71. east = self.spatial_extent.get_east()
  72. west = self.spatial_extent.get_west()
  73. top = self.spatial_extent.get_top()
  74. bottom = self.spatial_extent.get_bottom()
  75. return (north, south, east, west, top, bottom)
  76. def spatial_overlap(self, dataset):
  77. """Return True if the spatial extents overlap"""
  78. north = self.spatial_extent.overlap(dataset.spatial_extent)
  79. def select(self, dbif=None):
  80. """Select temporal dataset entry from database and fill up the internal structure"""
  81. connect = False
  82. if dbif == None:
  83. dbif = sql_database_interface()
  84. dbif.connect()
  85. connect = True
  86. dbif.cursor.execute("BEGIN TRANSACTION")
  87. self.base.select(dbif)
  88. if self.is_time_absolute():
  89. self.absolute_time.select(dbif)
  90. if self.is_time_relative():
  91. self.relative_time.select(dbif)
  92. self.spatial_extent.select(dbif)
  93. self.metadata.select(dbif)
  94. dbif.cursor.execute("COMMIT TRANSACTION")
  95. if connect:
  96. dbif.close()
  97. def is_in_db(self, dbif=None):
  98. """Check if the temporal dataset entry is in the database"""
  99. return self.base.is_in_db(dbif)
  100. def delete(self):
  101. """Delete temporal dataset entry from database if it exists"""
  102. raise IOError("This method must be implemented in the subclasses")
  103. def insert(self, dbif=None):
  104. """Insert temporal dataset entry into database from the internal structure"""
  105. connect = False
  106. if dbif == None:
  107. dbif = sql_database_interface()
  108. dbif.connect()
  109. connect = True
  110. dbif.cursor.execute("BEGIN TRANSACTION")
  111. self.base.insert(dbif)
  112. if self.is_time_absolute():
  113. self.absolute_time.insert(dbif)
  114. if self.is_time_relative():
  115. self.relative_time.insert(dbif)
  116. self.spatial_extent.insert(dbif)
  117. self.metadata.insert(dbif)
  118. dbif.cursor.execute("COMMIT TRANSACTION")
  119. if connect:
  120. dbif.close()
  121. def update(self, dbif=None):
  122. """Update temporal dataset entry of database from the internal structure
  123. excluding None variables
  124. """
  125. connect = False
  126. if dbif == None:
  127. dbif = sql_database_interface()
  128. dbif.connect()
  129. connect = True
  130. dbif.cursor.execute("BEGIN TRANSACTION")
  131. self.base.update(dbif)
  132. if self.is_time_absolute():
  133. self.absolute_time.update(dbif)
  134. if self.is_time_relative():
  135. self.relative_time.update(dbif)
  136. self.spatial_extent.update(dbif)
  137. self.metadata.update(dbif)
  138. dbif.cursor.execute("COMMIT TRANSACTION")
  139. if connect:
  140. dbif.close()
  141. def update_all(self, dbif=None):
  142. """Update temporal dataset entry of database from the internal structure
  143. and include None varuables.
  144. @param dbif: The database interface to be used
  145. """
  146. connect = False
  147. if dbif == None:
  148. dbif = sql_database_interface()
  149. dbif.connect()
  150. connect = True
  151. dbif.cursor.execute("BEGIN TRANSACTION")
  152. self.base.update_all(dbif)
  153. if self.is_time_absolute():
  154. self.absolute_time.update_all(dbif)
  155. if self.is_time_relative():
  156. self.relative_time.update_all(dbif)
  157. self.spatial_extent.update_all(dbif)
  158. self.metadata.update_all(dbif)
  159. dbif.cursor.execute("COMMIT TRANSACTION")
  160. if connect:
  161. dbif.close()
  162. def print_self(self):
  163. """Print the content of the internal structure to stdout"""
  164. self.base.print_self()
  165. if self.is_time_absolute():
  166. self.absolute_time.print_self()
  167. if self.is_time_relative():
  168. self.relative_time.print_self()
  169. self.spatial_extent.print_self()
  170. self.metadata.print_self()
  171. def print_info(self):
  172. """Print information about this class in human readable style"""
  173. if self.get_type() == "raster":
  174. # 1 2 3 4 5 6 7
  175. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  176. print ""
  177. print " +-------------------- Raster Dataset ----------------------------------------+"
  178. if self.get_type() == "raster3d":
  179. # 1 2 3 4 5 6 7
  180. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  181. print ""
  182. print " +-------------------- Raster3d Dataset --------------------------------------+"
  183. if self.get_type() == "vector":
  184. # 1 2 3 4 5 6 7
  185. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  186. print ""
  187. print " +-------------------- Vector Dataset ----------------------------------------+"
  188. if self.get_type() == "strds":
  189. # 1 2 3 4 5 6 7
  190. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  191. print ""
  192. print " +-------------------- Space Time Raster Dataset -----------------------------+"
  193. if self.get_type() == "str3ds":
  194. # 1 2 3 4 5 6 7
  195. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  196. print ""
  197. print " +-------------------- Space Time Raster3d Dataset ---------------------------+"
  198. if self.get_type() == "stvds":
  199. # 1 2 3 4 5 6 7
  200. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  201. print ""
  202. print " +-------------------- Space Time Vector Dataset -----------------------------+"
  203. print " | |"
  204. self.base.print_info()
  205. if self.is_time_absolute():
  206. self.absolute_time.print_info()
  207. if self.is_time_relative():
  208. self.relative_time.print_info()
  209. self.spatial_extent.print_info()
  210. self.metadata.print_info()
  211. print " +----------------------------------------------------------------------------+"
  212. def print_shell_info(self):
  213. """Print information about this class in shell style"""
  214. self.base.print_shell_info()
  215. if self.is_time_absolute():
  216. self.absolute_time.print_shell_info()
  217. if self.is_time_relative():
  218. self.relative_time.print_shell_info()
  219. self.spatial_extent.print_shell_info()
  220. self.metadata.print_shell_info()
  221. def set_time_to_absolute(self):
  222. self.base.set_ttype("absolute")
  223. def set_time_to_relative(self):
  224. self.base.set_ttype("relative")
  225. def is_time_absolute(self):
  226. if self.base.D.has_key("temporal_type"):
  227. return self.base.get_ttype() == "absolute"
  228. else:
  229. return None
  230. def is_time_relative(self):
  231. if self.base.D.has_key("temporal_type"):
  232. return self.base.get_ttype() == "relative"
  233. else:
  234. return None
  235. def temporal_relation(self, map):
  236. """Return the temporal relation of this and the provided temporal map"""
  237. if self.is_time_absolute() and map.is_time_absolute():
  238. return self.absolute_time.temporal_relation(map.absolute_time)
  239. if self.is_time_relative() and map.is_time_relative():
  240. return self.relative_time.temporal_relation(map.relative_time)
  241. return None