abstract_dataset.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 print_info(self):
  37. """Print information about this class in human readable style"""
  38. raise IOError("This method must be implemented in the subclasses")
  39. def print_shell_info(self):
  40. """Print information about this class in shell style"""
  41. raise IOError("This method must be implemented in the subclasses")
  42. def print_self(self):
  43. """Print the content of the internal structure to stdout"""
  44. self.base.print_self()
  45. if self.is_time_absolute():
  46. self.absolute_time.print_self()
  47. if self.is_time_relative():
  48. self.relative_time.print_self()
  49. self.spatial_extent.print_self()
  50. self.metadata.print_self()
  51. def get_id(self):
  52. return self.base.get_id()
  53. def get_valid_time(self):
  54. """Returns a tuple of the start, the end valid time, this can be either datetime or double values
  55. @return A tuple of (start_time, end_time)
  56. """
  57. start = None
  58. end = None
  59. if self.is_time_absolute():
  60. start = self.absolute_time.get_start_time()
  61. end = self.absolute_time.get_end_time()
  62. if self.is_time_relative():
  63. start = self.relative_time.get_start_time()
  64. end = self.relative_time.get_end_time()
  65. return (start, end)
  66. def get_absolute_time(self):
  67. """Returns a tuple of the start, the end valid time and the timezone of the map
  68. @return A tuple of (start_time, end_time, timezone)
  69. """
  70. start = self.absolute_time.get_start_time()
  71. end = self.absolute_time.get_end_time()
  72. tz = self.absolute_time.get_timezone()
  73. return (start, end, tz)
  74. def get_relative_time(self):
  75. """Returns the relative time interval (start_time, end_time) or None if not present"""
  76. start = self.relative_time.get_start_time()
  77. end = self.relative_time.get_end_time()
  78. return (start, end)
  79. def get_temporal_type(self):
  80. """Return the temporal type of this dataset"""
  81. return self.base.get_ttype()
  82. def get_spatial_extent(self):
  83. """Return a tuple of spatial extent (north, south, east, west, top, bottom) """
  84. north = self.spatial_extent.get_north()
  85. south = self.spatial_extent.get_south()
  86. east = self.spatial_extent.get_east()
  87. west = self.spatial_extent.get_west()
  88. top = self.spatial_extent.get_top()
  89. bottom = self.spatial_extent.get_bottom()
  90. return (north, south, east, west, top, bottom)
  91. def spatial_overlap(self, dataset):
  92. """Return True if the spatial extents overlap"""
  93. north = self.spatial_extent.overlap(dataset.spatial_extent)
  94. def select(self, dbif=None):
  95. """Select temporal dataset entry from database and fill up the internal structure"""
  96. connect = False
  97. if dbif == None:
  98. dbif = sql_database_interface()
  99. dbif.connect()
  100. connect = True
  101. dbif.cursor.execute("BEGIN TRANSACTION")
  102. self.base.select(dbif)
  103. if self.is_time_absolute():
  104. self.absolute_time.select(dbif)
  105. if self.is_time_relative():
  106. self.relative_time.select(dbif)
  107. self.spatial_extent.select(dbif)
  108. self.metadata.select(dbif)
  109. dbif.cursor.execute("COMMIT TRANSACTION")
  110. if connect:
  111. dbif.close()
  112. def is_in_db(self, dbif=None):
  113. """Check if the temporal dataset entry is in the database"""
  114. return self.base.is_in_db(dbif)
  115. def delete(self):
  116. """Delete temporal dataset entry from database if it exists"""
  117. raise IOError("This method must be implemented in the subclasses")
  118. def insert(self, dbif=None):
  119. """Insert temporal dataset entry into database from the internal structure"""
  120. connect = False
  121. if dbif == None:
  122. dbif = sql_database_interface()
  123. dbif.connect()
  124. connect = True
  125. dbif.cursor.execute("BEGIN TRANSACTION")
  126. self.base.insert(dbif)
  127. if self.is_time_absolute():
  128. self.absolute_time.insert(dbif)
  129. if self.is_time_relative():
  130. self.relative_time.insert(dbif)
  131. self.spatial_extent.insert(dbif)
  132. self.metadata.insert(dbif)
  133. dbif.cursor.execute("COMMIT TRANSACTION")
  134. if connect:
  135. dbif.close()
  136. def update(self, dbif=None):
  137. """Update temporal dataset entry of database from the internal structure
  138. excluding None variables
  139. """
  140. connect = False
  141. if dbif == None:
  142. dbif = sql_database_interface()
  143. dbif.connect()
  144. connect = True
  145. dbif.cursor.execute("BEGIN TRANSACTION")
  146. self.base.update(dbif)
  147. if self.is_time_absolute():
  148. self.absolute_time.update(dbif)
  149. if self.is_time_relative():
  150. self.relative_time.update(dbif)
  151. self.spatial_extent.update(dbif)
  152. self.metadata.update(dbif)
  153. dbif.cursor.execute("COMMIT TRANSACTION")
  154. if connect:
  155. dbif.close()
  156. def update_all(self, dbif=None):
  157. """Update temporal dataset entry of database from the internal structure
  158. and include None varuables.
  159. @param dbif: The database interface to be used
  160. """
  161. connect = False
  162. if dbif == None:
  163. dbif = sql_database_interface()
  164. dbif.connect()
  165. connect = True
  166. dbif.cursor.execute("BEGIN TRANSACTION")
  167. self.base.update_all(dbif)
  168. if self.is_time_absolute():
  169. self.absolute_time.update_all(dbif)
  170. if self.is_time_relative():
  171. self.relative_time.update_all(dbif)
  172. self.spatial_extent.update_all(dbif)
  173. self.metadata.update_all(dbif)
  174. dbif.cursor.execute("COMMIT TRANSACTION")
  175. if connect:
  176. dbif.close()
  177. def set_time_to_absolute(self):
  178. self.base.set_ttype("absolute")
  179. def set_time_to_relative(self):
  180. self.base.set_ttype("relative")
  181. def is_time_absolute(self):
  182. if self.base.D.has_key("temporal_type"):
  183. return self.base.get_ttype() == "absolute"
  184. else:
  185. return None
  186. def is_time_relative(self):
  187. if self.base.D.has_key("temporal_type"):
  188. return self.base.get_ttype() == "relative"
  189. else:
  190. return None
  191. def temporal_relation(self, map):
  192. """Return the temporal relation of this and the provided temporal map"""
  193. if self.is_time_absolute() and map.is_time_absolute():
  194. return self.absolute_time.temporal_relation(map.absolute_time)
  195. if self.is_time_relative() and map.is_time_relative():
  196. return self.relative_time.temporal_relation(map.relative_time)
  197. return None