dtclipper.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. """
  3. dtclipper
  4. `````````````
  5. """
  6. """
  7. Part of DigitizingTools, a QGIS plugin that
  8. subsumes different tools neded during digitizing sessions
  9. * begin : 2015-01-29
  10. * copyright : (C) 2015 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 DtDualToolSelectPolygon
  22. class DtClipWithPolygon(DtDualToolSelectPolygon):
  23. '''Clip from active editable layer with selected polygon from another layer'''
  24. def __init__(self, iface, toolBar):
  25. super().__init__(iface, toolBar,
  26. QtGui.QIcon(":/clipper.png"),
  27. QtCore.QCoreApplication.translate(
  28. "digitizingtools", "Clip with polygon (interactive)"),
  29. QtGui.QIcon(":/clipper_batch.png"),
  30. QtCore.QCoreApplication.translate(
  31. "digitizingtools", "Clip with selected polygons"),
  32. geometryTypes = [3, 6], dtName = "dtClipper")
  33. self.enable()
  34. def process(self):
  35. '''Function that does all the real work'''
  36. title = QtCore.QCoreApplication.translate(
  37. "digitizingtools", "Clipper")
  38. processLayer = self.iface.activeLayer()
  39. msgLst = dtutils.dtGetNoSelMessage()
  40. noSelMsg1 = msgLst[0]
  41. if processLayer.selectedFeatureCount() == 0:
  42. self.iface.messageBar().pushMessage(
  43. title, noSelMsg1 + " " + processLayer.name())
  44. return None
  45. else:
  46. clipperGeoms = []
  47. for feat in processLayer.selectedFeatures():
  48. clipperGeom = feat.geometry()
  49. if not clipperGeom.isGeosValid():
  50. thisWarning = dtutils.dtGetInvalidGeomWarning(processLayer)
  51. dtutils.dtShowWarning(self.iface, thisWarning)
  52. continue
  53. else:
  54. clipperGeoms.append(clipperGeom)
  55. if len(clipperGeoms) == 0:
  56. return None # could be only invalid geoms selected
  57. processLayer.invertSelection()
  58. idsToProcess = []
  59. processLayer.beginEditCommand(
  60. QtCore.QCoreApplication.translate(
  61. "editcommand", "Clip Features"
  62. ))
  63. featuresBeingClipped = 0
  64. for aFeat in processLayer.selectedFeatures():
  65. idsToProcess.append(aFeat.id())
  66. for clipperGeom in clipperGeoms:
  67. bbox = clipperGeom.boundingBox()
  68. processLayer.selectByRect(bbox) # make a new selection
  69. for selFeat in processLayer.selectedFeatures():
  70. if idsToProcess.count(selFeat.id()) == 0:
  71. continue
  72. selGeom = selFeat.geometry()
  73. if not selGeom.isGeosValid():
  74. thisWarning = dtutils.dtGetInvalidGeomWarning(processLayer)
  75. dtutils.dtShowWarning(self.iface, thisWarning)
  76. continue
  77. if clipperGeom.intersects(selGeom): # we have a candidate
  78. newGeom = selGeom.intersection(clipperGeom)
  79. if newGeom != None:
  80. if not newGeom.isEmpty():
  81. if newGeom.area() > 0:
  82. selFeat.setGeometry(newGeom)
  83. processLayer.updateFeature(selFeat)
  84. featuresBeingClipped += 1
  85. processLayer.removeSelection()
  86. self.iface.mapCanvas().refresh()
  87. if featuresBeingClipped == 0:
  88. processLayer.destroyEditCommand()
  89. else:
  90. processLayer.endEditCommand()