[QGIS-Developer] Detect a Map Canvas Mouse Event
Jacky Volpes
jacky.volpes at oslandia.com
Tue Apr 11 02:15:39 PDT 2023
Hello,
Here is a snipped of code that can help you.
To test this you have to:
- Show python console
- enable the polygonal digitizing tool (must be the active tool before
launching this snippet)
- execute the code in the python editor
- start digitizing
- see the area printing in the console
Don't hesitate if you have further questions.
Regards,
Jacky Volpes - Djedouas - Oslandia
---
# Measurement object (adapt with crs/transform context/ellipsoid)
da = QgsDistanceArea()
da.setSourceCrs(QgsProject.instance().crs(),
QgsProject.instance().transformContext())
# If distance measurement must use ellipsoid
da.setEllipsoid(QgsProject.instance().ellipsoid())
# This is a demonstration function to adapt with your needs
def showArea(current_point):
if not isinstance(iface.mapCanvas().mapTool(),
QgsMapToolDigitizeFeature):
return
# Snapping
snapped_point =
iface.mapCanvas().snappingUtils().snapToMap(current_point).point()
if not snapped_point.isEmpty():
current_point = snapped_point
# Coordinates in map coordinates
points = [QgsPointXY(point) for point in
iface.mapCanvas().mapTool().pointsZM()]
points.append(current_point)
# Show area
print(da.measurePolygon(points))
# Must be handled correctly in the final code, but here is for testing
iface.mapCanvas().xyCoordinates.connect(showArea)
Le 05/04/2023 à 17:56, Catania, Luke A ERDC-RDE-GRL-VA CIV via
QGIS-Developer a écrit :
>
> Looking for something like
>
> canvas.xyCoordinates.connect( self.canvasMoveEvent )
>
> but that’s from a 7 year old post so looks like xyCoordinates is not
> in QGIS 3.x
>
> *From:* QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> *On
> Behalf Of *Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer
> *Sent:* Wednesday, April 5, 2023 11:53 AM
> *To:* qgis-developer at lists.osgeo.org
> *Subject:* Re: [QGIS-Developer] Detect a Map Canvas Mouse Event
>
> I found
>
> QgsMapToolEmitPoint, QgsMapTooland |canvas.setMapTool(…)|
> ||
>
> |So I declared my class as |AoiSizeValidator(QgsMapToolEmitPoint)
>
> ||
> |But gave that a try and it gets me x,y, initially but when I select
> an add layer from the qgis edit menu, I guess it sets a new map tool
> so mine gets overridden. So looks like I need a different approach to
> get xy from the map canvas. |
>
> *From:* QGIS-Developer <qgis-developer-bounces at lists.osgeo.org> *On
> Behalf Of *Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer
> *Sent:* Wednesday, April 5, 2023 11:27 AM
> *To:* qgis-developer at lists.osgeo.org
> *Subject:* [QGIS-Developer] Detect a Map Canvas Mouse Event
>
> Trying to get a map canvas event when canvas is clicked and mouse is
> dragged. I have code that creates an empty layer, but I use the QGIS
> edit menu to add the actually layer. I want to be able to track the
> mouse movement so I can calculated the area of the feature being drawn
> in real time so I can make sure the area is within a defined range.
>
> Is there an event I can connect to so I can run my custom code when
> the even occurs?
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info:https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe:https://lists.osgeo.org/mailman/listinfo/qgis-developer
--
Jacky Volpes
Ingénieur SIG - Oslandia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230411/89c617dc/attachment-0001.htm>
More information about the QGIS-Developer
mailing list