v.dissolve.py 3.7 KB

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