dtfillgap.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. """
  3. dtfillgap
  4. `````````````
  5. """
  6. """
  7. Part of DigitizingTools, a QGIS plugin that
  8. subsumes different tools neded during digitizing sessions
  9. * begin : 2013-02-25
  10. * copyright : (C) 2013 by Bernhard Ströbl
  11. * email : bernhard.stroebl@jena.de
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. """
  17. from qgis.PyQt import QtCore, QtGui
  18. from qgis.core import *
  19. from qgis.gui import *
  20. import dt_icons_rc
  21. import dtutils
  22. from dttools import DtDualToolSelectGap, DtSingleTool, DtSelectGapTool
  23. class DtFillGap(DtDualToolSelectGap):
  24. '''Fill gaps between selected features of the active layer with new features'''
  25. def __init__(self, iface, toolBar):
  26. super().__init__(iface, toolBar,
  27. QtGui.QIcon(":/fillGap.png"),
  28. QtCore.QCoreApplication.translate("digitizingtools",
  29. "Fill gap with a new feature (interactive mode)"),
  30. QtGui.QIcon(":/fillGapBatch.png"),
  31. QtCore.QCoreApplication.translate("digitizingtools",
  32. "Fill all gaps between selected polygons with new features"),
  33. geometryTypes = [3, 6], dtName = "dtFillGap")
  34. self.newFid = None
  35. self.title = QtCore.QCoreApplication.translate("digitizingtools", "Fill gap")
  36. def gapFound(self, result):
  37. layer = self.iface.activeLayer()
  38. gap = result[0]
  39. defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
  40. layer.beginEditCommand(QtCore.QCoreApplication.translate(
  41. "editcommand", "Fill gap"))
  42. if self.iface.vectorLayerTools().addFeature(layer,
  43. defaultValues = defaultAttributeMap, defaultGeometry = gap):
  44. layer.endEditCommand()
  45. self.canvas.refresh()
  46. else:
  47. layer.destroyEditCommand()
  48. self.tool.reset()
  49. def process(self):
  50. # DtDualTool makes sure a selection exists
  51. layer = self.iface.activeLayer()
  52. multiGeom = dtutils.dtCombineSelectedPolygons(layer, self.iface)
  53. if multiGeom != None:
  54. rings = dtutils.dtExtractRings(multiGeom)
  55. if len(rings) == 0:
  56. self.iface.messageBar().pushWarning(self.title,
  57. QtCore.QCoreApplication.translate("digitizingtools",
  58. "There are no gaps between the polygons."))
  59. else:
  60. defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
  61. layer.featureAdded.connect(self.featureAdded)
  62. numRingsFilled = 0
  63. aborted = False
  64. for aRing in rings:
  65. if numRingsFilled == 0:
  66. layer.beginEditCommand(QtCore.QCoreApplication.translate(
  67. "editcommand", "Fill gaps"))
  68. if self.iface.vectorLayerTools().addFeature(
  69. layer, defaultValues = defaultAttributeMap, defaultGeometry = aRing):
  70. layer.featureAdded.disconnect(self.featureAdded)
  71. else:
  72. layer.featureAdded.disconnect(self.featureAdded)
  73. aborted = True
  74. break
  75. else:
  76. aFeat = dtutils.dtCopyFeature(layer, srcFid = self.newFid)
  77. aFeat.setGeometry(aRing)
  78. layer.addFeature(aFeat)
  79. numRingsFilled += 1
  80. if aborted:
  81. layer.destroyEditCommand()
  82. else:
  83. layer.endEditCommand()
  84. self.canvas.refresh()
  85. def featureAdded(self, newFid):
  86. self.newFid = newFid
  87. class DtFillGapAllLayers(DtSingleTool):
  88. '''Fill gaps between the polygons of all visible layers with new features'''
  89. def __init__(self, iface, toolBar):
  90. super().__init__(iface, toolBar,
  91. QtGui.QIcon(":/fillGapAll.png"),
  92. QtCore.QCoreApplication.translate("digitizingtools",
  93. "Fill gap between polygons of all visible layers with a new feature"),
  94. geometryTypes = [3, 6], dtName = "dtFillGapAll")
  95. self.tool = DtSelectGapTool(self.iface, True)
  96. self.tool.gapSelected.connect(self.gapFound)
  97. self.enable()
  98. def process(self):
  99. self.canvas.setMapTool(self.tool)
  100. self.act.setChecked(True)
  101. def gapFound(self, result):
  102. layer = self.iface.activeLayer()
  103. gap = result[0]
  104. defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
  105. layer.beginEditCommand(QtCore.QCoreApplication.translate(
  106. "editcommand", "Fill gap"))
  107. if self.iface.vectorLayerTools().addFeature(layer,
  108. defaultValues = defaultAttributeMap, defaultGeometry = gap):
  109. layer.endEditCommand()
  110. self.canvas.refresh()
  111. else:
  112. layer.destroyEditCommand()
  113. self.tool.reset()