region.py 9.5 KB

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