abstract_dataset.py 10 KB

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