region.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri May 25 12:57:10 2012
  4. @author: Pietro Zambelli
  5. """
  6. import ctypes
  7. import grass.lib.gis as libgis
  8. import grass.script as grass
  9. from grass.pygrass.errors import GrassError
  10. class Region(object):
  11. def __init__(self, default=False):
  12. """::
  13. >>> default = Region(default=True)
  14. >>> current = Region()
  15. >>> current_good = Region()
  16. >>> default == current
  17. True
  18. >>> current.cols
  19. 1500
  20. >>> current.ewres
  21. 10.0
  22. >>> current.cols = 3000
  23. >>> current.ewres
  24. 5.0
  25. >>> current.ewres = 20.0
  26. >>> current.cols
  27. 750
  28. >>> current.set_current()
  29. >>> default == current
  30. False
  31. >>> current.get_default()
  32. >>> default = Region(default=True)
  33. >>> default == current
  34. True
  35. >>> current_good.set_current()
  36. """
  37. self.c_region = ctypes.pointer(libgis.Cell_head())
  38. if default:
  39. self.get_default()
  40. else:
  41. self.get_current()
  42. def _set_param(self, key, value):
  43. grass.run_command('g.region', **{key: value})
  44. #----------LIMITS----------
  45. def _get_n(self):
  46. """Private function to obtain north value"""
  47. return self.c_region.contents.north
  48. def _set_n(self, value):
  49. """Private function to set north value"""
  50. self.c_region.contents.north = value
  51. north = property(fget=_get_n, fset=_set_n)
  52. def _get_s(self):
  53. """Private function to obtain south value"""
  54. return self.c_region.contents.south
  55. def _set_s(self, value):
  56. """Private function to set south value"""
  57. self.c_region.contents.south = value
  58. south = property(fget=_get_s, fset=_set_s)
  59. def _get_e(self):
  60. """Private function to obtain east value"""
  61. return self.c_region.contents.east
  62. def _set_e(self, value):
  63. """Private function to set east value"""
  64. self.c_region.contents.east = value
  65. east = property(fget=_get_e, fset=_set_e)
  66. def _get_w(self):
  67. """Private function to obtain west value"""
  68. return self.c_region.contents.west
  69. def _set_w(self, value):
  70. """Private function to set west value"""
  71. self.c_region.contents.west = value
  72. west = property(fget=_get_w, fset=_set_w)
  73. def _get_t(self):
  74. """Private function to obtain top value"""
  75. return self.c_region.contents.top
  76. def _set_t(self, value):
  77. """Private function to set top value"""
  78. self.c_region.contents.top = value
  79. top = property(fget=_get_t, fset=_set_t)
  80. def _get_b(self):
  81. """Private function to obtain bottom value"""
  82. return self.c_region.contents.bottom
  83. def _set_b(self, value):
  84. """Private function to set bottom value"""
  85. self.c_region.contents.bottom = value
  86. bottom = property(fget=_get_b, fset=_set_b)
  87. #----------RESOLUTION----------
  88. def _get_rows(self):
  89. """Private function to obtain rows value"""
  90. return self.c_region.contents.rows
  91. def _set_rows(self, value):
  92. """Private function to set rows value"""
  93. self.c_region.contents.rows = value
  94. self.adjust(rows=True)
  95. rows = property(fget=_get_rows, fset=_set_rows)
  96. def _get_cols(self):
  97. """Private function to obtain columns value"""
  98. return self.c_region.contents.cols
  99. def _set_cols(self, value):
  100. """Private function to set columns value"""
  101. self.c_region.contents.cols = value
  102. self.adjust(cols=True)
  103. cols = property(fget=_get_cols, fset=_set_cols)
  104. def _get_nsres(self):
  105. """Private function to obtain north-south value"""
  106. return self.c_region.contents.ns_res
  107. def _set_nsres(self, value):
  108. """Private function to obtain north-south value"""
  109. self.c_region.contents.ns_res = value
  110. self.adjust()
  111. nsres = property(fget=_get_nsres, fset=_set_nsres)
  112. def _get_ewres(self):
  113. """Private function to obtain east-west value"""
  114. return self.c_region.contents.ew_res
  115. def _set_ewres(self, value):
  116. """Private function to set east-west value"""
  117. self.c_region.contents.ew_res = value
  118. self.adjust()
  119. ewres = property(fget=_get_ewres, fset=_set_ewres)
  120. def _get_tbres(self):
  121. """Private function to obtain top-botton 3D value"""
  122. return self.c_region.contents.tb_res
  123. def _set_tbres(self, value):
  124. """Private function to set top-bottom 3D value"""
  125. self.c_region.contents.tb_res = value
  126. self.adjust()
  127. tbres = property(fget=_get_tbres, fset=_set_tbres)
  128. @property
  129. def zone(self):
  130. """Return the zone of projection
  131. >>> reg = Region()
  132. >>> reg.zone
  133. 0
  134. """
  135. return self.c_region.contents.zone
  136. @property
  137. def proj(self):
  138. """Return a code for projection
  139. >>> reg = Region()
  140. >>> reg.proj
  141. 99
  142. """
  143. return self.c_region.contents.proj
  144. #----------MAGIC METHODS----------
  145. def __repr__(self):
  146. return 'Region(n=%g, s=%g, e=%g, w=%g, nsres=%g, ewres=%g)' % (
  147. self.north, self.south, self.east, self.west,
  148. self.nsres, self.ewres)
  149. def __unicode__(self):
  150. return grass.pipe_command("g.region", flags="p").communicate()[0]
  151. def __str__(self):
  152. return self.__unicode__()
  153. def __eq__(self, reg):
  154. attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
  155. 'nsres', 'ewres', 'tbres']
  156. for attr in attrs:
  157. if getattr(self, attr) != getattr(reg, attr):
  158. return False
  159. return True
  160. def iteritems(self):
  161. return [('projection', self.proj),
  162. ('zone', self.zone),
  163. ('north', self.north),
  164. ('south', self.south),
  165. ('west', self.west),
  166. ('east', self.east),
  167. ('top', self.top),
  168. ('bottom', self.bottom),
  169. ('nsres', self.nsres),
  170. ('ewres', self.ewres),
  171. ('tbres', self.tbres),
  172. ('rows', self.rows),
  173. ('cols', self.cols),
  174. ('cells', self.rows * self.cols)]
  175. #----------METHODS----------
  176. def zoom(self, raster_name):
  177. """Shrink region until it meets non-NULL data from this raster map:"""
  178. self._set_param('zoom', str(raster_name))
  179. self.get_current()
  180. def align(self, raster_name):
  181. """Adjust region cells to cleanly align with this raster map"""
  182. self._set_param('align', str(raster_name))
  183. self.get_current()
  184. def adjust(self, rows=False, cols=False):
  185. """Adjust rows and cols number according with the nsres and ewres
  186. resolutions. If rows or cols parameters are True, the adjust method
  187. update nsres and ewres according with the rows and cols numbers.
  188. """
  189. libgis.G_adjust_Cell_head(self.c_region, bool(rows), bool(cols))
  190. def vect(self, vector_name):
  191. """Adjust bounding box of region using a vector ::
  192. >>> reg = Region()
  193. >>> reg.vect('census')
  194. >>> reg.get_bbox()
  195. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  196. >>> reg.get_default()
  197. """
  198. from grass.pygrass.vector import VectorTopo
  199. vect = VectorTopo(vector_name)
  200. vect.open()
  201. bbox = vect.bbox()
  202. self.set_bbox(bbox)
  203. def get_current(self):
  204. """Set the current GRASS region to the Region object"""
  205. libgis.G_get_set_window(self.c_region)
  206. def set_current(self):
  207. """Set the Region object to the current GRASS region"""
  208. libgis.G_set_window(self.c_region)
  209. def get_default(self):
  210. """Set the default GRASS region to the Region object"""
  211. libgis.G_get_window(self.c_region)
  212. def set_default(self):
  213. """Set the Region object to the default GRASS region.
  214. It works only in PERMANENT mapset"""
  215. from grass.pygrass.gis import Mapset
  216. mapset = Mapset()
  217. if mapset.name != 'PERMANENT':
  218. raise GrassError("ERROR: Unable to change default region. The " \
  219. "current mapset is not <PERMANENT>.")
  220. self.adjust()
  221. if libgis.G_put_window(self.c_region) < 0:
  222. raise GrassError("Cannot change region (DEFAUL_WIND file).")
  223. def get_bbox(self):
  224. """Return a Bbox object with the extension of the region ::
  225. >>> reg = Region()
  226. >>> reg.get_bbox()
  227. Bbox(228500.0, 215000.0, 645000.0, 630000.0)
  228. """
  229. from grass.pygrass.vector.basic import Bbox
  230. return Bbox(north=self.north, south=self.south,
  231. east=self.east, west=self.west,
  232. top=self.top, bottom=self.bottom)
  233. def set_bbox(self, bbox):
  234. """Set region from Bbox ::
  235. >>> from grass.pygrass.vector.basic import Bbox
  236. >>> b = Bbox(230963.640878, 212125.562878,
  237. ... 645837.437393, 628769.374393)
  238. >>> reg = Region()
  239. >>> reg.set_bbox(b)
  240. >>> reg.get_bbox()
  241. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  242. >>> reg.get_current()
  243. """
  244. self.north = bbox.north
  245. self.south = bbox.south
  246. self.east = bbox.east
  247. self.west = bbox.west