abstract_dataset.py 8.4 KB

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