def showDialog(self): flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint # QgisGui.ModalDialogFlags self.gui = DtMoveSideByArea_Dialog(self.iface.mainWindow(), flags) self.gui.initGui() self.gui.show() QObject.connect(self.gui, SIGNAL("unsetTool()"), self.unsetTool) QObject.connect(self.gui, SIGNAL("moveSide()"), self.moveSide)
def showDialog(self): flags = QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowMaximizeButtonHint # QgisGui.ModalDialogFlags self.gui = DtMoveSideByArea_Dialog(self.iface.mainWindow(), flags) self.gui.initGui() self.gui.show() self.gui.unsetTool.connect(self.unsetTool) self.gui.moveSide.connect(self.moveSide)
class DtMoveSideByArea(): '''Parallel move polygon side in order to achieve a desired polygon area''' def __init__(self, iface, toolBar): # Save reference to the QGIS interface self.iface = iface self.canvas = self.iface.mapCanvas() self.gui = None self.multipolygon_detected = False # points of the selected segment # p1 is always the left point self.p1 = None self.p2 = None self.rb1 = QgsRubberBand(self.canvas, False) self.selected_feature = None #create action self.side_mover = QtGui.QAction(QtGui.QIcon(":/ParallelMovePolygonSideByArea.png"), QtCore.QCoreApplication.translate("digitizingtools", "Parallel move of polygon side to target area"), self.iface.mainWindow()) self.side_mover.triggered.connect(self.run) self.iface.currentLayerChanged.connect(self.enable) toolBar.addAction(self.side_mover) self.enable() self.tool = DtSelectSegmentTool(self.canvas, self.iface) def showDialog(self): flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint # QgisGui.ModalDialogFlags self.gui = DtMoveSideByArea_Dialog(self.iface.mainWindow(), flags) self.gui.initGui() self.gui.show() QObject.connect(self.gui, SIGNAL("unsetTool()"), self.unsetTool) QObject.connect(self.gui, SIGNAL("moveSide()"), self.moveSide) def enableSegmentTool(self): self.canvas.setMapTool(self.tool) #Connect to the DtSelectVertexTool QObject.connect(self.tool, SIGNAL("segmentFound(PyQt_PyObject)"), self.storeSegmentPoints) def unsetTool(self): self.p1 = None self.p2 = None self.selected_feature = None self.canvas.unsetMapTool(self.tool) def run(self): '''Function that does all the real work''' layer = self.iface.activeLayer() if(layer.dataProvider().geometryType() == 6): self.multipolygon_detected = True title = QtCore.QCoreApplication.translate("digitizingtools", "Move polygon side by area") if layer.selectedFeatureCount() == 0: QtGui.QMessageBox.information(None, title, QtCore.QCoreApplication.translate("digitizingtools", "Please select one polygon to edit.")) elif layer.selectedFeatureCount() > 1: QtGui.QMessageBox.information(None, title, QtCore.QCoreApplication.translate("digitizingtools", "Please select only one polygon to edit.")) else: #One selected feature self.selected_feature = layer.selectedFeatures()[0] self.enableSegmentTool() self.showDialog() self.gui.writeArea(self.selected_feature.geometry().area()) def storeSegmentPoints(self, result): if result[0].x() < result[1].x(): self.p1 = result[0] self.p2 = result[1] elif result[0].x() == result[1].x(): self.p1 = result[0] self.p2 = result[1] else: self.p1 = result[1] self.p2 = result[0] def enable(self): '''Enables/disables the corresponding button.''' # Disable the Button by default self.side_mover.setEnabled(False) layer = self.iface.activeLayer() if layer <> None: #Only for vector layers. if layer.type() == QgsMapLayer.VectorLayer: # only for polygon layers if layer.geometryType() == 2: # enable if editable self.side_mover.setEnabled(layer.isEditable()) try: layer.editingStarted.disconnect(self.enable) # disconnect, will be reconnected except: pass try: layer.editingStopped.disconnect(self.enable) # when it becomes active layer again except: pass layer.editingStarted.connect(self.enable) layer.editingStopped.connect(self.enable) def moveSide(self): new_a = -1.0 try: new_a = float(self.gui.targetArea.text()) except: pass if (new_a == -1.0): QMessageBox.information(None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate("digitizingtools", "Target Area not valid.")) return if self.p1 == None or self.p2 == None: QMessageBox.information(None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate("digitizingtools", "Polygon side not selected.")) else: touch_p1_p2 = self.selected_feature.geometry().touches(QgsGeometry.fromPolyline([self.p1, self.p2])) if (not touch_p1_p2): QMessageBox.information(None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate("digitizingtools", "Selected segment should be on the selected polygon.")) else: #Select tool to create new geometry here if self.gui.method == "fixed": new_geom = moveFixed(self.selected_feature.geometry(), self.p1, self.p2, new_a, self.multipolygon_detected) else: new_geom = moveVariable(self.selected_feature.geometry(), self.p1, self.p2, new_a, self.multipolygon_detected) #Store new geometry on the memory buffer fid = self.selected_feature.id() layer = self.iface.activeLayer() layer.beginEditCommand(QtCore.QCoreApplication.translate("editcommand", "Move Side By Area")) layer.changeGeometry(fid,new_geom) self.canvas.refresh() layer.endEditCommand()
class DtMoveSideByArea(): '''Parallel move polygon side in order to achieve a desired polygon area''' def __init__(self, iface, toolBar): # Save reference to the QGIS interface self.iface = iface self.canvas = self.iface.mapCanvas() self.gui = None self.multipolygon_detected = False # points of the selected segment # p1 is always the left point self.p1 = None self.p2 = None self.rb1 = QgsRubberBand(self.canvas, False) self.selected_feature = None #create action self.side_mover = QtGui.QAction( QtGui.QIcon(":/ParallelMovePolygonSideByArea.png"), QtCore.QCoreApplication.translate( "digitizingtools", "Parallel move of polygon side to target area"), self.iface.mainWindow()) self.side_mover.triggered.connect(self.run) self.iface.currentLayerChanged.connect(self.enable) toolBar.addAction(self.side_mover) self.enable() self.tool = DtSelectSegmentTool(self.canvas) def showDialog(self): flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint # QgisGui.ModalDialogFlags self.gui = DtMoveSideByArea_Dialog(self.iface.mainWindow(), flags) self.gui.initGui() self.gui.show() QObject.connect(self.gui, SIGNAL("unsetTool()"), self.unsetTool) QObject.connect(self.gui, SIGNAL("moveSide()"), self.moveSide) def enableSegmentTool(self): self.canvas.setMapTool(self.tool) #Connect to the DtSelectVertexTool QObject.connect(self.tool, SIGNAL("segmentFound(PyQt_PyObject)"), self.storeSegmentPoints) def unsetTool(self): self.p1 = None self.p2 = None self.selected_feature = None self.canvas.unsetMapTool(self.tool) def run(self): '''Function that does all the real work''' layer = self.iface.activeLayer() if (layer.dataProvider().geometryType() == 6): self.multipolygon_detected = True title = QtCore.QCoreApplication.translate("digitizingtools", "Move polygon side by area") if layer.selectedFeatureCount() == 0: QtGui.QMessageBox.information( None, title, QtCore.QCoreApplication.translate( "digitizingtools", "Please select one polygon to edit.")) elif layer.selectedFeatureCount() > 1: QtGui.QMessageBox.information( None, title, QtCore.QCoreApplication.translate( "digitizingtools", "Please select only one polygon to edit.")) else: #One selected feature self.selected_feature = layer.selectedFeatures()[0] self.enableSegmentTool() self.showDialog() self.gui.writeArea(self.selected_feature.geometry().area()) def storeSegmentPoints(self, result): if result[0].x() < result[1].x(): self.p1 = result[0] self.p2 = result[1] elif result[0].x() == result[1].x(): self.p1 = result[0] self.p2 = result[1] else: self.p1 = result[1] self.p2 = result[0] def enable(self): '''Enables/disables the corresponding button.''' # Disable the Button by default self.side_mover.setEnabled(False) layer = self.iface.activeLayer() if layer <> None: #Only for vector layers. if layer.type() == QgsMapLayer.VectorLayer: # only for polygon layers if layer.geometryType() == 2: # enable if editable self.side_mover.setEnabled(layer.isEditable()) try: layer.editingStarted.disconnect( self.enable) # disconnect, will be reconnected except: pass try: layer.editingStopped.disconnect( self.enable) # when it becomes active layer again except: pass layer.editingStarted.connect(self.enable) layer.editingStopped.connect(self.enable) def moveSide(self): new_a = -1.0 try: new_a = float(self.gui.targetArea.text()) except: pass if (new_a == -1.0): QMessageBox.information( None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate("digitizingtools", "Target Area not valid.")) return if self.p1 == None or self.p2 == None: QMessageBox.information( None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate("digitizingtools", "Polygon side not selected.")) else: touch_p1_p2 = self.selected_feature.geometry().touches( QgsGeometry.fromPolyline([self.p1, self.p2])) if (not touch_p1_p2): QMessageBox.information( None, QCoreApplication.translate("digitizingtools", "Cancel"), QCoreApplication.translate( "digitizingtools", "Selected segment should be on the selected polygon.")) else: #Select tool to create new geometry here if self.gui.method == "fixed": new_geom = moveFixed(self.selected_feature.geometry(), self.p1, self.p2, new_a, self.multipolygon_detected) else: new_geom = moveVariable(self.selected_feature.geometry(), self.p1, self.p2, new_a, self.multipolygon_detected) #Store new geometry on the memory buffer fid = self.selected_feature.id() layer = self.iface.activeLayer() layer.beginEditCommand( QtCore.QCoreApplication.translate("editcommand", "Move Side By Area")) layer.changeGeometry(fid, new_geom) self.canvas.refresh() layer.endEditCommand()