__init__.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import (nested_scopes, generators, division, absolute_import,
  4. with_statement, print_function, unicode_literals)
  5. from os import listdir
  6. from os.path import join, isdir
  7. import shutil
  8. import ctypes as ct
  9. import fnmatch
  10. import grass.lib.gis as libgis
  11. from grass.pygrass.errors import GrassError
  12. from grass.script.utils import encode, decode
  13. test_vector_name = "Gis_test_vector"
  14. test_raster_name = "Gis_test_raster"
  15. libgis.G_gisinit('')
  16. ETYPE = {'raster': libgis.G_ELEMENT_RASTER,
  17. 'raster_3d': libgis.G_ELEMENT_RASTER3D,
  18. 'vector': libgis.G_ELEMENT_VECTOR,
  19. 'label': libgis.G_ELEMENT_LABEL,
  20. 'region': libgis.G_ELEMENT_REGION,
  21. 'group': libgis.G_ELEMENT_GROUP}
  22. CHECK_IS = {"GISBASE": libgis.G_is_gisbase,
  23. "GISDBASE": lambda x: True,
  24. "LOCATION_NAME": libgis.G_is_location,
  25. "MAPSET": libgis.G_is_mapset}
  26. def is_valid(value, path, type):
  27. """Private function to check the correctness of a value.
  28. :param value: Name of the directory
  29. :type value: str
  30. :param path: Path where the directory is located
  31. :type path: path
  32. :param type: it is a string defining the type that will e checked,
  33. valid types are: GISBASE, GISDBASE, LOCATION_NAME, MAPSET
  34. :type type: str
  35. :return: True if valid else False
  36. :rtype: str
  37. """
  38. return bool(CHECK_IS[type](join(path, value)))
  39. def _check_raise(value, path, type):
  40. """Private function to check the correctness of a value.
  41. :param value: Name of the directory
  42. :type value: str
  43. :param path: Path where the directory is located
  44. :type path: path
  45. :param type: it is a string defining the type that will e checked,
  46. valid types are: GISBASE, GISDBASE, LOCATION_NAME, MAPSET
  47. :type type: str
  48. :return: the value if verify else None and
  49. if value is empty return environmental variable
  50. :rtype: str
  51. """
  52. if value is '':
  53. from grass.pygrass.utils import getenv
  54. return getenv(type)
  55. if is_valid(value, path, type):
  56. return value
  57. raise GrassError("%s <%s> not found" % (type.title(), join(path, value)))
  58. def set_current_mapset(mapset, location=None, gisdbase=None):
  59. """Set the current mapset as working area
  60. :param mapset: Name of the mapset
  61. :type value: str
  62. :param location: Name of the location
  63. :type location: str
  64. :param gisdbase: Name of the gisdbase
  65. :type gisdbase: str
  66. """
  67. libgis.G_setenv('MAPSET', mapset)
  68. if location:
  69. libgis.G_setenv('LOCATION_NAME', location)
  70. if gisdbase:
  71. libgis.G_setenv('GISDBASE', gisdbase)
  72. def make_mapset(mapset, location=None, gisdbase=None):
  73. """Create a new mapset
  74. :param mapset: Name of the mapset
  75. :type value: str
  76. :param location: Name of the location
  77. :type location: str
  78. :param gisdbase: Name of the gisdbase
  79. :type gisdbase: str"""
  80. res = libgis.G_make_mapset(gisdbase, location, mapset)
  81. if res == -1:
  82. raise GrassError("Cannot create new mapset")
  83. elif res == -2:
  84. raise GrassError("Illegal name")
  85. class Gisdbase(object):
  86. """Return Gisdbase object. ::
  87. >>> from grass.script.core import gisenv
  88. >>> gisdbase = Gisdbase()
  89. >>> gisdbase.name == gisenv()['GISDBASE']
  90. True
  91. ..
  92. """
  93. def __init__(self, gisdbase=''):
  94. self.name = gisdbase
  95. def _get_name(self):
  96. return self._name
  97. def _set_name(self, name):
  98. self._name = _check_raise(name, '', "GISDBASE")
  99. name = property(fget=_get_name, fset=_set_name,
  100. doc="Set or obtain the name of GISDBASE")
  101. def __str__(self):
  102. return self.name
  103. def __repr__(self):
  104. return 'Gisdbase(%s)' % self.name
  105. def __getitem__(self, location):
  106. """Return a Location object. ::
  107. >>> from grass.script.core import gisenv
  108. >>> loc_env = gisenv()['LOCATION_NAME']
  109. >>> gisdbase = Gisdbase()
  110. >>> loc_py = gisdbase[loc_env]
  111. >>> loc_env == loc_py.name
  112. True
  113. ..
  114. """
  115. if location in self.locations():
  116. return Location(location, self.name)
  117. else:
  118. raise KeyError('Location: %s does not exist' % location)
  119. def __iter__(self):
  120. for loc in self.locations():
  121. yield Location(loc, self.name)
  122. # TODO remove or complete this function
  123. def new_location(self):
  124. if libgis.G_make_location() != 0:
  125. raise GrassError("Cannot create new location")
  126. def locations(self):
  127. """Return a list of locations that are available in the gisdbase: ::
  128. >>> gisdbase = Gisdbase()
  129. >>> gisdbase.locations() # doctest: +ELLIPSIS
  130. [...]
  131. ..
  132. """
  133. return sorted([loc for loc in listdir(self.name)
  134. if libgis.G_is_location(encode(join(self.name, loc)))])
  135. class Location(object):
  136. """Location object ::
  137. >>> from grass.script.core import gisenv
  138. >>> location = Location()
  139. >>> location # doctest: +ELLIPSIS
  140. Location(...)
  141. >>> location.gisdbase == gisenv()['GISDBASE']
  142. True
  143. >>> location.name == gisenv()['LOCATION_NAME']
  144. True
  145. ..
  146. """
  147. def __init__(self, location='', gisdbase=''):
  148. self.gisdbase = gisdbase
  149. self.name = location
  150. def _get_gisdb(self):
  151. return self._gisdb
  152. def _set_gisdb(self, gisdb):
  153. self._gisdb = _check_raise(gisdb, '', "GISDBASE")
  154. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
  155. doc="Set or obtain the name of GISDBASE")
  156. def _get_name(self):
  157. return self._name
  158. def _set_name(self, name):
  159. self._name = _check_raise(name, self._gisdb, "LOCATION_NAME")
  160. name = property(fget=_get_name, fset=_set_name,
  161. doc="Set or obtain the name of LOCATION")
  162. def __getitem__(self, mapset):
  163. if mapset in self.mapsets():
  164. return Mapset(mapset)
  165. else:
  166. raise KeyError('Mapset: %s does not exist' % mapset)
  167. def __iter__(self):
  168. lpath = self.path()
  169. return (m for m in listdir(lpath)
  170. if (isdir(join(lpath, m)) and is_valid(m, lpath, "MAPSET")))
  171. def __len__(self):
  172. return len(self.mapsets())
  173. def __str__(self):
  174. return self.name
  175. def __repr__(self):
  176. return 'Location(%r)' % self.name
  177. def mapsets(self, pattern=None, permissions=True):
  178. """Return a list of the available mapsets.
  179. :param pattern: the pattern to filter the result
  180. :type pattern: str
  181. :param permissions: check the permission of mapset
  182. :type permissions: bool
  183. :return: a list of mapset's names
  184. :rtype: list of strings
  185. ::
  186. >>> location = Location()
  187. >>> sorted(location.mapsets()) # doctest: +ELLIPSIS
  188. [...]
  189. """
  190. mapsets = [mapset for mapset in self]
  191. if permissions:
  192. mapsets = [mapset for mapset in mapsets
  193. if libgis.G_mapset_permissions(encode(mapset))]
  194. if pattern:
  195. return fnmatch.filter(mapsets, pattern)
  196. return mapsets
  197. def path(self):
  198. """Return the complete path of the location"""
  199. return join(self.gisdbase, self.name)
  200. class Mapset(object):
  201. """Mapset ::
  202. >>> from grass.script.core import gisenv
  203. >>> genv = gisenv()
  204. >>> mapset = Mapset()
  205. >>> mapset # doctest: +ELLIPSIS
  206. Mapset(...)
  207. >>> mapset.gisdbase == genv['GISDBASE']
  208. True
  209. >>> mapset.location == genv['LOCATION_NAME']
  210. True
  211. >>> mapset.name == genv['MAPSET']
  212. True
  213. ..
  214. """
  215. def __init__(self, mapset='', location='', gisdbase=''):
  216. self.gisdbase = gisdbase
  217. self.location = location
  218. self.name = mapset
  219. self.visible = VisibleMapset(self.name, self.location, self.gisdbase)
  220. def _get_gisdb(self):
  221. return self._gisdb
  222. def _set_gisdb(self, gisdb):
  223. self._gisdb = _check_raise(gisdb, '', "GISDBASE")
  224. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
  225. doc="Set or obtain the name of GISDBASE")
  226. def _get_loc(self):
  227. return self._loc
  228. def _set_loc(self, loc):
  229. self._loc = _check_raise(loc, self._gisdb, "LOCATION_NAME")
  230. location = property(fget=_get_loc, fset=_set_loc,
  231. doc="Set or obtain the name of LOCATION")
  232. def _get_name(self):
  233. return self._name
  234. def _set_name(self, name):
  235. self._name = _check_raise(name, join(self._gisdb, self._loc), "MAPSET")
  236. name = property(fget=_get_name, fset=_set_name,
  237. doc="Set or obtain the name of MAPSET")
  238. def __str__(self):
  239. return self.name
  240. def __repr__(self):
  241. return 'Mapset(%r)' % self.name
  242. def glist(self, type, pattern=None):
  243. """Return a list of grass types like:
  244. * 'group',
  245. * 'label',
  246. * 'raster',
  247. * 'raster_3d',
  248. * 'region',
  249. * 'vector',
  250. :param type: the type of element to query
  251. :type type: str
  252. :param pattern: the pattern to filter the result
  253. :type pattern: str
  254. ::
  255. >>> mapset = Mapset()
  256. >>> mapset.current()
  257. >>> rast = mapset.glist('raster')
  258. >>> test_raster_name in rast
  259. True
  260. >>> vect = mapset.glist('vector')
  261. >>> test_vector_name in vect
  262. True
  263. ..
  264. """
  265. if type not in ETYPE:
  266. str_err = "Type %s is not valid, valid types are: %s."
  267. raise TypeError(str_err % (type, ', '.join(ETYPE.keys())))
  268. clist = libgis.G_list(ETYPE[type], self.gisdbase,
  269. self.location, self.name)
  270. elist = []
  271. for el in clist:
  272. el_name = ct.cast(el, ct.c_char_p).value
  273. if el_name:
  274. elist.append(el_name)
  275. else:
  276. if pattern:
  277. return fnmatch.filter(elist, pattern)
  278. return elist
  279. def is_current(self):
  280. """Check if the MAPSET is the working MAPSET"""
  281. return (self.name == libgis.G_getenv('MAPSET') and
  282. self.location == libgis.G_getenv('LOCATION_NAME') and
  283. self.gisdbase == libgis.G_getenv('GISDBASE'))
  284. def current(self):
  285. """Set the mapset as current"""
  286. set_current_mapset(self.name, self.location, self.gisdbase)
  287. def delete(self):
  288. """Delete the mapset"""
  289. if self.is_current():
  290. raise GrassError('The mapset is in use.')
  291. shutil.rmtree(self.path())
  292. def path(self):
  293. """Return the complete path of the mapset"""
  294. return join(self.gisdbase, self.location, self.name)
  295. class VisibleMapset(object):
  296. """VisibleMapset object
  297. """
  298. def __init__(self, mapset, location='', gisdbase=''):
  299. self.mapset = mapset
  300. self.location = Location(location, gisdbase)
  301. self._list = []
  302. self.spath = join(self.location.path(), self.mapset, 'SEARCH_PATH')
  303. def __repr__(self):
  304. return repr(self.read())
  305. def __iter__(self):
  306. for mapset in self.read():
  307. yield mapset
  308. def read(self):
  309. """Return the mapsets in the search path"""
  310. with open(self.spath, "ab+") as f:
  311. lines = f.readlines()
  312. if lines:
  313. return [decode(l.strip()) for l in lines]
  314. lns = [u'PERMANENT', ]
  315. self._write(lns)
  316. return lns
  317. def _write(self, mapsets):
  318. """Write to SEARCH_PATH file the changes in the search path
  319. :param mapsets: a list of mapset's names
  320. :type mapsets: list
  321. """
  322. with open(self.spath, "wb+") as f:
  323. ms = [decode(m) for m in self.location.mapsets()]
  324. f.write(b'\n'.join([encode(m) for m in mapsets if m in ms]))
  325. def add(self, mapset):
  326. """Add a mapset to the search path
  327. :param mapset: a mapset's name
  328. :type mapset: str
  329. """
  330. if mapset not in self.read() and mapset in self.location:
  331. with open(self.spath, "a+") as f:
  332. f.write('\n%s' % mapset)
  333. else:
  334. raise TypeError('Mapset not found')
  335. def remove(self, mapset):
  336. """Remove mapset to the search path
  337. :param mapset: a mapset's name
  338. :type mapset: str
  339. """
  340. mapsets = self.read()
  341. mapsets.remove(mapset)
  342. self._write(mapsets)
  343. def extend(self, mapsets):
  344. """Add more mapsets to the search path
  345. :param mapsets: a list of mapset's names
  346. :type mapsets: list
  347. """
  348. ms = [decode(m) for m in self.location.mapsets()]
  349. final = [decode(m) for m in self.read()]
  350. mapsets = [decode(m) for m in mapsets]
  351. final.extend([m for m in mapsets if m in ms and m not in final])
  352. self._write(final)
  353. def reset(self):
  354. """Reset to the original search path"""
  355. final = [self.mapset, 'PERMANENT']
  356. self._write(final)
  357. if __name__ == "__main__":
  358. import doctest
  359. from grass.pygrass import utils
  360. from grass.script.core import run_command
  361. utils.create_test_vector_map(test_vector_name)
  362. run_command("g.region", n=50, s=0, e=60, w=0, res=1)
  363. run_command("r.mapcalc", expression="%s = 1" % (test_raster_name),
  364. overwrite=True)
  365. run_command("g.region", n=40, s=0, e=40, w=0, res=2)
  366. doctest.testmod()
  367. # Remove the generated vector map, if exist
  368. mset = utils.get_mapset_vector(test_vector_name, mapset='')
  369. if mset:
  370. run_command("g.remove", flags='f', type='vector',
  371. name=test_vector_name)
  372. mset = utils.get_mapset_raster(test_raster_name, mapset='')
  373. if mset:
  374. run_command("g.remove", flags='f', type='raster',
  375. name=test_raster_name)