functions.py 9.7 KB

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