v.dissolve.py 3.5 KB

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