functions.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jun 26 12:38:48 2012
  4. @author: pietro
  5. """
  6. import fnmatch
  7. import grass.lib.gis as libgis
  8. import grass.lib.raster as libraster
  9. from grass.script import core as grasscore
  10. from grass.pygrass.errors import GrassError
  11. from grass.pygrass.gis.region import Region
  12. def looking(obj, filter_string):
  13. """
  14. >>> import grass.lib.vector as libvect
  15. >>> sorted(looking(libvect, '*by_box*')) # doctest: +NORMALIZE_WHITESPACE
  16. ['Vect_select_areas_by_box', 'Vect_select_isles_by_box',
  17. 'Vect_select_lines_by_box', 'Vect_select_nodes_by_box']
  18. """
  19. word_list = dir(obj)
  20. word_list.sort()
  21. return fnmatch.filter(word_list, filter_string)
  22. def remove(**kargs):
  23. grasscore.run_command('g.remove', **kargs)
  24. def rename(oldname, newname, maptype):
  25. grasscore.run_command('g.rename',
  26. **{maptype: '{old},{new}'.format(old=oldname,
  27. new=newname), })
  28. def copy(existingmap, newmap, maptype):
  29. grasscore.run_command('g.copy',
  30. **{maptype: '{old},{new}'.format(old=existingmap,
  31. new=newmap), })
  32. def getenv(env):
  33. """Return the current grass environment variables:
  34. >>> getenv("MAPSET")
  35. 'user1'
  36. .."""
  37. return libgis.G__getenv(env)
  38. def get_mapset_raster(mapname, mapset=''):
  39. return libgis.G_find_raster(mapname, '')
  40. def get_mapset_vector(mapname, mapset=''):
  41. return libgis.G_find_vector(mapname, '')
  42. def exist(mapname, mapset=''):
  43. mapset = get_mapset_raster(mapname, mapset)
  44. if mapset != '':
  45. return True
  46. else:
  47. mapset = get_mapset_vector(mapname, mapset)
  48. if mapset:
  49. return True
  50. return False
  51. def clean_map_name(name):
  52. name.strip()
  53. for char in ' @#^?°,;%&/':
  54. name = name.replace(char, '')
  55. return name
  56. def coor2pixel((east, north), region):
  57. """Convert coordinates into a pixel row and col ::
  58. >>> reg = Region()
  59. >>> coor2pixel((reg.west, reg.north), reg)
  60. (0.0, 0.0)
  61. >>> coor2pixel((reg.east, reg.south), reg) == (reg.cols, reg.rows)
  62. True
  63. """
  64. return (libraster.Rast_northing_to_row(north, region.c_region),
  65. libraster.Rast_easting_to_col(east, region.c_region))
  66. def pixel2coor((col, row), region):
  67. """Convert row and col of a pixel into a coordinates ::
  68. >>> reg = Region()
  69. >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
  70. True
  71. >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.east, reg.south)
  72. True
  73. """
  74. return (libraster.Rast_row_to_northing(row, region.c_region),
  75. libraster.Rast_col_to_easting(col, region.c_region))
  76. def get_raster_for_points(point, raster):
  77. """Query a raster map for each point feature of a vector
  78. Parameters
  79. -------------
  80. point: point vector object
  81. raster: raster object
  82. """
  83. reg = Region()
  84. if not point.is_open():
  85. point.open()
  86. if point.num_primitive_of('point') == 0:
  87. raise GrassError(_("Vector doesn't contain points"))
  88. values = [raster.get_value(poi.coords, reg) for poi in point.viter('point')]
  89. return values