v.dissolve.py 3.4 KB

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