[QGIS-Developer] Digitizing with Edit Menu

Jacky Volpes jacky.volpes at oslandia.com
Tue May 2 00:26:23 PDT 2023


Hello,

Have you tried this following code? Using 
iface.mapCanvas().mapTool().pointsZM() is good for me with any 
digitizing tool, while drawing.


# 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 of currently drawn points in map coordinates
     points = [QgsPointXY(point) for point in 
iface.mapCanvas().mapTool().pointsZM()]
     points.append(current_point)

     # Get area
     print(da.measurePolygon(points))

# Must be handled correctly in the final code, but here is for testing
iface.mapCanvas().xyCoordinates.connect(showArea)



Le 29/04/2023 à 01:45, Catania, Luke A ERDC-RDE-GRL-VA CIV via 
QGIS-Developer a écrit :
>
> 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.
>
>
> _______________________________________________
> 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/20230502/b12f758a/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 82922 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230502/b12f758a/attachment-0001.png>


More information about the QGIS-Developer mailing list