v.dissolve.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.dissolve
  5. # AUTHOR: M. Hamish Bowman, Dept. Marine Science, Otago Univeristy,
  6. # New Zealand
  7. # Markus Neteler for column support
  8. # Converted to Python by Glynn Clements
  9. # PURPOSE: Dissolve common boundaries between areas with common cat
  10. # (frontend to v.extract -d)
  11. # COPYRIGHT: (c) 2006 Hamish Bowman, and the GRASS Development Team
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Dissolves boundaries between adjacent areas sharing a common category number or attribute.
  19. #% keywords: vector
  20. #% keywords: area
  21. #% keywords: dissolve
  22. #%end
  23. #%option G_OPT_V_INPUT
  24. #%end
  25. #%option G_OPT_V_FIELD_ALL
  26. #% label: Layer number or name. If -1, all layers are extracted.
  27. #% required: no
  28. #%end
  29. #%option G_OPT_DB_COLUMN
  30. #% description: Name of attribute column used to dissolve common boundaries
  31. #%end
  32. #%option G_OPT_V_OUTPUT
  33. #%end
  34. import sys
  35. import os
  36. import atexit
  37. import grass.script as grass
  38. def cleanup():
  39. nuldev = file(os.devnull, 'w')
  40. grass.run_command('g.remove', vect = '%s_%s' % (output, tmp), quiet = True, stderr = nuldev)
  41. def main():
  42. global output, tmp
  43. input = options['input']
  44. output = options['output']
  45. layer = options['layer']
  46. column = options['column']
  47. #### setup temporary file
  48. tmp = str(os.getpid())
  49. # does map exist?
  50. if not grass.find_file(input, element = 'vector')['file']:
  51. grass.fatal(_("Vector map <%s> not found") % input)
  52. if not column:
  53. grass.run_command('v.extract', flags = 'd', input = input,
  54. output = output, type = 'area', layer = layer)
  55. else:
  56. try:
  57. coltype = grass.vector_columns(input, layer)[column]
  58. except KeyError:
  59. grass.fatal(_('Column <%s> not found') % column)
  60. if coltype['type'] not in ('INTEGER', 'CHARACTER'):
  61. grass.fatal(_("Key column must be of type integer or string"))
  62. f = grass.vector_layer_db(input, layer)
  63. table = f['table']
  64. tmpfile = '%s_%s' % (output, tmp)
  65. if grass.run_command('v.reclass', input = input, output = tmpfile,
  66. layer = layer, column = column) == 0:
  67. grass.run_command('v.extract', flags = 'd', input = tmpfile,
  68. output = output, type = 'area', layer = layer)
  69. # write cmd history:
  70. grass.vector_history(output)
  71. if __name__ == "__main__":
  72. options, flags = grass.parser()
  73. atexit.register(cleanup)
  74. main()