v.dissolve.py 3.6 KB

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