__init__.py 14 KB

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