v.dissolve.py 3.2 KB

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