region.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. """This class is design to easily access and modify GRASS computational
  15. region. ::
  16. >>> default = Region(default=True)
  17. >>> current_original = 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_original.set_current()
  40. ..
  41. """
  42. def __init__(self, default=False):
  43. self.c_region = ctypes.pointer(libgis.Cell_head())
  44. if default:
  45. self.get_default()
  46. else:
  47. self.get_current()
  48. def _set_param(self, key, value):
  49. grass.run_command('g.region', **{key: value})
  50. #----------LIMITS----------
  51. def _get_n(self):
  52. """Private function to obtain north value"""
  53. return self.c_region.contents.north
  54. def _set_n(self, value):
  55. """Private function to set north value"""
  56. self.c_region.contents.north = value
  57. north = property(fget=_get_n, fset=_set_n,
  58. doc="Set and obtain north coordinate")
  59. def _get_s(self):
  60. """Private function to obtain south value"""
  61. return self.c_region.contents.south
  62. def _set_s(self, value):
  63. """Private function to set south value"""
  64. self.c_region.contents.south = value
  65. south = property(fget=_get_s, fset=_set_s,
  66. doc="Set and obtain south coordinate")
  67. def _get_e(self):
  68. """Private function to obtain east value"""
  69. return self.c_region.contents.east
  70. def _set_e(self, value):
  71. """Private function to set east value"""
  72. self.c_region.contents.east = value
  73. east = property(fget=_get_e, fset=_set_e,
  74. doc="Set and obtain east coordinate")
  75. def _get_w(self):
  76. """Private function to obtain west value"""
  77. return self.c_region.contents.west
  78. def _set_w(self, value):
  79. """Private function to set west value"""
  80. self.c_region.contents.west = value
  81. west = property(fget=_get_w, fset=_set_w,
  82. doc="Set and obtain west coordinate")
  83. def _get_t(self):
  84. """Private function to obtain top value"""
  85. return self.c_region.contents.top
  86. def _set_t(self, value):
  87. """Private function to set top value"""
  88. self.c_region.contents.top = value
  89. top = property(fget=_get_t, fset=_set_t,
  90. doc="Set and obtain top value")
  91. def _get_b(self):
  92. """Private function to obtain bottom value"""
  93. return self.c_region.contents.bottom
  94. def _set_b(self, value):
  95. """Private function to set bottom value"""
  96. self.c_region.contents.bottom = value
  97. bottom = property(fget=_get_b, fset=_set_b,
  98. doc="Set and obtain bottom value")
  99. #----------RESOLUTION----------
  100. def _get_rows(self):
  101. """Private function to obtain rows value"""
  102. return self.c_region.contents.rows
  103. def _set_rows(self, value):
  104. """Private function to set rows value"""
  105. self.c_region.contents.rows = value
  106. self.adjust(rows=True)
  107. rows = property(fget=_get_rows, fset=_set_rows,
  108. doc="Set and obtain number of rows")
  109. def _get_cols(self):
  110. """Private function to obtain columns value"""
  111. return self.c_region.contents.cols
  112. def _set_cols(self, value):
  113. """Private function to set columns value"""
  114. self.c_region.contents.cols = value
  115. self.adjust(cols=True)
  116. cols = property(fget=_get_cols, fset=_set_cols,
  117. doc="Set and obtain number of columns")
  118. def _get_nsres(self):
  119. """Private function to obtain north-south value"""
  120. return self.c_region.contents.ns_res
  121. def _set_nsres(self, value):
  122. """Private function to obtain north-south value"""
  123. self.c_region.contents.ns_res = value
  124. self.adjust()
  125. nsres = property(fget=_get_nsres, fset=_set_nsres,
  126. doc="Set and obtain north-south resolution value")
  127. def _get_ewres(self):
  128. """Private function to obtain east-west value"""
  129. return self.c_region.contents.ew_res
  130. def _set_ewres(self, value):
  131. """Private function to set east-west value"""
  132. self.c_region.contents.ew_res = value
  133. self.adjust()
  134. ewres = property(fget=_get_ewres, fset=_set_ewres,
  135. doc="Set and obtain east-west resolution value")
  136. def _get_tbres(self):
  137. """Private function to obtain top-botton 3D value"""
  138. return self.c_region.contents.tb_res
  139. def _set_tbres(self, value):
  140. """Private function to set top-bottom 3D value"""
  141. self.c_region.contents.tb_res = value
  142. self.adjust()
  143. tbres = property(fget=_get_tbres, fset=_set_tbres,
  144. doc="Set and obtain top-bottom 3D value")
  145. @property
  146. def zone(self):
  147. """Return the zone of projection
  148. >>> reg = Region()
  149. >>> reg.zone
  150. 0
  151. """
  152. return self.c_region.contents.zone
  153. @property
  154. def proj(self):
  155. """Return a code for projection
  156. >>> reg = Region()
  157. >>> reg.proj
  158. 99
  159. """
  160. return self.c_region.contents.proj
  161. @property
  162. def cells(self):
  163. """Return the number of cells"""
  164. return self.rows * self.cols
  165. #----------MAGIC METHODS----------
  166. def __repr__(self):
  167. rg = 'Region(north=%g, south=%g, east=%g, west=%g, nsres=%g, ewres=%g)'
  168. return rg % (self.north, self.south, self.east, self.west,
  169. self.nsres, self.ewres)
  170. def _repr_html_(self):
  171. return dict2html(dict(self.items()), keys=self.keys(),
  172. border='1', kdec='b')
  173. def __unicode__(self):
  174. return grass.pipe_command("g.region", flags="pu").communicate()[0]
  175. def __str__(self):
  176. return self.__unicode__()
  177. def __eq__(self, reg):
  178. """Compare two region.
  179. >>> r0 = Region()
  180. >>> r1 = Region()
  181. >>> r2 = Region()
  182. >>> r2.nsres = 5
  183. >>> r0 == r1
  184. True
  185. >>> r1 == r2
  186. False
  187. """
  188. attrs = ['north', 'south', 'west', 'east', 'top', 'bottom',
  189. 'nsres', 'ewres', 'tbres']
  190. for attr in attrs:
  191. if getattr(self, attr) != getattr(reg, attr):
  192. return False
  193. return True
  194. def __ne__(self, other):
  195. return not self == other
  196. # Restore Python 2 hashing beaviour on Python 3
  197. __hash__ = object.__hash__
  198. def keys(self):
  199. """Return a list of valid keys. ::
  200. >>> reg = Region()
  201. >>> reg.keys() # doctest: +ELLIPSIS
  202. [u'proj', u'zone', ..., u'cols', u'cells']
  203. ..
  204. """
  205. return ['proj', 'zone', 'north', 'south', 'west', 'east',
  206. 'top', 'bottom', 'nsres', 'ewres', 'tbres', 'rows',
  207. 'cols', 'cells']
  208. def items(self):
  209. """Return a list of tuple with key and value. ::
  210. >>> reg = Region()
  211. >>> reg.items() # doctest: +ELLIPSIS
  212. [(u'proj', 99), ..., (u'cells', 2025000)]
  213. ..
  214. """
  215. return [(k, self.__getattribute__(k)) for k in self.keys()]
  216. #----------METHODS----------
  217. def zoom(self, raster_name):
  218. """Shrink region until it meets non-NULL data from this raster map
  219. :param raster_name: the name of raster
  220. :type raster_name: str
  221. """
  222. self._set_param('zoom', str(raster_name))
  223. self.get_current()
  224. def align(self, raster_name):
  225. """Adjust region cells to cleanly align with this raster map
  226. :param raster_name: the name of raster
  227. :type raster_name: str
  228. """
  229. self._set_param('align', str(raster_name))
  230. self.get_current()
  231. def adjust(self, rows=False, cols=False):
  232. """Adjust rows and cols number according with the nsres and ewres
  233. resolutions. If rows or cols parameters are True, the adjust method
  234. update nsres and ewres according with the rows and cols numbers.
  235. """
  236. libgis.G_adjust_Cell_head(self.c_region, bool(rows), bool(cols))
  237. def vect(self, vector_name):
  238. """Adjust bounding box of region using a vector
  239. :param vector_name: the name of vector
  240. :type vector_name: str
  241. ::
  242. >>> reg = Region()
  243. >>> reg.vect('census')
  244. >>> reg.get_bbox()
  245. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  246. >>> reg.get_default()
  247. ..
  248. """
  249. from grass.pygrass.vector import VectorTopo
  250. with VectorTopo(vector_name, mode='r') as vect:
  251. bbox = vect.bbox()
  252. self.set_bbox(bbox)
  253. def get_current(self):
  254. """Set the current GRASS region to the Region object"""
  255. libgis.G_get_set_window(self.c_region)
  256. def set_current(self):
  257. """Set the Region object to the current GRASS region"""
  258. libgis.G_set_window(self.c_region)
  259. def get_default(self):
  260. """Set the default GRASS region to the Region object"""
  261. libgis.G_get_window(self.c_region)
  262. def set_default(self):
  263. """Set the Region object to the default GRASS region.
  264. It works only in PERMANENT mapset"""
  265. from grass.pygrass.gis import Mapset
  266. mapset = Mapset()
  267. if mapset.name != 'PERMANENT':
  268. raise GrassError("ERROR: Unable to change default region. The " \
  269. "current mapset is not <PERMANENT>.")
  270. self.adjust()
  271. if libgis.G_put_window(self.c_region) < 0:
  272. raise GrassError("Cannot change region (DEFAUL_WIND file).")
  273. def get_bbox(self):
  274. """Return a Bbox object with the extension of the region. ::
  275. >>> reg = Region()
  276. >>> reg.get_bbox()
  277. Bbox(228500.0, 215000.0, 645000.0, 630000.0)
  278. ..
  279. """
  280. from grass.pygrass.vector.basic import Bbox
  281. return Bbox(north=self.north, south=self.south,
  282. east=self.east, west=self.west,
  283. top=self.top, bottom=self.bottom)
  284. def set_bbox(self, bbox):
  285. """Set region extent from Bbox
  286. :param bbox: a Bbox object to set the extent
  287. :type bbox: Bbox object
  288. ::
  289. >>> from grass.pygrass.vector.basic import Bbox
  290. >>> b = Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  291. >>> reg = Region()
  292. >>> reg.set_bbox(b)
  293. >>> reg.get_bbox()
  294. Bbox(230963.640878, 212125.562878, 645837.437393, 628769.374393)
  295. >>> reg.get_current()
  296. ..
  297. """
  298. self.north = bbox.north
  299. self.south = bbox.south
  300. self.east = bbox.east
  301. self.west = bbox.west