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.
...
with an action that retrieves theQgsMapCanvas.extent()
(pyQGIS API).