Re: [QGIS-Developer] Detect a Map Canvas Mouse Event
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 *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@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 *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@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@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 ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] Detect a Map Canvas Mouse Event
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 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@lists.osgeo.org Subject: Re: [QGIS-Developer] Detect a Map Canvas Mouse Event I found QgsMapToolEmitPoint , QgsMapTool and 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 mailto:qgis-developer-boun...@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@lists.osgeo.org<mailto:qgis-developer@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@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] Detect a Map Canvas Mouse Event
I found QgsMapToolEmitPoint , QgsMapTool and 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 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@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@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
[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@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer