__init__.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. >>> gisdbase = Gisdbase()
  54. >>> gisdbase.name # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
  55. '/home/...'
  56. """
  57. def __init__(self, gisdbase=''):
  58. self.name = gisdbase
  59. def _get_name(self):
  60. return self._name
  61. def _set_name(self, name):
  62. self._name = _check(name, '', "GISDBASE")
  63. name = property(fget=_get_name, fset=_set_name)
  64. def __str__(self):
  65. return self.name
  66. def __repr__(self):
  67. return 'Gisdbase(%s)' % self.name
  68. def __getitem__(self, location):
  69. """Return a Location object. ::
  70. >>> gisdbase = Gisdbase()
  71. >>> gisdbase['nc_basic_spm_grass7']
  72. Location('nc_basic_spm_grass7')
  73. ..
  74. """
  75. if location in self.locations():
  76. return Location(location, self.name)
  77. else:
  78. raise KeyError('Location: %s does not exist' % location)
  79. def __iter__(self):
  80. for loc in self.locations():
  81. yield Location(loc, self.name)
  82. def new_location(self):
  83. if libgis.G__make_location() != 0:
  84. raise GrassError("I cannot create a new mapset.")
  85. def locations(self):
  86. """Return a list of locations that are available in the gisdbase: ::
  87. >>> gisdbase = Gisdbase()
  88. >>> gisdbase.locations() # doctest: +ELLIPSIS
  89. [...]
  90. ..
  91. """
  92. locations = []
  93. for loc in listdir(self.name):
  94. if libgis.G_is_location(join(self.name, loc)):
  95. locations.append(loc)
  96. locations.sort()
  97. return locations
  98. class Location(object):
  99. """Location object ::
  100. >>> location = Location()
  101. >>> location # doctest: +ELLIPSIS
  102. Location(...)
  103. >>> location.gisdbase # doctest: +ELLIPSIS
  104. '/home/...'
  105. >>> location.name
  106. 'nc_basic_spm_grass7'
  107. """
  108. def __init__(self, location='', gisdbase=''):
  109. self.gisdbase = gisdbase
  110. self.name = location
  111. def _get_gisdb(self):
  112. return self._gisdb
  113. def _set_gisdb(self, gisdb):
  114. self._gisdb = _check(gisdb, '', "GISDBASE")
  115. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
  116. def _get_name(self):
  117. return self._name
  118. def _set_name(self, name):
  119. self._name = _check(name, self._gisdb, "LOCATION_NAME")
  120. name = property(fget=_get_name, fset=_set_name)
  121. def __getitem__(self, mapset):
  122. if mapset in self.mapsets():
  123. return Mapset(mapset)
  124. else:
  125. raise KeyError('Mapset: %s does not exist' % mapset)
  126. def __iter__(self):
  127. for mapset in libgis.G_available_mapsets():
  128. mapset_name = ct.cast(mapset, ct.c_char_p).value
  129. if mapset_name and libgis.G__mapset_permissions(mapset):
  130. yield mapset_name
  131. else:
  132. break
  133. def __len__(self):
  134. return len(self.mapsets())
  135. def __str__(self):
  136. return self.name
  137. def __repr__(self):
  138. return 'Location(%r)' % self.name
  139. def mapsets(self):
  140. """Return a list of the available mapsets. ::
  141. >>> location = Location()
  142. >>> location.mapsets()
  143. ['PERMANENT', 'user1']
  144. """
  145. return [mapset for mapset in self]
  146. def new_mapset(self, mapset):
  147. if libgis.G__make_mapset(self.gisdbase, self.location, mapset) != 0:
  148. raise GrassError("I cannot create a new mapset.")
  149. class Mapset(object):
  150. """Mapset ::
  151. >>> mapset = Mapset()
  152. >>> mapset
  153. Mapset('user1')
  154. >>> mapset.gisdbase # doctest: +ELLIPSIS
  155. '/home/...'
  156. >>> mapset.location
  157. 'nc_basic_spm_grass7'
  158. >>> mapset.name
  159. 'user1'
  160. """
  161. def __init__(self, mapset='', location='', gisdbase=''):
  162. self.gisdbase = gisdbase
  163. self.location = location
  164. self.name = mapset
  165. def _get_gisdb(self):
  166. return self._gisdb
  167. def _set_gisdb(self, gisdb):
  168. self._gisdb = _check(gisdb, '', "GISDBASE")
  169. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb)
  170. def _get_loc(self):
  171. return self._loc
  172. def _set_loc(self, loc):
  173. self._loc = _check(loc, self._gisdb, "LOCATION_NAME")
  174. location = property(fget=_get_loc, fset=_set_loc)
  175. def _get_name(self):
  176. return self._name
  177. def _set_name(self, name):
  178. self._name = _check(name, join(self._gisdb, self._loc), "MAPSET")
  179. name = property(fget=_get_name, fset=_set_name)
  180. def __str__(self):
  181. return self.name
  182. def __repr__(self):
  183. return 'Mapset(%r)' % self.name
  184. def glist(self, type, pattern=None):
  185. """Return a list of grass types like:
  186. * 'asciivect',
  187. * 'group',
  188. * 'icon',
  189. * 'labels',
  190. * 'oldvect',
  191. * 'rast',
  192. * 'rast3d',
  193. * 'region',
  194. * 'region3d',
  195. * 'sites',
  196. * 'vect',
  197. * 'view3d'
  198. ::
  199. >>> mapset = Mapset('PERMANENT')
  200. >>> rast = mapset.glist('rast')
  201. >>> rast.sort()
  202. >>> rast # doctest: +ELLIPSIS
  203. ['basins', 'elevation', ...]
  204. >>> mapset.glist('rast', pattern='el*')
  205. ['elevation_shade', 'elevation']
  206. """
  207. if type not in ETYPE:
  208. str_err = "Type %s is not valid, valid types are: %s."
  209. raise TypeError(str_err % (type, ', '.join(ETYPE.keys())))
  210. clist = libgis.G_list(ETYPE[type], self.gisdbase,
  211. self.location, self.name)
  212. elist = []
  213. for el in clist:
  214. el_name = ct.cast(el, ct.c_char_p).value
  215. if el_name:
  216. elist.append(el_name)
  217. else:
  218. if pattern:
  219. return fnmatch.filter(elist, pattern)
  220. return elist