__init__.py 14 KB

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