region.py 9.5 KB

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