TransWikia.com

QgsExtentGroupBox draw on canvas causing odd behaviour

Geographic Information Systems Asked by MappingThePast on August 5, 2021

I am trying to make the most of the QgsExtentGroupBox from the Qt designer QGIS widgets in a plugin I’m building (using pluginbuilder 3.2.1, and QGIS 3.16 builtin Python 3.7 and Qt Designer). Currently, all it does is fill the extent given in the widget with equally spaced vectorpoints. This works fine when using the basic version (which takes a layer extent) and extending it with setMapCanvas(iface.mapCanvas())

My issue is with setOutputExtentDrawOnCanvas(). All this should do is to set outputExtent to a rectangle drawn on the canvas.
In the UI, the extent is updated correctly. When pressing ok (def run), extent is not filled with points, and if I click the canvas, the plugin opens back up, with pressing OK still not doing anything. However, if I draw the rectangle, press OK and then open the plugin from the plugin menu and press OK again, it does run. However, the rectangle is also still there, and the rectangle drawing tool is still active (clicking the canvas will still reopen the plugin).

Is this a bug, or something I can solve?

Code in dialog:

class PluginDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(PluginDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.setupUi() you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.extent = None
        self.mExtentGroupBox.setMapCanvas(iface.mapCanvas())
        self.mExtentGroupBox.setOutputExtentFromDrawOnCanvas()
        self.mExtentGroupBox.extentChanged.connect(self.setExtent)

    def setExtent(self):
        """Attaches the extent given by the user to a variable, and updates the 'current extent'
        so that the input can be used in further analysis"""
        self.extent = self.mExtentGroupBox.outputExtent()
        self.mExtentGroupBox.setCurrentExtent(self.extent, self.mExtentGroupBox.outputCrs())

Code in main plugin code:

    def run(self):
        """Run method that performs all the real work"""

        # Create the dialog with elements (after translation) and keep reference
        # Only create GUI ONCE in callback, so that it will only load when the plugin is started
        if self.first_start == True:
            self.first_start = False
            self.dlg = PluginDialog()


        # show the dialog
        self.dlg.show()
        # Run the dialog event loop
        result = self.dlg.exec_()
        # See if OK was pressed
        if result:

            layer = iface.activeLayer() 
            spacing = self.dlg.spinBox_resolution.value()
            inset = spacing * 0.5 #set inset
            crs = layer.crs()

            #Create new vector point layer
            vectorpoint_base = QgsVectorLayer('Point', 'Name', 'memory', crs=crs,) 
            data_provider = vectorpoint_base.dataProvider()

            #Set extent of the new layer
            if self.dlg.extent is None:
                self.iface.messageBar().pushMessage('Extent not chosen!', level=1)
            else:
                self.iface.messageBar().pushMessage('Extent set!', level=0)
                xmin = self.dlg.extent.xMinimum() + inset
                xmax = self.dlg.extent.xMaximum()
                ymin = self.dlg.extent.yMinimum()
                ymax = self.dlg.extent.yMaximum() - inset

            #Create the coordinates of the points in the grid
                points = []
                y = ymax
                while y >= ymin:
                    x = xmin
                    while x <= xmax:
                        geom = QgsGeometry.fromPointXY(QgsPointXY(x,y))
                        feat = QgsFeature()
                        feat.setGeometry(geom)
                        points.append(feat)
                        x += spacing
                    y = y-spacing
                data_provider.addFeatures(points)
                vectorpoint_base.updateExtents()

                # Add layer to map
                QgsProject.instance().addMapLayer(vectorpoint_base)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP