__init__.py 7.8 KB

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