dtflipline.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. """
  3. dtflipline
  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. from dttools import DtDualToolSelectFeature
  22. class DtFlipLine(DtDualToolSelectFeature):
  23. '''Flip line direction tool'''
  24. def __init__(self, iface, toolBar):
  25. super().__init__(iface, toolBar,
  26. QtGui.QIcon(":/flipLine.png"),
  27. QtCore.QCoreApplication.translate("digitizingtools", "Flip line (interactive mode)"),
  28. QtGui.QIcon(":/flipLineBatch.png"),
  29. QtCore.QCoreApplication.translate("digitizingtools", "Flip selected lines"),
  30. geometryTypes = [2, 5], dtName = "dtFlipLine")
  31. def process(self):
  32. '''algorythm taken from Nathan Woodrow's Swap Line Direction see
  33. http://gis.stackexchange.com/questions/9261/how-can-i-switch-line-direction-in-qgis
  34. adapted to use with MultiPolylines '''
  35. layer = self.iface.activeLayer()
  36. if layer.selectedFeatureCount() == 1:
  37. layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Flip line"))
  38. elif layer.selectedFeatureCount() > 1:
  39. layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Flip lines"))
  40. else:
  41. return None
  42. hadError = False
  43. for feat in layer.selectedFeatures():
  44. geom = QgsGeometry(feat.geometry())
  45. if not geom.isGeosValid():
  46. thisWarning = dtutils.dtGetInvalidGeomWarning(layer)
  47. dtutils.dtShowWarning(self.iface, thisWarning)
  48. continue
  49. if layer.wkbType() == 2 or layer.wkbType() == -2147483646:
  50. nodes = geom.asPolyline()
  51. rNodes = self.reverse(nodes)
  52. newNodes = rNodes
  53. elif layer.wkbType() == 5 or layer.wkbType() ==-2147483643:
  54. newNodes = []
  55. for aLine in geom.asGeometryCollection():
  56. aNodes = aLine.asPolyline()
  57. rNodes = self.reverse(aNodes)
  58. newNodes.append(rNodes)
  59. else: # should not happen as tool is deactivated in all other cases
  60. newNodes = []
  61. if layer.wkbType() == 2 or layer.wkbType() == -2147483646:
  62. newGeom = QgsGeometry.fromPolylineXY(newNodes)
  63. else:
  64. newGeom = QgsGeometry.fromMultiPolylineXY(newNodes)
  65. if not layer.changeGeometry(feat.id(), newGeom):
  66. hadError = True
  67. if hadError:
  68. self.iface.messageBar().pushCritical(QtCore.QCoreApplication.translate("digitizingtools",
  69. "An error occured during flipping"))
  70. layer.destroyEditCommand()
  71. else:
  72. layer.endEditCommand()
  73. self.canvas.refresh()
  74. def reverse(self, nodes):
  75. '''reverse the order in array nodes
  76. nodes.reverse does not work with 25D geometries'''
  77. rNodes = []
  78. while len(nodes) > 0:
  79. rNodes.append(nodes.pop())
  80. return rNodes