__init__.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python2.7
  3. from __future__ import print_function
  4. #import os
  5. from os import listdir
  6. from os.path import join
  7. import shutil
  8. import ctypes as ct
  9. import fnmatch
  10. from grass import script
  11. #from grass.script import setup
  12. #
  13. #
  14. #GISBASE = "/home/pietro/docdat/src/gis/grass/grass70/dist.x86_64-unknown-linux-gnu"
  15. #LOCATION = "nc_basic_spm_grass7'"
  16. #GISDBASE = "/home/pietro/docdat/gis"
  17. #MAPSET = "sqlite"
  18. #GUI = "wxpython"
  19. #
  20. #setup.init(GISBASE, GISDBASE, LOCATION, MAPSET)
  21. script.gisenv()
  22. import grass.lib.gis as libgis
  23. from grass.pygrass.functions import getenv
  24. from grass.pygrass.errors import GrassError
  25. import region
  26. #write dec to check if user have permissions or not
  27. ETYPE = {'rast': libgis.G_ELEMENT_RASTER,
  28. 'rast3d': libgis.G_ELEMENT_RASTER3D,
  29. 'vect': libgis.G_ELEMENT_VECTOR,
  30. 'oldvect': libgis.G_ELEMENT_OLDVECTOR,
  31. 'asciivect': libgis.G_ELEMENT_ASCIIVECTOR,
  32. 'icon': libgis.G_ELEMENT_ICON,
  33. 'labels': libgis.G_ELEMENT_LABEL,
  34. 'sites': libgis.G_ELEMENT_SITE,
  35. 'region': libgis.G_ELEMENT_REGION,
  36. 'region3d': libgis.G_ELEMENT_REGION3D,
  37. 'group': libgis.G_ELEMENT_GROUP,
  38. 'view3d': libgis.G_ELEMENT_3DVIEW}
  39. CHECK_IS = {"GISBASE": libgis.G_is_gisbase,
  40. "GISDBASE": lambda x: True,
  41. "LOCATION_NAME": libgis.G_is_location,
  42. "MAPSET": libgis.G_is_mapset}
  43. def _check(value, path, type):
  44. #import pdb; pdb.set_trace()
  45. if value and CHECK_IS[type](join(path, value)):
  46. return value
  47. elif value is '':
  48. return getenv(type)
  49. else:
  50. raise GrassError("%s <%s> not found." % (type.title(),
  51. join(path, value)))
  52. def set_current_mapset(mapset, location=None, gisdbase=None):
  53. libgis.G_setenv('MAPSET', mapset)
  54. if location:
  55. libgis.G_setenv('LOCATION_NAME', location)
  56. if gisdbase:
  57. libgis.G_setenv('GISDBASEE', gisdbase)
  58. def make_mapset(mapset, location=None, gisdbase=None):
  59. if libgis.G__make_mapset(gisdbase, location, mapset) != 0:
  60. raise GrassError("I cannot create a new mapset.")
  61. class Gisdbase(object):
  62. """Return Gisdbase object. ::
  63. >>> from grass.script.core import gisenv
  64. >>> gisdbase = Gisdbase()
  65. >>> gisdbase.name == gisenv()['GISDBASE']
  66. True
  67. """
  68. def __init__(self, gisdbase=''):
  69. self.name = gisdbase
  70. def _get_name(self):
  71. return self._name
  72. def _set_name(self, name):
  73. self._name = _check(name, '', "GISDBASE")
  74. name = property(fget=_get_name, fset=_set_name)
  75. def __str__(self):
  76. return self.name
  77. def __repr__(self):
  78. return 'Gisdbase(%s)' % self.name
  79. def __getitem__(self, location):
  80. """Return a Location object. ::
  81. >>> from grass.script.core import gisenv
  82. >>> loc_env = gisenv()['LOCATION_NAME']
  83. >>> gisdbase = Gisdbase()
  84. >>> loc_py = gisdbase[loc_env]
  85. >>> loc_env == loc_py.name
  86. True
  87. ..
  88. """
  89. if location in self.locations():
  90. return Location(location, self.name)
  91. else:
  92. raise KeyError('Location: %s does not exist' % location)
  93. def __iter__(self):
  94. for loc in self.locations():
  95. yield Location(loc, self.name)
  96. def new_location(self):
  97. if libgis.G__make_location() != 0:
  98. raise GrassError("I cannot create a new mapset.")
  99. def locations(self):
  100. """Return a list of locations that are available in the gisdbase: ::
  101. >>> gisdbase = Gisdbase()
  102. >>> gisdbase.locations() # doctest: +ELLIPSIS
  103. [...]
  104. ..
  105. """
  106. locations = []
  107. for loc in listdir(self.name):
  108. if libgis.G_is_location(join(self.name, loc)):
  109. locations.append(loc)
  110. locations.sort()
  111. return locations
  112. class Location(object):
  113. """Location object ::
  114. >>> from grass.script.core import gisenv
  115. >>> location = Location()
  116. >>> location # doctest: +ELLIPSIS
  117. Location(...)
  118. >>> location.gisdbase == gisenv()['GISDBASE']
  119. True
  120. >>> location.name == gisenv()['LOCATION_NAME']
  121. True
  122. """
  123. def __init__(self, location='', gisdbase=''):
  124. self.gisdbase = gisdbase
  125. self.name = location
  126. def _get_gisdb(self):
  127. return self._gisdb
  128. def _set_gisdb(self, gisdb):
  129. self._gisdb = _check(gisdb, '', "GISDBASE")
  130. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
  131. def _get_name(self):
  132. return self._name
  133. def _set_name(self, name):
  134. self._name = _check(name, self._gisdb, "LOCATION_NAME")
  135. name = property(fget=_get_name, fset=_set_name)
  136. def __getitem__(self, mapset):
  137. if mapset in self.mapsets():
  138. return Mapset(mapset)
  139. else:
  140. raise KeyError('Mapset: %s does not exist' % mapset)
  141. def __iter__(self):
  142. for mapset in libgis.G_available_mapsets():
  143. mapset_name = ct.cast(mapset, ct.c_char_p).value
  144. if mapset_name and libgis.G__mapset_permissions(mapset):
  145. yield mapset_name
  146. else:
  147. break
  148. def __len__(self):
  149. return len(self.mapsets())
  150. def __str__(self):
  151. return self.name
  152. def __repr__(self):
  153. return 'Location(%r)' % self.name
  154. def mapsets(self, pattern=None):
  155. """Return a list of the available mapsets. ::
  156. >>> location = Location()
  157. >>> location.mapsets()
  158. ['PERMANENT', 'user1']
  159. """
  160. mapsets = [mapset for mapset in self]
  161. if pattern:
  162. return fnmatch.filter(mapsets, pattern)
  163. return mapsets
  164. class Mapset(object):
  165. """Mapset ::
  166. >>> mapset = Mapset()
  167. >>> mapset
  168. Mapset('user1')
  169. >>> mapset.gisdbase # doctest: +ELLIPSIS
  170. '/home/...'
  171. >>> mapset.location
  172. 'nc_basic_spm_grass7'
  173. >>> mapset.name
  174. 'user1'
  175. """
  176. def __init__(self, mapset='', location='', gisdbase=''):
  177. self.gisdbase = gisdbase
  178. self.location = location
  179. self.name = mapset
  180. def _get_gisdb(self):
  181. return self._gisdb
  182. def _set_gisdb(self, gisdb):
  183. self._gisdb = _check(gisdb, '', "GISDBASE")
  184. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
  185. def _get_loc(self):
  186. return self._loc
  187. def _set_loc(self, loc):
  188. self._loc = _check(loc, self._gisdb, "LOCATION_NAME")
  189. location = property(fget=_get_loc, fset=_set_loc)
  190. def _get_name(self):
  191. return self._name
  192. def _set_name(self, name):
  193. self._name = _check(name, join(self._gisdb, self._loc), "MAPSET")
  194. name = property(fget=_get_name, fset=_set_name)
  195. def __str__(self):
  196. return self.name
  197. def __repr__(self):
  198. return 'Mapset(%r)' % self.name
  199. def glist(self, type, pattern=None):
  200. """Return a list of grass types like:
  201. * 'asciivect',
  202. * 'group',
  203. * 'icon',
  204. * 'labels',
  205. * 'oldvect',
  206. * 'rast',
  207. * 'rast3d',
  208. * 'region',
  209. * 'region3d',
  210. * 'sites',
  211. * 'vect',
  212. * 'view3d'
  213. ::
  214. >>> mapset = Mapset('PERMANENT')
  215. >>> rast = mapset.glist('rast')
  216. >>> rast.sort()
  217. >>> rast # doctest: +ELLIPSIS
  218. ['basins', 'elevation', ...]
  219. >>> mapset.glist('rast', pattern='el*')
  220. ['elevation_shade', 'elevation']
  221. """
  222. if type not in ETYPE:
  223. str_err = "Type %s is not valid, valid types are: %s."
  224. raise TypeError(str_err % (type, ', '.join(ETYPE.keys())))
  225. clist = libgis.G_list(ETYPE[type], self.gisdbase,
  226. self.location, self.name)
  227. elist = []
  228. for el in clist:
  229. el_name = ct.cast(el, ct.c_char_p).value
  230. if el_name:
  231. elist.append(el_name)
  232. else:
  233. if pattern:
  234. return fnmatch.filter(elist, pattern)
  235. return elist
  236. def is_current(self):
  237. return (self.name == libgis.G_getenv('MAPSET') and
  238. self.location == libgis.G_getenv('LOCATION_NAME') and
  239. self.gisdbase == libgis.G_getenv('GISDBASE'))
  240. def current(self):
  241. """Set the mapset as current"""
  242. set_current_mapset(self.name, self.location, self.gisdbase)
  243. def delete(self):
  244. """Delete the mapset"""
  245. if self.is_current():
  246. raise GrassError('The mapset is in use.')
  247. shutil.rmtree(join(self.gisdbase, self.location, self.name))