QGIS – How to Reproject Geometry with a QGIS Expression

coordinate systemqgisqgis-expression

In the QGIS attribute form, I'm trying to convert from NAD83/BC Albers (EPSG:3005) to WGS84 / Pseudo-Mercator (EPSG 4326) with the following code:

format_number(
    y(
        transform(
            $geometry,
            'EPSG:3005',
            'EPSG:4326'
        )
    ),
    6
)  

The process works but I'm getting incorrect coordinates returned. I use identical code lines to successfully transform other CRSs, but obviously I'm messing something up here. I've attached a screengrab that previews the properly formatted resultant 44.199697° that should be more like 49.920942°

enter image description here

Best Answer

Make sure that the assigned CRS is set to EPSG:3005 and correct the value of your target EPSG.

enter image description here

You should put this as a default values for your lon/lat fields, respectively :

format_number(x( transform( $geometry, layer_property(@layer,'crs'), 'EPSG:4326')),6)
format_number(y( transform( $geometry, layer_property(@layer,'crs'), 'EPSG:4326')),6)
Related Question