<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<font face="Ubuntu">Hello,<br>
<br>
Here is a snipped of code that can help you.<br>
To test this you have to:<br>
- Show python console<br>
- enable the polygonal digitizing tool (must be the active tool
before launching this snippet)<br>
- execute the code in the python editor<br>
- start digitizing<br>
- see the area printing in the console<br>
<br>
</font>Don't hesitate if you have further questions.<br>
<font face="Ubuntu"><font face="Ubuntu"><br>
Regards,<br>
Jacky Volpes - Djedouas - Oslandia<br>
</font><br>
---<br>
<br>
# Measurement object (adapt with crs/transform context/ellipsoid)<br>
da = QgsDistanceArea()<br>
da.setSourceCrs(QgsProject.instance().crs(),
QgsProject.instance().transformContext())<br>
# If distance measurement must use ellipsoid<br>
da.setEllipsoid(QgsProject.instance().ellipsoid())<br>
<br>
# This is a demonstration function to adapt with your needs<br>
def showArea(current_point):<br>
if not isinstance(iface.mapCanvas().mapTool(),
QgsMapToolDigitizeFeature):<br>
return<br>
<br>
# Snapping<br>
snapped_point =
iface.mapCanvas().snappingUtils().snapToMap(current_point).point()<br>
if not snapped_point.isEmpty():<br>
current_point = snapped_point<br>
<br>
# Coordinates in map coordinates<br>
points = [QgsPointXY(point) for point in
iface.mapCanvas().mapTool().pointsZM()]<br>
points.append(current_point)<br>
<br>
# Show area<br>
print(da.measurePolygon(points))<br>
<br>
# Must be handled correctly in the final code, but here is for
testing<br>
iface.mapCanvas().xyCoordinates.connect(</font><font face="Ubuntu"><font
face="Ubuntu">showArea</font>)<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</font><br>
<div class="moz-cite-prefix">Le 05/04/2023 à 17:56, Catania, Luke A
ERDC-RDE-GRL-VA CIV via QGIS-Developer a écrit :<br>
</div>
<blockquote type="cite"
cite="mid:9b55acc5a90341a2b1613c1e6cdf9488@erdc.dren.mil">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style>@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face
{font-family:"var\(--ff-mono\)";
panose-1:0 0 0 0 0 0 0 0 0 0;}@font-face
{font-family:inherit;
panose-1:0 0 0 0 0 0 0 0 0 0;}p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}code
{mso-style-priority:99;
font-family:"Courier New";}pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:"Courier New";}span.EmailStyle22
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}div.WordSection1
{page:WordSection1;}</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal">Looking for something like <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="vertical-align:baseline"><span
style="font-size:10.0pt;font-family:"inherit",serif;border:none
windowtext 1.0pt;padding:0in">canvas.xyCoordinates.connect(
self.canvasMoveEvent )<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">but that’s from a 7 year old post so looks
like <span
style="font-size:10.0pt;font-family:"inherit",serif;border:none
windowtext 1.0pt;padding:0in">
xyCoordinates is not in QGIS 3.x</span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1
1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> QGIS-Developer
<a class="moz-txt-link-rfc2396E" href="mailto:qgis-developer-bounces@lists.osgeo.org"><qgis-developer-bounces@lists.osgeo.org></a>
<b>On Behalf Of </b>Catania, Luke A ERDC-RDE-GRL-VA CIV
via QGIS-Developer<br>
<b>Sent:</b> Wednesday, April 5, 2023 11:53 AM<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a><br>
<b>Subject:</b> Re: [QGIS-Developer] Detect a Map Canvas
Mouse Event<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I found <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<pre style="vertical-align:baseline"><span style="font-family:"var(--ff-mono)",serif">QgsMapToolEmitPoint</span><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> , QgsMapTool</span><span style="font-family:"var(--ff-mono)",serif"> and </span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">canvas.setMapTool(…)<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"><o:p> </o:p></span></code></pre>
<p class="MsoNormal" style="vertical-align:baseline"><code><span
style="font-size:10.0pt;font-family:"inherit",serif;border:none
windowtext 1.0pt;padding:0in">So I declared my class as
</span></code><span
style="font-size:10.0pt;font-family:"var(--ff-mono)",serif">AoiSizeValidator(QgsMapToolEmitPoint)</span><span
style="font-family:"var(--ff-mono)",serif"><o:p></o:p></span></p>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"><o:p> </o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">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. </span></code><span style="font-family:"var(--ff-mono)",serif"><o:p></o:p></span></pre>
<p class="MsoNormal" style="vertical-align:baseline"><span
style="font-size:10.0pt;font-family:"var(--ff-mono)",serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1
1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> QGIS-Developer <<a
href="mailto:qgis-developer-bounces@lists.osgeo.org"
moz-do-not-send="true" class="moz-txt-link-freetext">qgis-developer-bounces@lists.osgeo.org</a>>
<b>On Behalf Of </b>Catania, Luke A ERDC-RDE-GRL-VA CIV
via QGIS-Developer<br>
<b>Sent:</b> Wednesday, April 5, 2023 11:27 AM<br>
<b>To:</b> <a
href="mailto:qgis-developer@lists.osgeo.org"
moz-do-not-send="true" class="moz-txt-link-freetext">qgis-developer@lists.osgeo.org</a><br>
<b>Subject:</b> [QGIS-Developer] Detect a Map Canvas Mouse
Event<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Is there an event I can connect to so I can
run my custom code when the even occurs?<o:p></o:p></p>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-Developer mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-Developer@lists.osgeo.org">QGIS-Developer@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-developer">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a>
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Jacky Volpes
Ingénieur SIG - Oslandia</pre>
</body>
</html>