abstract_dataset.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. self.base.select(dbif)
  78. if self.is_time_absolute():
  79. self.absolute_time.select(dbif)
  80. if self.is_time_relative():
  81. self.relative_time.select(dbif)
  82. self.spatial_extent.select(dbif)
  83. self.metadata.select(dbif)
  84. def is_in_db(self, dbif=None):
  85. """Check if the temporal dataset entry is in the database"""
  86. return self.base.is_in_db(dbif)
  87. def delete(self):
  88. """Delete temporal dataset entry from database if it exists"""
  89. raise IOError("This method must be implemented in the subclasses")
  90. def insert(self, dbif=None):
  91. """Insert temporal dataset entry into database from the internal structure"""
  92. self.base.insert(dbif)
  93. if self.is_time_absolute():
  94. self.absolute_time.insert(dbif)
  95. if self.is_time_relative():
  96. self.relative_time.insert(dbif)
  97. self.spatial_extent.insert(dbif)
  98. self.metadata.insert(dbif)
  99. def update(self, dbif=None):
  100. """Update temporal dataset entry of database from the internal structure
  101. excluding None variables
  102. """
  103. self.base.update(dbif)
  104. if self.is_time_absolute():
  105. self.absolute_time.update(dbif)
  106. if self.is_time_relative():
  107. self.relative_time.update(dbif)
  108. self.spatial_extent.update(dbif)
  109. self.metadata.update(dbif)
  110. def update_all(self, dbif=None):
  111. """Update temporal dataset entry of database from the internal structure
  112. and include None varuables.
  113. @param dbif: The database interface to be used
  114. """
  115. self.base.update_all(dbif)
  116. if self.is_time_absolute():
  117. self.absolute_time.update_all(dbif)
  118. if self.is_time_relative():
  119. self.relative_time.update_all(dbif)
  120. self.spatial_extent.update_all(dbif)
  121. self.metadata.update_all(dbif)
  122. def print_self(self):
  123. """Print the content of the internal structure to stdout"""
  124. self.base.print_self()
  125. if self.is_time_absolute():
  126. self.absolute_time.print_self()
  127. if self.is_time_relative():
  128. self.relative_time.print_self()
  129. self.spatial_extent.print_self()
  130. self.metadata.print_self()
  131. def print_info(self):
  132. """Print information about this class in human readable style"""
  133. if self.get_type() == "raster":
  134. # 1 2 3 4 5 6 7
  135. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  136. print ""
  137. print " +-------------------- Raster Dataset ----------------------------------------+"
  138. if self.get_type() == "raster3d":
  139. # 1 2 3 4 5 6 7
  140. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  141. print ""
  142. print " +-------------------- Raster3d Dataset --------------------------------------+"
  143. if self.get_type() == "vector":
  144. # 1 2 3 4 5 6 7
  145. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  146. print ""
  147. print " +-------------------- Vector Dataset ----------------------------------------+"
  148. if self.get_type() == "strds":
  149. # 1 2 3 4 5 6 7
  150. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  151. print ""
  152. print " +-------------------- Space Time Raster Dataset -----------------------------+"
  153. if self.get_type() == "str3ds":
  154. # 1 2 3 4 5 6 7
  155. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  156. print ""
  157. print " +-------------------- Space Time Raster3d Dataset ---------------------------+"
  158. if self.get_type() == "stvds":
  159. # 1 2 3 4 5 6 7
  160. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  161. print ""
  162. print " +-------------------- Space Time Vector Dataset -----------------------------+"
  163. print " | |"
  164. self.base.print_info()
  165. if self.is_time_absolute():
  166. self.absolute_time.print_info()
  167. if self.is_time_relative():
  168. self.relative_time.print_info()
  169. self.spatial_extent.print_info()
  170. self.metadata.print_info()
  171. print " +----------------------------------------------------------------------------+"
  172. def print_shell_info(self):
  173. """Print information about this class in shell style"""
  174. self.base.print_shell_info()
  175. if self.is_time_absolute():
  176. self.absolute_time.print_shell_info()
  177. if self.is_time_relative():
  178. self.relative_time.print_shell_info()
  179. self.spatial_extent.print_shell_info()
  180. self.metadata.print_shell_info()
  181. def set_time_to_absolute(self):
  182. self.base.set_ttype("absolute")
  183. def set_time_to_relative(self):
  184. self.base.set_ttype("relative")
  185. def is_time_absolute(self):
  186. if self.base.D.has_key("temporal_type"):
  187. return self.base.get_ttype() == "absolute"
  188. else:
  189. return None
  190. def is_time_relative(self):
  191. if self.base.D.has_key("temporal_type"):
  192. return self.base.get_ttype() == "relative"
  193. else:
  194. return None
  195. def temporal_relation(self, map):
  196. """Return the temporal relation of this and the provided temporal map"""
  197. if self.is_time_absolute() and map.is_time_absolute():
  198. return self.absolute_time.temporal_relation(map.absolute_time)
  199. if self.is_time_relative() and map.is_time_relative():
  200. return self.relative_time.temporal_relation(map.relative_time)
  201. return None