__init__.py 14 KB

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