functions.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 pygrass.errors import GrassError
  11. from pygrass.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. >>> from pygrass.region import Region
  59. >>> reg = Region()
  60. >>> coor2pixel((reg.west, reg.north), reg)
  61. (0.0, 0.0)
  62. >>> coor2pixel((reg.east, reg.south), reg) == (reg.cols, reg.rows)
  63. True
  64. """
  65. return (libraster.Rast_northing_to_row(north, region.c_region),
  66. libraster.Rast_easting_to_col(east, region.c_region))
  67. def pixel2coor((col, row), region):
  68. """Convert row and col of a pixel into a coordinates ::
  69. >>> from pygrass.region import Region
  70. >>> reg = Region()
  71. >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
  72. True
  73. >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.east, reg.south)
  74. True
  75. """
  76. return (libraster.Rast_row_to_northing(row, region.c_region),
  77. libraster.Rast_col_to_easting(col, region.c_region))
  78. def get_raster_for_points(point, raster):
  79. """Query a raster map for each point feature of a vector
  80. Parameters
  81. -------------
  82. point: point vector object
  83. raster: raster object
  84. """
  85. reg = Region()
  86. if not point.is_open():
  87. point.open()
  88. if point.num_primitive_of('point') == 0:
  89. raise GrassError(_("Vector doesn't contain points"))
  90. values = [raster.get_value(poi.coords, reg) for poi in point.viter('point')]
  91. return values