v.dissolve.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #% description: Layer number. If -1, all layers are extracted
  41. #% answer: 1
  42. #% required : no
  43. #%end
  44. #%option
  45. #% key: column
  46. #% type: string
  47. #% description: Name of column used to dissolve common boundaries
  48. #% required : no
  49. #%end
  50. import sys
  51. import os
  52. import grass
  53. import atexit
  54. def cleanup():
  55. nuldev = file(os.devnull, 'w')
  56. grass.run_command('g.remove', vect = '%s_%s' % (output, tmp), quiet = True, stderr = nuldev)
  57. def main():
  58. global output, tmp
  59. input = options['input']
  60. output = options['output']
  61. layer = options['layer']
  62. column = options['column']
  63. # save command line
  64. cmdline = grass.basename(sys.argv[0])
  65. cmdline += ' input=' + input
  66. cmdline += ' output=' + output
  67. if layer:
  68. cmdline += ' layer=' + layer
  69. if column:
  70. cmdline += ' column=' + column
  71. #### setup temporary file
  72. tmp = str(os.getpid())
  73. # does map exist?
  74. if not grass.find_file(input, element = 'vector')['file']:
  75. grass.fatal("Vector map '$GIS_OPT_INPUT' not found in mapset search path")
  76. if not column:
  77. grass.run_command('v.extract', flags = 'd', input = input,
  78. output = output, type = 'area', layer = layer)
  79. else:
  80. coltype = ''
  81. s = grass.read_command('v.info', flags = 'c', map = input, quiet = True)
  82. for line in s.splitlines():
  83. f = line.split('|')
  84. if len(f) > 1 and f[1] == column:
  85. coltype = f[0]
  86. if coltype not in ['INTEGER', 'CHARACTER']:
  87. grass.fatal("Key column must be of type integer or string")
  88. s = grass.read_command('v.db.connect', flags = 'g', map = input, layer = layer)
  89. table = s.split()[1]
  90. if not table:
  91. grass.fatal("There is no table connected to this map")
  92. tmpfile = '%s_%s' % (output, tmp)
  93. grass.run_command('v.reclass', input = input, output = tmpfile,
  94. layer = layer, column = column)
  95. grass.run_command('v.extract', flags = 'd', input = tmpfile,
  96. output = output, type = 'area', layer = layer)
  97. # write cmd history:
  98. grass.run_command('v.support', map = output, cmdhist = cmdline)
  99. if __name__ == "__main__":
  100. options, flags = grass.parser()
  101. atexit.register(cleanup)
  102. main()