v.dissolve.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 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, area, dissolve
  20. #%End
  21. #%option
  22. #% key: input
  23. #% type: string
  24. #% gisprompt: old,vector,vector
  25. #% key_desc: name
  26. #% description: Name of input vector map
  27. #% required: yes
  28. #%end
  29. #%option
  30. #% key: output
  31. #% type: string
  32. #% gisprompt: new,vector,vector
  33. #% key_desc: name
  34. #% description: Name for output vector map
  35. #% required: yes
  36. #%end
  37. #%option
  38. #% key: layer
  39. #% type: integer
  40. #% label: Layer number. If -1, all layers are extracted.
  41. #% description: A single vector map can be connected to multiple database tables. This number determines which table to use.
  42. #% answer: 1
  43. #% gisprompt: old_layer,layer,layer_all
  44. #% required : no
  45. #%end
  46. #%option
  47. #% key: column
  48. #% type: string
  49. #% description: Name of column used to dissolve common boundaries
  50. #% gisprompt: old_dbcolumn,dbcolumn,dbcolumn
  51. #% required : no
  52. #%end
  53. import sys
  54. import os
  55. from grass.script import core as grass
  56. import atexit
  57. def cleanup():
  58. nuldev = file(os.devnull, 'w')
  59. grass.run_command('g.remove', vect = '%s_%s' % (output, tmp), quiet = True, stderr = nuldev)
  60. def main():
  61. global output, tmp
  62. input = options['input']
  63. output = options['output']
  64. layer = options['layer']
  65. column = options['column']
  66. #### setup temporary file
  67. tmp = str(os.getpid())
  68. # does map exist?
  69. if not grass.find_file(input, element = 'vector')['file']:
  70. grass.fatal("Vector map <%s> not found" % input)
  71. if not column:
  72. grass.run_command('v.extract', flags = 'd', input = input,
  73. output = output, type = 'area', layer = layer)
  74. else:
  75. try:
  76. coltype = grass.vector_columns(input, layer)[column]
  77. except KeyError:
  78. grass.fatal('Column <%s> not found' % column)
  79. if coltype not in ('INTEGER', 'CHARACTER'):
  80. grass.fatal("Key column must be of type integer or string")
  81. f = grass.vector_layer_db(input, layer)
  82. table = f['table']
  83. tmpfile = '%s_%s' % (output, tmp)
  84. if grass.run_command('v.reclass', input = input, output = tmpfile,
  85. layer = layer, column = column) == 0:
  86. grass.run_command('v.extract', flags = 'd', input = tmpfile,
  87. output = output, type = 'area', layer = layer)
  88. # write cmd history:
  89. grass.vector_history(output)
  90. if __name__ == "__main__":
  91. options, flags = grass.parser()
  92. atexit.register(cleanup)
  93. main()