<div dir="ltr">Have you considered listening to the featureAdded signal instead? <div>The digitizing shape (aka rubberband) you see while digitizing is not really accessible. In fact, it isn't guaranteed to be the geometry of the feature being created. It could even not exist at all ; this is all dependent on the current QgsMapTool used.</div><div><br></div><div>Rather than monitoring clicks, or accessing the rubberband, you could listen to the featureAdded signal. This signal is emitted when a new feature is added to the layer. You can then perform your check (e;g area validation), and delete the feature if the check fails. It won't even appear on screen.</div><div><br></div><div>Here is a possible implementation. Note that I split the AreaValidator into three classes for genericity, but you could merge these three classes in a single one.</div><div><br></div><div>LayerConnector : this is used to automatically connect all map layers to a function</div><div>FeatureValidator: Specialization, connects all QgsVectorLayer featureAdded signal to a function that takes the layer and the feature being added as parameters<br></div><div>AreaValidator: Contains the actual area computation, and possible deletion of the feature </div><div><br></div><div><br></div><div><div style="color:rgb(0,0,0);font-family:Hasklig,Consolas,"Courier New",monospace,Consolas,"Courier New",monospace;font-size:15px;line-height:20px;white-space:pre"><div><span style="color:rgb(0,0,255)">class</span> <span style="color:rgb(38,127,153)">LayerConnector</span>(<span style="color:rgb(38,127,153)">QObject</span>):</div><div>    <span style="color:rgb(163,21,21)">"""Generic class to connect to all QgsMapLayer"""</span></div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">__init__</span>(<span style="color:rgb(0,16,128)">self</span>):</div><div>        <span style="color:rgb(38,127,153)">super</span>().<span style="color:rgb(121,94,38)">__init__</span>()</div><div>        QgsProject.instance().layerWasAdded.connect(<span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">connect_layer</span>)</div><div>        <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">connect_layers</span>()</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">__del__</span>(<span style="color:rgb(0,16,128)">self</span>):</div><div>        QgsProject.instance().layerWasAdded.disconnect(<span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">connect_layer</span>)</div><div>        <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">disconnect_layers</span>()</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">connect_layers</span>(<span style="color:rgb(0,16,128)">self</span>):</div><div>        <span style="color:rgb(121,94,38)">print</span>(<span style="color:rgb(163,21,21)">"Connecting"</span>)</div><div>        <span style="color:rgb(175,0,219)">for</span> <span style="color:rgb(0,16,128)">layer</span> <span style="color:rgb(175,0,219)">in</span> QgsProject.instance().mapLayers().values():</div><div>            <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">connect_layer</span>(<span style="color:rgb(0,16,128)">layer</span>)</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">disconnect_layers</span>(<span style="color:rgb(0,16,128)">self</span>):</div><div>        <span style="color:rgb(121,94,38)">print</span>(<span style="color:rgb(163,21,21)">"DisConnecting"</span>)</div><div>        <span style="color:rgb(175,0,219)">for</span> <span style="color:rgb(0,16,128)">layer</span> <span style="color:rgb(175,0,219)">in</span> QgsProject.instance().mapLayers().values():</div><div>            <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">disconnect_layer</span>(<span style="color:rgb(0,16,128)">layer</span>)</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">connect_layer</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>):</div><div>        <span style="color:rgb(175,0,219)">pass</span></div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">disconnect_layer</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>):</div><div>        <span style="color:rgb(175,0,219)">pass</span></div><br><br><div><span style="color:rgb(0,0,255)">class</span> <span style="color:rgb(38,127,153)">FeatureValidator</span>(<span style="color:rgb(38,127,153)">LayerConnector</span>):</div><div>    <span style="color:rgb(163,21,21)">"""Connect all QgsVectorLayer to a process_feature method when a feature is added"""</span></div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">connect_layer</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>):</div><div>        <span style="color:rgb(175,0,219)">if</span> <span style="color:rgb(0,0,255)">not</span> <span style="color:rgb(121,94,38)">isinstance</span>(<span style="color:rgb(0,16,128)">layer</span>, QgsVectorLayer):</div><div>            <span style="color:rgb(175,0,219)">return</span></div><div>        <span style="color:rgb(0,16,128)">layer</span>.featureAdded.connect(<span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">on_feature_added</span>)</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">disconnect_layer</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>):</div><div>        <span style="color:rgb(175,0,219)">if</span> <span style="color:rgb(0,0,255)">not</span> <span style="color:rgb(121,94,38)">isinstance</span>(<span style="color:rgb(0,16,128)">layer</span>, QgsVectorLayer):</div><div>            <span style="color:rgb(175,0,219)">return</span></div><div>        <span style="color:rgb(0,16,128)">layer</span>.featureAdded.disconnect(<span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">on_feature_added</span>)</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">on_feature_added</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">id</span>):</div><div>        <span style="color:rgb(0,16,128)">layer</span> = <span style="color:rgb(0,16,128)">self</span>.sender()</div><div>        <span style="color:rgb(0,16,128)">feat</span> = <span style="color:rgb(0,16,128)">layer</span>.getFeature(<span style="color:rgb(0,16,128)">id</span>)</div><div>        <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(121,94,38)">process_feature</span>(<span style="color:rgb(0,16,128)">layer</span>, <span style="color:rgb(0,16,128)">feat</span>)</div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">process_feature</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>, <span style="color:rgb(0,16,128)">feature</span>):</div><div>        <span style="color:rgb(121,94,38)">print</span>(<span style="color:rgb(0,0,255)">f</span><span style="color:rgb(163,21,21)">"Feature </span><span style="color:rgb(0,0,255)">{</span><span style="color:rgb(0,16,128)">feature</span>.id()<span style="color:rgb(0,0,255)">}</span><span style="color:rgb(163,21,21)"> added to layer </span><span style="color:rgb(0,0,255)">{</span><span style="color:rgb(0,16,128)">layer</span>.name()<span style="color:rgb(0,0,255)">}</span><span style="color:rgb(163,21,21)">"</span>)</div><br><br><div><span style="color:rgb(0,0,255)">class</span> <span style="color:rgb(38,127,153)">AreaValidator</span>(<span style="color:rgb(38,127,153)">FeatureValidator</span>):</div><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">__init__</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">max_area</span>):</div><div>        <span style="color:rgb(38,127,153)">super</span>().<span style="color:rgb(121,94,38)">__init__</span>()</div><div>        <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(0,16,128)">max_area</span> = <span style="color:rgb(0,16,128)">max_area</span></div><br><div>    <span style="color:rgb(0,0,255)">def</span> <span style="color:rgb(121,94,38)">process_feature</span>(<span style="color:rgb(0,16,128)">self</span>, <span style="color:rgb(0,16,128)">layer</span>, <span style="color:rgb(0,16,128)">feature</span>):</div><div>        <span style="color:rgb(0,16,128)">geom</span> = <span style="color:rgb(0,16,128)">feature</span>.geometry()</div><div>        <span style="color:rgb(175,0,219)">if</span> <span style="color:rgb(0,0,255)">not</span> <span style="color:rgb(0,16,128)">geom</span>:</div><div>            <span style="color:rgb(175,0,219)">return</span></div><br><div>        <span style="color:rgb(0,128,0)"># Here you should handle CRS transformation</span></div><div>        <span style="color:rgb(0,128,0)"># before computing the feature area</span></div><div>        <span style="color:rgb(0,16,128)">area</span> = <span style="color:rgb(0,16,128)">geom</span>.area()</div><br><div>        <span style="color:rgb(175,0,219)">if</span> <span style="color:rgb(0,16,128)">area</span> > <span style="color:rgb(0,16,128)">self</span>.<span style="color:rgb(0,16,128)">max_area</span>:</div><div>            iface.messageBar().pushWarning(<span style="color:rgb(163,21,21)">"Cannot add feature"</span>, <span style="color:rgb(0,0,255)">f</span><span style="color:rgb(163,21,21)">"Area too big : </span><span style="color:rgb(0,0,255)">{</span><span style="color:rgb(121,94,38)">round</span>(<span style="color:rgb(0,16,128)">area</span>, <span style="color:rgb(9,134,88)">2</span>)<span style="color:rgb(0,0,255)">}</span><span style="color:rgb(163,21,21)">"</span>)</div><div>            <span style="color:rgb(0,16,128)">layer</span>.deleteFeature(<span style="color:rgb(0,16,128)">feature</span>.id())</div><br><br><div><span style="color:rgb(0,16,128)">validator</span> = <span style="color:rgb(38,127,153)">AreaValidator</span>(<span style="color:rgb(9,134,88)">500</span>)</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le dim. 30 avr. 2023 à 18:42, Catania, Luke A ERDC-RDE-GRL-VA CIV via QGIS-Developer <<a href="mailto:qgis-developer@lists.osgeo.org">qgis-developer@lists.osgeo.org</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg7101726221067993375">





<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_7101726221067993375WordSection1">
<p class="MsoNormal">When you digitize a shape using the Edit menu in QGIS is the shape accessible?  I am looking to get he area of the shape as it is drawn.  I have added an event filter to my code to capture the coordinates and calculate it by creating a
 QgsGeometry, but my code is getting complicated as there are many shapes that a user can draw from this menu so if I can get access to this shape if it is temporary in memory, then I can just calculate the area on the shape and not have to capture all the
 positions of the mouse when clicked and moved.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><img width="296" height="160" style="width: 3.0833in; height: 1.6666in;" id="m_7101726221067993375Picture_x0020_1" src="cid:187d412cd514cff311"><u></u><u></u></p>
</div>
</div>

_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</div></blockquote></div>