functions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(oldname, maptype):
  23. """Remove a map"""
  24. grasscore.run_command('g.remove', **{maptype: '{old}'.format(old=oldname)})
  25. def rename(oldname, newname, maptype):
  26. """Rename a map"""
  27. grasscore.run_command('g.rename',
  28. **{maptype: '{old},{new}'.format(old=oldname,
  29. new=newname), })
  30. def copy(existingmap, newmap, maptype):
  31. """Copy a map
  32. >>> copy('census', 'mycensus', 'vect')
  33. >>> rename('mycensus', 'mynewcensus', 'vect')
  34. >>> remove('mynewcensus', 'vect')
  35. """
  36. grasscore.run_command('g.copy',
  37. **{maptype: '{old},{new}'.format(old=existingmap,
  38. new=newmap), })
  39. def getenv(env):
  40. """Return the current grass environment variables ::
  41. >>> getenv("MAPSET")
  42. 'user1'
  43. """
  44. return libgis.G__getenv(env)
  45. def get_mapset_raster(mapname, mapset=''):
  46. """Return the mapset of the raster map ::
  47. >>> get_mapset_raster('elevation')
  48. 'PERMANENT'
  49. """
  50. return libgis.G_find_raster(mapname, '')
  51. def get_mapset_vector(mapname, mapset=''):
  52. """Return the mapset of the vector map ::
  53. >>> get_mapset_vector('census')
  54. 'PERMANENT'
  55. """
  56. return libgis.G_find_vector(mapname, '')
  57. def is_clean_name(name):
  58. """Return if the name is valid ::
  59. >>> is_clean_name('census')
  60. True
  61. >>> is_clean_name('0census')
  62. False
  63. >>> is_clean_name('census&')
  64. False
  65. """
  66. if name[0].isdigit():
  67. return False
  68. for char in ' @#^?°,;%&/':
  69. if name.find(char) != -1:
  70. return False
  71. return True
  72. def coor2pixel((east, north), region):
  73. """Convert coordinates into a pixel row and col ::
  74. >>> reg = Region()
  75. >>> coor2pixel((reg.west, reg.north), reg)
  76. (0.0, 0.0)
  77. >>> coor2pixel((reg.east, reg.south), reg) == (reg.rows, reg.cols)
  78. True
  79. """
  80. return (libraster.Rast_northing_to_row(north, region.c_region),
  81. libraster.Rast_easting_to_col(east, region.c_region))
  82. def pixel2coor((col, row), region):
  83. """Convert row and col of a pixel into a coordinates ::
  84. >>> reg = Region()
  85. >>> pixel2coor((0, 0), reg) == (reg.north, reg.west)
  86. True
  87. >>> pixel2coor((reg.cols, reg.rows), reg) == (reg.south, reg.east)
  88. True
  89. """
  90. return (libraster.Rast_row_to_northing(row, region.c_region),
  91. libraster.Rast_col_to_easting(col, region.c_region))
  92. def get_raster_for_points(point, raster):
  93. """Query a raster map for each point feature of a vector
  94. Parameters
  95. -------------
  96. point: point vector object
  97. raster: raster object
  98. """
  99. reg = Region()
  100. if not point.is_open():
  101. point.open()
  102. if point.num_primitive_of('point') == 0:
  103. raise GrassError(_("Vector doesn't contain points"))
  104. values = [raster.get_value(poi.coords, reg) for poi in point.viter('point')]
  105. return values