functions.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 the mapset of the raster map ::
  40. >>> get_mapset_raster('elevation')
  41. 'PERMANENT'
  42. """
  43. return libgis.G_find_raster(mapname, '')
  44. def get_mapset_vector(mapname, mapset=''):
  45. """Return the mapset of the vector map ::
  46. >>> get_mapset_vector('census')
  47. 'PERMANENT'
  48. """
  49. return libgis.G_find_vector(mapname, '')
  50. def is_clean_name(name):
  51. """Return if the name is valid ::
  52. >>> is_clean_name('census')
  53. True
  54. >>> is_clean_name('0census')
  55. False
  56. >>> is_clean_name('census&')
  57. False
  58. """
  59. if name[0].isdigit():
  60. return False
  61. for char in ' @#^?°,;%&/':
  62. if name.find(char) != -1:
  63. return False
  64. return True
  65. def coor2pixel((east, north), region):
  66. """Convert coordinates into a pixel row and col ::
  67. >>> reg = Region()
  68. >>> coor2pixel((reg.west, reg.north), reg)
  69. (0.0, 0.0)
  70. >>> coor2pixel((reg.east, reg.south), reg) == (reg.rows, reg.cols)
  71. True
  72. """
  73. return (libraster.Rast_northing_to_row(north, region.c_region),
  74. libraster.Rast_easting_to_col(east, region.c_region))
  75. def pixel2coor((col, row), region):
  76. """Convert row and col of a pixel into a coordinates ::
  77. >>> reg = Region()
  78. >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
  79. True
  80. >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.south, reg.east)
  81. True
  82. """
  83. return (libraster.Rast_row_to_northing(row, region.c_region),
  84. libraster.Rast_col_to_easting(col, region.c_region))
  85. def get_raster_for_points(point, raster):
  86. """Query a raster map for each point feature of a vector
  87. Parameters
  88. -------------
  89. point: point vector object
  90. raster: raster object
  91. """
  92. reg = Region()
  93. if not point.is_open():
  94. point.open()
  95. if point.num_primitive_of('point') == 0:
  96. raise GrassError(_("Vector doesn't contain points"))
  97. values = [raster.get_value(poi.coords, reg) for poi in point.viter('point')]
  98. return values