v.dissolve.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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: 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', 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. if int(layer) == -1:
  57. grass.warning(_("Invalid layer number (%d). "
  58. "Parameter '%s' specified, assuming layer '1'.") %
  59. (int(layer), 'column'))
  60. layer = '1'
  61. try:
  62. coltype = grass.vector_columns(input, layer)[column]
  63. except KeyError:
  64. grass.fatal(_('Column <%s> not found') % column)
  65. if coltype['type'] not in ('INTEGER', 'SMALLINT', 'CHARACTER', 'TEXT'):
  66. grass.fatal(_("Key column must be of type integer or string"))
  67. f = grass.vector_layer_db(input, layer)
  68. table = f['table']
  69. tmpfile = '%s_%s' % (output, tmp)
  70. if grass.run_command('v.reclass', input = input, output = tmpfile,
  71. layer = layer, column = column) == 0:
  72. grass.run_command('v.extract', flags = 'd', input = tmpfile,
  73. output = output, type = 'area', layer = layer)
  74. # write cmd history:
  75. grass.vector_history(output)
  76. if __name__ == "__main__":
  77. options, flags = grass.parser()
  78. atexit.register(cleanup)
  79. main()