dtfillring.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. """
  3. dtcutter
  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. import dt_icons_rc
  20. import dtutils
  21. from dttools import DtDualToolSelectRing
  22. class DtFillRing(DtDualToolSelectRing):
  23. '''Fill selected ring/all rings in selected feature in active polygon layer'''
  24. def __init__(self, iface, toolBar):
  25. super().__init__(iface, toolBar,
  26. QtGui.QIcon(":/fillRing.png"),
  27. QtCore.QCoreApplication.translate("digitizingtools", "Fill ring with a new feature (interactive mode)"),
  28. QtGui.QIcon(":/fillRingBatch.png"),
  29. QtCore.QCoreApplication.translate("digitizingtools", "Fill all rings in selected polygons with new features"),
  30. geometryTypes = [3, 6], dtName = "dtFillRing")
  31. self.newFid = None
  32. def ringFound(self, selectRingResult):
  33. layer = self.iface.activeLayer()
  34. thisRing = selectRingResult[0]
  35. defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
  36. layer.beginEditCommand(QtCore.QCoreApplication.translate(
  37. "editcommand", "Fill ring"))
  38. if self.iface.vectorLayerTools().addFeature(layer,
  39. defaultValues = defaultAttributeMap,
  40. defaultGeometry = thisRing):
  41. layer.endEditCommand()
  42. self.canvas.refresh()
  43. else:
  44. layer.destroyEditCommand()
  45. self.tool.reset()
  46. def process(self):
  47. layer = self.iface.activeLayer()
  48. layer.featureAdded.connect(self.featureAdded)
  49. numRingsFilled = 0
  50. aborted = False
  51. for featureToFill in layer.selectedFeatures():
  52. geom = QgsGeometry(featureToFill.geometry())
  53. if not geom.isGeosValid():
  54. thisWarning = dtutils.dtGetInvalidGeomWarning(layer)
  55. dtutils.dtShowWarning(self.iface, thisWarning)
  56. continue
  57. rings = dtutils.dtExtractRings(geom)
  58. for aRing in rings:
  59. if numRingsFilled == 0:
  60. defaultAttributeMap = dtutils.dtGetDefaultAttributeMap(layer)
  61. layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Fill rings"))
  62. if self.iface.vectorLayerTools().addFeature(layer, defaultValues = defaultAttributeMap, defaultGeometry = aRing):
  63. layer.featureAdded.disconnect(self.featureAdded)
  64. else:
  65. layer.featureAdded.disconnect(self.featureAdded)
  66. layer.destroyEditCommand()
  67. aborted = True
  68. break
  69. else:
  70. aFeat = dtutils.dtCopyFeature(layer, srcFid = self.newFid)
  71. aFeat.setGeometry(aRing)
  72. layer.addFeature(aFeat)
  73. numRingsFilled += 1
  74. if aborted:
  75. break
  76. layer.endEditCommand()
  77. self.canvas.refresh()
  78. def featureAdded(self, newFid):
  79. self.newFid = newFid