v.dissolve.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-2014 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: dissolve
  21. #% keywords: area
  22. #%end
  23. #%option G_OPT_V_INPUT
  24. #%end
  25. #%option G_OPT_V_FIELD
  26. #% label: Layer number or name.
  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', type='vect', name='%s_%s' % (output, tmp), quiet=True, flags='f', 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.warning(_("No '%s' option specified. Dissolving based on category values from layer <%s>.") % \
  54. ("column", layer))
  55. grass.run_command('v.extract', flags = 'd', input = input,
  56. output = output, type = 'area', layer = layer)
  57. else:
  58. if int(layer) == -1:
  59. grass.warning(_("Invalid layer number (%d). "
  60. "Parameter '%s' specified, assuming layer '1'.") %
  61. (int(layer), 'column'))
  62. layer = '1'
  63. try:
  64. coltype = grass.vector_columns(input, layer)[column]
  65. except KeyError:
  66. grass.fatal(_('Column <%s> not found') % column)
  67. if coltype['type'] not in ('INTEGER', 'SMALLINT', 'CHARACTER', 'TEXT'):
  68. grass.fatal(_("Key column must be of type integer or string"))
  69. f = grass.vector_layer_db(input, layer)
  70. table = f['table']
  71. tmpfile = '%s_%s' % (output, tmp)
  72. if grass.run_command('v.reclass', input = input, output = tmpfile,
  73. layer = layer, column = column) == 0:
  74. grass.run_command('v.extract', flags = 'd', input = tmpfile,
  75. output = output, type = 'area', layer = layer)
  76. # write cmd history:
  77. grass.vector_history(output)
  78. if __name__ == "__main__":
  79. options, flags = grass.parser()
  80. atexit.register(cleanup)
  81. main()