__init__.py 7.7 KB

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