1

I am trying to initialise my DrawPolygonMapTool within my application once the DrawPolygonButton is enabled. When the relevant button is pushed, it does run the __init__ function in my DrawPolygonMapTool Class but it doesn't seem to set the map tool on the canvas or the cursor. I don't get any errors when I run the code.

class DrawPolygonMapTool(QgsMapToolEmitPoint):
    def __init__(self, canvas, layer,*args, **kwargs):
        super(DrawPolygonMapTool,self).__init__(canvas)
        print ('i am here')
        self.canvas = canvas
        self.layer = layer
        self.setCursor(Qt.CrossCursor)
        self.vertices = []
        self.provider = layer.dataProvider()
        print (self.layer, self.provider)
        #self.canvasClicked.connect(self.clicked)

    def canvasPressEvent(self, mouseEvent):
        if mouseEvent.button() == Qt.LeftButton:
            qgsPoint = self.toMapCoordinates(mouseEvent.pos())
            qgspointxy = QgsPointXY(qgsPoint)
            self.vertices.append(qgspointxy)
            print (self.vertices, self.vertices[0], type(self.vertices[0]))

        #def canvasDoubleClickEvenr(self, mouseEvent):
        if mouseEvent.button() == Qt.RightButton:
            polygeom = QgsGeometry().fromPolygonXY([self.vertices])
            feature = QgsFeature()
            feature.setGeometry(polygeom)
            self.layer.dataProvider().addFeatures([feature])
            self.layer.updateExtents()
            QgsProject().instance().addMapLayers([self.layer])
            self.canvas.refresh()


class DrawPolygonButton(QAction):
    def __init__(self, *args, **kwargs):
        super(DrawPolygonButton, self).__init__(*args, **kwargs)
        self.setCheckable(True)
        self.setEnabled(True)
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.canvas.refresh()
        self.project = QgsProject.instance()


    def is_checked(self,state):
        if state==True:
            self.canvas.refresh()
            so_layer = [lyr for lyr in self.canvas.layers() if lyr.name()=='so_outlines'][0] #vector Multipolygon layer 
            so_layer.startEditing()
            mpt = DrawPolygonMapTool(self.canvas,so_layer)
            self.canvas.setMapTool(mpt)


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        # Save reference to the QGIS interface
        self.iface = iface
        self.canvas = self.iface.mapCanvas()
        self.project = QgsProject.instance()
        self.setWindowTitle('Interpretation')

        #add toolbar
        self.toolbar = QToolBar("Interpretation Toolbar")
        self.toolbar.setIconSize(QSize(16,16))
        self.addToolBar(self.toolbar)

        self.DrawPolygonButton = DrawPolygonButton(QIcon(r'puzzle--plus.png'), 'Draw Polygon', self)
        self.toolbar.addAction(self.DrawPolygonButton)
        self.toolbar.addSeparator()
        self.DrawPolygonButton.toggled.connect(self.DrawPolygonButton.is_checked)

app = QApplication(sys.argv)
window = MainWindow()
window.show()

Running the DrawPolygonMapTool separately works fine- I'm just having problems trying to connect the application to the tool. What am I missing?

  • 1
    Have you tried self.mpt = DrawP... for keeping reference ? – J. Monticolo Apr 7 at 14:13
  • thank you-I've been staring at this for hours ! – thesunnyscientist Apr 7 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.