dtsplitmultipart.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. """
  3. /***************************************************************************
  4. DigitizingTools
  5. A QGIS plugin
  6. Subsumes different tools useful during digitizing sessions
  7. Tool: SplitMultipart features into single part
  8. integrated into DigitizingTools by Bernhard Ströbl
  9. -------------------
  10. begin : 2013-01-17
  11. copyright : (C) 2013 by Alexandre Neto
  12. email : senhor.neto@gmail.com
  13. ***************************************************************************/
  14. /***************************************************************************
  15. * *
  16. * This program is free software; you can redistribute it and/or modify *
  17. * it under the terms of the GNU General Public License as published by *
  18. * the Free Software Foundation; either version 2 of the License, or *
  19. * (at your option) any later version. *
  20. * *
  21. ***************************************************************************/
  22. """
  23. from qgis.PyQt import QtCore, QtGui
  24. from qgis.core import *
  25. import dtutils
  26. import dt_icons_rc
  27. from dttools import DtDualToolSelectFeature
  28. class DtSplitMultiPartTool(DtDualToolSelectFeature):
  29. def __init__(self, iface, toolBar):
  30. super().__init__(iface, toolBar,
  31. QtGui.QIcon(":/MultiToSingle.png"),
  32. QtCore.QCoreApplication.translate("digitizingtools", "Split multi-part feature to single part (interactive mode)"),
  33. QtGui.QIcon(":/MultiToSingleBatch.png"),
  34. QtCore.QCoreApplication.translate("digitizingtools", "Split selected multi-part features to single part"),
  35. geometryTypes = [1, 2, 3, 4, 5, 6], dtName = "dtSplitMultiPart")
  36. def process(self):
  37. layer = self.iface.mapCanvas().currentLayer()
  38. newFeatures = []
  39. if layer.selectedFeatureCount() == 1:
  40. editCommand = QtCore.QCoreApplication.translate("editcommand", "Split feature")
  41. elif layer.selectedFeatureCount() > 1:
  42. editCommand = QtCore.QCoreApplication.translate("editcommand", "Split features")
  43. for feature in layer.selectedFeatures():
  44. geom = QgsGeometry(feature.geometry())
  45. if not geom.isGeosValid():
  46. thisWarning = dtutils.dtGetInvalidGeomWarning(layer)
  47. dtutils.dtShowWarning(self.iface, thisWarning)
  48. continue
  49. # if feature geometry is multipart starts split processing
  50. if geom.isMultipart():
  51. if len(newFeatures) == 0:
  52. layer.beginEditCommand(editCommand)
  53. # Get parts from original feature
  54. parts = geom.asGeometryCollection ()
  55. # update feature geometry to hold first part single geometry
  56. # (this way one of the output feature keeps the original Id)
  57. feature.setGeometry(parts.pop(0))
  58. layer.updateFeature(feature)
  59. # create new features from parts and add them to the list of newFeatures
  60. newFeatures = newFeatures + dtutils.dtMakeFeaturesFromGeometries(layer, feature, parts)
  61. # add new features to layer
  62. if len(newFeatures) > 0:
  63. layer.addFeatures(newFeatures)
  64. layer.endEditCommand()