In QGIS 3.28 I have layers with a user defined projection. It is simply Transverse Mercator with a custom central meridian. It shows up as EPSG:10000.
Predictably, $area calculations return NaN.
If I use Project Properties > CRS and manually specify EPSG:32734 as the project CRS, OTF transforms do their thing and I am able to calculate $geometries and items relying on the CRS in the layout.
HOWEVER, if I attempt to achieve this via the Python console, Processing toolbox or similar, using the following command:
crs = QgsCoordinateReferenceSystem("EPSG:32734")
QgsProject.instance().setCrs(crs)
The correct EPSG code is displayed in the bottom right and if I check Project>CRS, it shows UTM34S as the selected project CRS but none of the $geometry calculations work, the scale bar in the print layout is wrong.
The map coordinates change to what they should be and on the surface, the new CRS is being used but I still cannot derive an $area calculation.
I cannot fathom what is happening.
EDIT: This is the .prj string:
PROJCS["Transverse_Mercator",
GEOGCS["GCS_WGS_1984",
DATUM["D_WGS84",
SPHEROID["WGS84",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]],
PROJECTION["Transverse_Mercator"],
PARAMETER["false_easting",0.0],
PARAMETER["false_northing",0.0],
PARAMETER["central_meridian",27.0],
PARAMETER["scale_factor",1.0],
PARAMETER["latitude_of_origin",0.0],
UNIT["Meter",1.0]]
-
1Please write the text of your own CRS so that others can reproduce your difficulty.Vsevolod Tsukanov– Vsevolod Tsukanov2023年08月01日 23:00:22 +00:00Commented Aug 1, 2023 at 23:00
-
1Please write in more detail where and how you perform $area calculation. I set $area to be displayed as labels - they are displayed normally for both projections.Vsevolod Tsukanov– Vsevolod Tsukanov2023年08月02日 07:51:14 +00:00Commented Aug 2, 2023 at 7:51
-
I'm just doing the $area calc in the field calculator. $area is meant to give the ellipsoidal area and as I am getting NaN, I feel as though the project CRS is not being correctly set via the setCrs() method. Manually changing the project CRS does allow me to perform $area calculations however, which is where I am confused. The setCrs() method does not seem to work in the same way as manually setting the CRS. area($geometry) works fine even with my older PROJ string.Ingwe– Ingwe2023年08月02日 14:19:20 +00:00Commented Aug 2, 2023 at 14:19
-
I was able to reproduce your issue. Indeed, when setting a project's CRS with a script, the $area function in the calculator uses the previous projection, unless the projection is set manually. Now it's clear.Vsevolod Tsukanov– Vsevolod Tsukanov2023年08月02日 17:48:47 +00:00Commented Aug 2, 2023 at 17:48
1 Answer 1
According to the documentation, only if adjustEllipsoid is set to True, the ellipsoid of this project will be set to the ellipsoid imposed by the CRS.
crs = QgsCoordinateReferenceSystem("EPSG:32734")
QgsProject.instance().setCrs(crs, True)
And since your projection did not specify an ellipsoid and you did not set the value to True when setting the new CRS for the project, the project remained without an ellipsoid value (if I understand correctly).
In general, add True
in your code and then $area
will work in the calculator after running the script.
Explore related questions
See similar questions with these tags.