spatial_extent.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related spatial extent functions to be used in Python scripts and tgis packages.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. extent = tgis.raster_spatial_extent()
  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. from base import *
  17. class spatial_extent(sql_database_interface):
  18. """This is the spatial extent base class for all maps and spacetime datasets"""
  19. def __init__(self, table=None, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None, proj="XY"):
  20. sql_database_interface.__init__(self, table, ident)
  21. self.set_id(ident)
  22. self.set_spatial_extent(north, south, east, west, top, bottom)
  23. self.set_projection(proj)
  24. def overlap_2d(self, extent):
  25. """Return True if the 2d extents overlap. Code is lend from wind_overlap.c in lib/gis"""
  26. if self.get_projection() != extent.get_projection():
  27. core.error(_("Projections are different. Unable to compute overlap_2d for spatial extents"))
  28. N = extent.get_north()
  29. S = extent.get_south()
  30. E = extent.get_east()
  31. W = extent.get_west()
  32. if(self.get_north() <= S):
  33. return False
  34. if(self.get_south() >= N):
  35. return False
  36. # Adjust the east and west in case of LL projection
  37. if self.get_proj() == "LL":
  38. while E < self.get_west():
  39. E += 360.0
  40. W += 360.0
  41. while W > self.get_east():
  42. E -= 360.0
  43. W -= 360.0
  44. if self.get_east() <= W:
  45. return False
  46. if self.get_west() >= E:
  47. return False
  48. return True
  49. def overlap(self, extent):
  50. """Return True if the extents overlap."""
  51. if self.overlap_2d(extent) == False:
  52. return False
  53. T = extent.get_top()
  54. B = extent.get_bottom()
  55. if self.get_top() <= B:
  56. return False
  57. if self.get_bottom() >= T:
  58. return False
  59. return True
  60. def set_spatial_extent(self, north, south, east, west, top, bottom):
  61. """Set the spatial extent"""
  62. self.set_north(north)
  63. self.set_south(south)
  64. self.set_east(east)
  65. self.set_west(west)
  66. self.set_top(top)
  67. self.set_bottom(bottom)
  68. def set_projection(self, proj):
  69. """Set the projection of the spatial extent it should be XY or LL.
  70. As default the projection is XY
  71. """
  72. if proj == None or (proj != "XY" and proj != "LL"):
  73. self.D["proj"] = "XY"
  74. else:
  75. self.D["proj"] = proj
  76. def set_spatial_extent_2d(self, north, south, east, west):
  77. self.set_id(ident)
  78. self.set_north(north)
  79. self.set_south(south)
  80. self.set_east(east)
  81. self.set_west(west)
  82. self.set_top(0)
  83. self.set_bottom(0)
  84. def set_id(self, ident):
  85. """Convenient method to set the unique identifier (primary key)"""
  86. self.ident = ident
  87. self.D["id"] = ident
  88. def set_north(self, north):
  89. """Set the northern edge of the map"""
  90. self.D["north"] = north
  91. def set_south(self, sourth):
  92. """Set the sourthern edge of the map"""
  93. self.D["south"] = sourth
  94. def set_west(self, west):
  95. """Set the western edge of the map"""
  96. self.D["west"] = west
  97. def set_east(self, east):
  98. """Set the eastern edge of the map"""
  99. self.D["east"] = east
  100. def set_top(self, top):
  101. """Set the top edge of the map"""
  102. self.D["top"] = top
  103. def set_bottom(self, bottom):
  104. """Set the bottom edge of the map"""
  105. self.D["bottom"] = bottom
  106. def get_id(self):
  107. """Convenient method to get the unique identifier (primary key)
  108. @return None if not found
  109. """
  110. if self.D.has_key("id"):
  111. return self.D["id"]
  112. else:
  113. return None
  114. def get_projection(self):
  115. """Get the projection of the spatial extent"""
  116. return self.D["proj"]
  117. def get_volume(self):
  118. """Compute the volume of the extent, in case z is zero (top == bottom or top - bottom = 1) the area is returned"""
  119. if self.get_projection() == "LL":
  120. core.error(_("Volume computation is not supported for LL projections"))
  121. area = self.get_area()
  122. bbox = self.get_spatial_extent()
  123. z = abs(bbox[4] - bbox[5])
  124. if z == 0:
  125. z = 1.0
  126. return area*z
  127. def get_area(self):
  128. """Compute the area of the extent, extent in z direction is ignored"""
  129. if self.get_projection() == "LL":
  130. core.error(_("Area computation is not supported for LL projections"))
  131. bbox = self.get_spatial_extent()
  132. y = abs(bbox[0] - bbox[1])
  133. x = abs(bbox[2] - bbox[3])
  134. return x*y
  135. def get_spatial_extent(self):
  136. """Return a tuple (north, south, east, west, top, bottom) of the spatial extent"""
  137. return (self.get_north(), self.get_south, self.get_east(), self.get_west(), \
  138. self.get_top(), self.get_bottom())
  139. def get_spatial_extent_2d(self):
  140. """Return a tuple (north, south, east, west,) of the 2d spatial extent"""
  141. return (self.get_north(), self.get_south, self.get_east(), self.get_west())
  142. def get_north(self):
  143. """Get the northern edge of the map
  144. @return None if not found"""
  145. if self.D.has_key("north"):
  146. return self.D["north"]
  147. else:
  148. return None
  149. def get_south(self):
  150. """Get the southern edge of the map
  151. @return None if not found"""
  152. if self.D.has_key("south"):
  153. return self.D["south"]
  154. else:
  155. return None
  156. def get_east(self):
  157. """Get the eastern edge of the map
  158. @return None if not found"""
  159. if self.D.has_key("east"):
  160. return self.D["east"]
  161. else:
  162. return None
  163. def get_west(self):
  164. """Get the western edge of the map
  165. @return None if not found"""
  166. if self.D.has_key("west"):
  167. return self.D["west"]
  168. else:
  169. return None
  170. def get_top(self):
  171. """Get the top edge of the map
  172. @return None if not found"""
  173. if self.D.has_key("top"):
  174. return self.D["top"]
  175. else:
  176. return None
  177. def get_bottom(self):
  178. """Get the bottom edge of the map
  179. @return None if not found"""
  180. if self.D.has_key("bottom"):
  181. return self.D["bottom"]
  182. else:
  183. return None
  184. def print_info(self):
  185. """Print information about this class in human readable style"""
  186. # 0123456789012345678901234567890
  187. print " +-------------------- Spatial extent ----------------------------------------+"
  188. print " | North:...................... " + str(self.get_north())
  189. print " | South:...................... " + str(self.get_south())
  190. print " | East:.. .................... " + str(self.get_east())
  191. print " | West:....................... " + str(self.get_west())
  192. print " | Top:........................ " + str(self.get_top())
  193. print " | Bottom:..................... " + str(self.get_bottom())
  194. def print_shell_info(self):
  195. """Print information about this class in shell style"""
  196. print "north=" + str(self.get_north())
  197. print "south=" + str(self.get_south())
  198. print "east=" + str(self.get_east())
  199. print "west=" + str(self.get_west())
  200. print "top=" + str(self.get_top())
  201. print "bottom=" + str(self.get_bottom())
  202. ###############################################################################
  203. class raster_spatial_extent(spatial_extent):
  204. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  205. spatial_extent.__init__(self, "raster_spatial_extent", ident, north, south, east, west, top, bottom)
  206. class raster3d_spatial_extent(spatial_extent):
  207. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  208. spatial_extent.__init__(self, "raster3d_spatial_extent", ident, north, south, east, west, top, bottom)
  209. class vector_spatial_extent(spatial_extent):
  210. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  211. spatial_extent.__init__(self, "vector_spatial_extent", ident, north, south, east, west, top, bottom)
  212. class strds_spatial_extent(spatial_extent):
  213. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  214. spatial_extent.__init__(self, "strds_spatial_extent", ident, north, south, east, west, top, bottom)
  215. class str3ds_spatial_extent(spatial_extent):
  216. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  217. spatial_extent.__init__(self, "str3ds_spatial_extent", ident, north, south, east, west, top, bottom)
  218. class stvds_spatial_extent(spatial_extent):
  219. def __init__(self, ident=None, north=None, south=None, east=None, west=None, top=None, bottom=None):
  220. spatial_extent.__init__(self, "stvds_spatial_extent", ident, north, south, east, west, top, bottom)