__init__.py 14 KB

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