TransWikia.com

Select extent on canvas - QGIS Plugin

Geographic Information Systems Asked by Vincent Bré on September 15, 2020

I’m designing a QGIS plugin where I’d like to offer the user the functionality to select the extent of a bounding box from the canvas like in some tools like Create grid.

enter image description here
I’ve explored QT Designer but there is no widget with this functionality.

I have also explored the tool code of tools that offer this functionality but found nothing.

Finally, I didn’t find any documentation about this feature. Any ideas?

One Answer

You mean select a region on the map, on the Canvas map? And that the selected region is shown on the map by clicking and dragging the cursor? From a dialog box or button? And capture the coordinates of the region for a process?

In that case, you must create a custom map tool that interacts with the mapCanvas. That is, write a class that extends QgsMapTool or QgsMapToolEmitPoint.

In the cookbook they explain in detail how to do it, here https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/canvas.html#id4 Go to the item Writing Custom Map Tools

The map tools require the mapCanvas on which they will act. Inside the class you must rewrite the different events, for example

def canvasPressEvent(self, event):

The event gives you the coordinates where you clicked event.pos().x() in screen coordinates, but you can convert them to map coordinates if you previously, in the init, create a QgsCoordinateTransform, for example with:

self.transform = self.can.getCoordinateTransform()

Concerning the trace of the rectangle drawn by the user, you use a QgsRubberBand that requires the mapCanvas and the geometry it will represent.

To activate the tool after clicking a button, simply create an instance of your tool (QgsMapTool), tool=youTool(canvas) and then activate it by assigning it to the canvas with iface.mapCanvas().setMapTool(youTool).

There are several things to consider but this will guide you if you are going to create your own mapTool for what you want.

Answered by Luis Perez on September 15, 2020

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