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