utils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. # -*- coding: utf-8 -*-
  2. import itertools
  3. import fnmatch
  4. import os
  5. from sqlite3 import OperationalError
  6. import grass.lib.gis as libgis
  7. libgis.G_gisinit('')
  8. import grass.lib.raster as libraster
  9. from grass.script import core as grasscore
  10. from grass.pygrass.errors import GrassError
  11. def looking(obj, filter_string):
  12. """
  13. >>> import grass.lib.vector as libvect
  14. >>> sorted(looking(libvect, '*by_box*')) # doctest: +NORMALIZE_WHITESPACE
  15. ['Vect_select_areas_by_box', 'Vect_select_isles_by_box',
  16. 'Vect_select_lines_by_box', 'Vect_select_nodes_by_box']
  17. """
  18. word_list = dir(obj)
  19. word_list.sort()
  20. return fnmatch.filter(word_list, filter_string)
  21. def findfiles(dirpath, match=None):
  22. """Return a list of the files"""
  23. res = []
  24. for f in sorted(os.listdir(dirpath)):
  25. abspath = os.path.join(dirpath, f)
  26. if os.path.isdir(abspath):
  27. res.extend(findfiles(abspath, match))
  28. if match:
  29. if fnmatch.fnmatch(abspath, match):
  30. res.append(abspath)
  31. else:
  32. res.append(abspath)
  33. return res
  34. def findmaps(type, pattern=None, mapset='', location='', gisdbase=''):
  35. """Return a list of tuple contining the names of the:
  36. * map
  37. * mapset,
  38. * location,
  39. * gisdbase
  40. """
  41. from grass.pygrass.gis import Gisdbase, Location, Mapset
  42. def find_in_location(type, pattern, location):
  43. res = []
  44. for msetname in location.mapsets():
  45. mset = Mapset(msetname, location.name, location.gisdbase)
  46. res.extend([(m, mset.name, mset.location, mset.gisdbase)
  47. for m in mset.glist(type, pattern)])
  48. return res
  49. def find_in_gisdbase(type, pattern, gisdbase):
  50. res = []
  51. for loc in gisdbase.locations():
  52. res.extend(find_in_location(type, pattern,
  53. Location(loc, gisdbase.name)))
  54. return res
  55. if gisdbase and location and mapset:
  56. mset = Mapset(mapset, location, gisdbase)
  57. return [(m, mset.name, mset.location, mset.gisdbase)
  58. for m in mset.glist(type, pattern)]
  59. elif gisdbase and location:
  60. loc = Location(location, gisdbase)
  61. return find_in_location(type, pattern, loc)
  62. elif gisdbase:
  63. gis = Gisdbase(gisdbase)
  64. return find_in_gisdbase(type, pattern, gis)
  65. elif location:
  66. loc = Location(location)
  67. return find_in_location(type, pattern, loc)
  68. elif mapset:
  69. mset = Mapset(mapset)
  70. return [(m, mset.name, mset.location, mset.gisdbase)
  71. for m in mset.glist(type, pattern)]
  72. else:
  73. gis = Gisdbase()
  74. return find_in_gisdbase(type, pattern, gis)
  75. def remove(oldname, maptype):
  76. """Remove a map"""
  77. grasscore.run_command('g.remove', quiet=True, flags='f',
  78. type=maptype, name=oldname)
  79. def rename(oldname, newname, maptype, **kwargs):
  80. """Rename a map"""
  81. kwargs.update({maptype: '{old},{new}'.format(old=oldname, new=newname), })
  82. grasscore.run_command('g.rename', quiet=True, **kwargs)
  83. def copy(existingmap, newmap, maptype, **kwargs):
  84. """Copy a map
  85. >>> copy('census', 'mycensus', 'vect')
  86. >>> rename('mycensus', 'mynewcensus', 'vect')
  87. >>> remove('mynewcensus', 'vect')
  88. """
  89. kwargs.update({maptype: '{old},{new}'.format(old=existingmap, new=newmap)})
  90. grasscore.run_command('g.copy', quiet=True, **kwargs)
  91. def getenv(env):
  92. """Return the current grass environment variables
  93. >>> getenv("MAPSET")
  94. 'user1'
  95. """
  96. return libgis.G_getenv_nofatal(env)
  97. def get_mapset_raster(mapname, mapset=''):
  98. """Return the mapset of the raster map
  99. >>> get_mapset_raster('elevation')
  100. 'PERMANENT'
  101. """
  102. return libgis.G_find_raster2(mapname, mapset)
  103. def get_mapset_vector(mapname, mapset=''):
  104. """Return the mapset of the vector map
  105. >>> get_mapset_vector('census')
  106. 'PERMANENT'
  107. """
  108. return libgis.G_find_vector2(mapname, mapset)
  109. def is_clean_name(name):
  110. """Return if the name is valid
  111. >>> is_clean_name('census')
  112. True
  113. >>> is_clean_name('0census')
  114. True
  115. >>> is_clean_name('census?')
  116. False
  117. >>> is_clean_name('cénsus')
  118. False
  119. """
  120. if libgis.G_legal_filename(name) < 0:
  121. return False
  122. return True
  123. def coor2pixel(coord, region):
  124. """Convert coordinates into a pixel row and col
  125. >>> reg = Region()
  126. >>> coor2pixel((reg.west, reg.north), reg)
  127. (0.0, 0.0)
  128. >>> coor2pixel((reg.east, reg.south), reg) == (reg.rows, reg.cols)
  129. True
  130. """
  131. (east, north) = coord
  132. return (libraster.Rast_northing_to_row(north, region.c_region),
  133. libraster.Rast_easting_to_col(east, region.c_region))
  134. def pixel2coor(pixel, region):
  135. """Convert row and col of a pixel into a coordinates
  136. >>> reg = Region()
  137. >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
  138. True
  139. >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.south, reg.east)
  140. True
  141. """
  142. (col, row) = pixel
  143. return (libraster.Rast_row_to_northing(row, region.c_region),
  144. libraster.Rast_col_to_easting(col, region.c_region))
  145. def get_raster_for_points(poi_vector, raster, column=None, region=None):
  146. """Query a raster map for each point feature of a vector
  147. Example
  148. >>> from grass.pygrass.vector import VectorTopo
  149. >>> from grass.pygrass.raster import RasterRow
  150. >>> ele = RasterRow('elevation')
  151. >>> copy('schools','myschools','vect')
  152. >>> sch = VectorTopo('myschools')
  153. >>> sch.open(mode='r')
  154. >>> get_raster_for_points(sch, ele) # doctest: +ELLIPSIS
  155. [(1, 633649.2856743174, 221412.94434781274, 145.06602)...
  156. >>> sch.table.columns.add('elevation','double precision')
  157. >>> 'elevation' in sch.table.columns
  158. True
  159. >>> get_raster_for_points(sch, ele, 'elevation')
  160. True
  161. >>> sch.table.filters.select('NAMESHORT','elevation')
  162. Filters(u'SELECT NAMESHORT, elevation FROM myschools;')
  163. >>> cur = sch.table.execute()
  164. >>> cur.fetchall() # doctest: +ELLIPSIS
  165. [(u'SWIFT CREEK', 145.06602), ... (u'9TH GRADE CTR', None)]
  166. >>> remove('myschools','vect')
  167. :param point: point vector object
  168. :param raster: raster object
  169. :param str column: column name to update
  170. """
  171. from math import isnan
  172. if not column:
  173. result = []
  174. if region is None:
  175. from grass.pygrass.gis.region import Region
  176. region = Region()
  177. if not poi_vector.is_open():
  178. poi_vector.open()
  179. if not raster.is_open():
  180. raster.open()
  181. if poi_vector.num_primitive_of('point') == 0:
  182. raise GrassError(_("Vector doesn't contain points"))
  183. for poi in poi_vector.viter('points'):
  184. val = raster.get_value(poi, region)
  185. if column:
  186. if val is not None and not isnan(val):
  187. poi.attrs[column] = val
  188. else:
  189. if val is not None and not isnan(val):
  190. result.append((poi.id, poi.x, poi.y, val))
  191. else:
  192. result.append((poi.id, poi.x, poi.y, None))
  193. if not column:
  194. return result
  195. else:
  196. poi.attrs.commit()
  197. return True
  198. def r_export(rast, output='', fmt='png', **kargs):
  199. from grass.pygrass.modules import Module
  200. if rast.exist():
  201. output = output if output else "%s_%s.%s" % (rast.name, rast.mapset,
  202. fmt)
  203. Module('r.out.%s' % fmt, input=rast.fullname(), output=output,
  204. overwrite=True, **kargs)
  205. return output
  206. else:
  207. raise ValueError('Raster map does not exist.')
  208. def get_lib_path(modname, libname):
  209. """Return the path of the libname contained in the module.
  210. >>> get_lib_path(modname='r.modis', libname='libmodis')
  211. """
  212. from os.path import isdir, join
  213. from os import getenv
  214. if isdir(join(getenv('GISBASE'), 'etc', modname)):
  215. path = join(os.getenv('GISBASE'), 'etc', modname)
  216. elif getenv('GRASS_ADDON_BASE') and \
  217. isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname)):
  218. path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
  219. elif getenv('GRASS_ADDON_BASE') and \
  220. isdir(join(getenv('GRASS_ADDON_BASE'), modname, modname)):
  221. path = join(os.getenv('GRASS_ADDON_BASE'), modname, modname)
  222. elif isdir(join('..', libname)):
  223. path = join('..', libname)
  224. else:
  225. path = None
  226. return path
  227. def set_path(modulename, dirname, path='.'):
  228. import sys
  229. """Set sys.path looking in the the local directory GRASS directories."""
  230. pathlib = os.path.join(path, dirname)
  231. if os.path.exists(pathlib):
  232. # we are running the script from the script directory
  233. sys.path.append(os.path.abspath(path))
  234. else:
  235. # running from GRASS GIS session
  236. from grass.pygrass.utils import get_lib_path
  237. path = get_lib_path(modulename, dirname)
  238. if path is None:
  239. raise ImportError("Not able to find the path %s directory." % path)
  240. sys.path.append(path)
  241. def split_in_chunk(iterable, length=10):
  242. """Split a list in chunk.
  243. >>> for chunk in split_in_chunk(range(25)): print chunk
  244. (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  245. (10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
  246. (20, 21, 22, 23, 24)
  247. >>> for chunk in split_in_chunk(range(25), 3): print chunk
  248. (0, 1, 2)
  249. (3, 4, 5)
  250. (6, 7, 8)
  251. (9, 10, 11)
  252. (12, 13, 14)
  253. (15, 16, 17)
  254. (18, 19, 20)
  255. (21, 22, 23)
  256. (24,)
  257. """
  258. it = iter(iterable)
  259. while True:
  260. chunk = tuple(itertools.islice(it, length))
  261. if not chunk:
  262. return
  263. yield chunk
  264. def table_exist(cursor, table_name):
  265. """Return True if the table exist False otherwise"""
  266. try:
  267. # sqlite
  268. cursor.execute("SELECT name FROM sqlite_master"
  269. " WHERE type='table' AND name='%s';" % table_name)
  270. except OperationalError:
  271. try:
  272. # pg
  273. cursor.execute("SELECT EXISTS(SELECT * FROM "
  274. "information_schema.tables "
  275. "WHERE table_name=%s)" % table_name)
  276. except OperationalError:
  277. return False
  278. one = cursor.fetchone() if cursor else None
  279. return True if one and one[0] else False