v.dissolve.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from grass.exceptions import CalledModuleError
  39. def cleanup():
  40. nuldev = file(os.devnull, 'w')
  41. grass.run_command('g.remove', type='vect', name='%s_%s' % (output, tmp), quiet=True, flags='f', stderr=nuldev)
  42. def main():
  43. global output, tmp
  44. input = options['input']
  45. output = options['output']
  46. layer = options['layer']
  47. column = options['column']
  48. #### setup temporary file
  49. tmp = str(os.getpid())
  50. # does map exist?
  51. if not grass.find_file(input, element = 'vector')['file']:
  52. grass.fatal(_("Vector map <%s> not found") % input)
  53. if not column:
  54. grass.warning(_("No '%s' option specified. Dissolving based on category values from layer <%s>.") % \
  55. ("column", layer))
  56. grass.run_command('v.extract', flags = 'd', input = input,
  57. output = output, type = 'area', layer = layer)
  58. else:
  59. if int(layer) == -1:
  60. grass.warning(_("Invalid layer number (%d). "
  61. "Parameter '%s' specified, assuming layer '1'.") %
  62. (int(layer), 'column'))
  63. layer = '1'
  64. try:
  65. coltype = grass.vector_columns(input, layer)[column]
  66. except KeyError:
  67. grass.fatal(_('Column <%s> not found') % column)
  68. if coltype['type'] not in ('INTEGER', 'SMALLINT', 'CHARACTER', 'TEXT'):
  69. grass.fatal(_("Key column must be of type integer or string"))
  70. f = grass.vector_layer_db(input, layer)
  71. table = f['table']
  72. tmpfile = '%s_%s' % (output, tmp)
  73. try:
  74. grass.run_command('v.reclass', input=input, output=tmpfile,
  75. layer=layer, column=column)
  76. grass.run_command('v.extract', flags='d', input=tmpfile,
  77. output=output, type='area', layer=layer)
  78. except CalledModuleError, e:
  79. grass.fatal(_("Final extraction steps failed."
  80. " Check above error messages and"
  81. " see following details:\n%s") % e)
  82. # write cmd history:
  83. grass.vector_history(output)
  84. if __name__ == "__main__":
  85. options, flags = grass.parser()
  86. atexit.register(cleanup)
  87. main()