__init__.py 7.5 KB

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