v.dissolve.py 3.5 KB

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