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