dtextractpart.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. """
  3. dtextractpart
  4. `````````````
  5. """
  6. """
  7. Part of DigitizingTools, a QGIS plugin that
  8. subsumes different tools neded during digitizing sessions
  9. Tool: Extract an part of a multifart feature and add it as new feature
  10. * begin : 2014-07-09
  11. * copyright : (C) 2014 by Bernhard Ströbl
  12. * email : bernhard.stroebl@jena.de
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. """
  18. from qgis.PyQt import QtCore, QtGui
  19. from qgis.core import *
  20. import dtutils
  21. import dt_icons_rc
  22. from dttools import DtSingleTool, DtSelectPartTool
  23. class DtExtractPartTool(DtSingleTool):
  24. def __init__(self, iface, toolBar):
  25. super().__init__(iface, toolBar,
  26. QtGui.QIcon(":/ExtractPart.png"),
  27. QtCore.QCoreApplication.translate("digitizingtools",
  28. "Split off one part and add it as a new feature"),
  29. geometryTypes = [1, 2, 3, 4, 5, 6], dtName = "dtExtractPart")
  30. self.tool = DtSelectPartTool(self.iface)
  31. self.tool.partSelected.connect(self.partSelected)
  32. self.enable()
  33. def process(self):
  34. self.canvas.setMapTool(self.tool)
  35. self.act.setChecked(True)
  36. def partSelected(self, part):
  37. fid = part[0]
  38. partNumber = part[1]
  39. aPart = part[2]
  40. layer = self.iface.mapCanvas().currentLayer()
  41. feature = dtutils.dtGetFeatureForId(layer, fid)
  42. geom = QgsGeometry(feature.geometry())
  43. if geom.deletePart(partNumber):
  44. layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Extract part"))
  45. aFeat = dtutils.dtCopyFeature(layer, feature)
  46. aFeat.setGeometry(aPart)
  47. layer.addFeature(aFeat)
  48. feature.setGeometry(geom)
  49. layer.updateFeature(feature)
  50. layer.endEditCommand()
  51. self.canvas.refresh()
  52. self.tool.reset()