Bug report #17603

Area calculation issue changing the Coordinate Reference System

Added by Geo Dev about 2 years ago. Updated 11 months ago.

Status:Open
Priority:High
Assignee:-
Category:Field calculator
Affected QGIS version:3.7(master) Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:25500

Description

Changing the CRS of a project, areas calculated with the function $area don't change according to the new settings.

Steps to replicate the issue:
- open any polygon vector layer
- add a numeric field called "Area" and calculate the feature areas using the function $area
- change the projection to any another CRS or to "No projection"
- recalculate the field "Area": the areas are exactly the same (unexpected result as they should change)
- simply remove the layer and add it again into the project (without changing any settings)
- recalculate the field "Area": the areas are now changed (expected result)

The issue also doesn't occur changing the CRS and saving-reopening the project before executing the calculation.

The issue is OS independent and occurs in QGIS 2.99.0-master, QGIS 2.18.14 and QGIS 2.14.20.

History

#1 Updated by Geo Dev about 2 years ago

More useful details

The function $area takes into account only the CRS set at the time when the Attribute Table is opened.
All the subsequent changes to the CRS are ignored by the function until the Attribute Table is reopened.

Steps to replicate this behavior:
- set the projection to CRS X
- open the Attribute Table and calculate the areas
- leave the Attribute Table open and change the projection to another CRS Y
- open a second Attribute Table (leaving the first one still open) and recalculate the areas
- in the first table $area will continue to calculate the areas in the CRS X while in the second table $area will continue to calculate the areas in the CRS Y

The function $area works properly only if no CRS changes are made while the Attribute Table in which it is called remains open.

#2 Updated by Harrissou Santanna about 2 years ago

I don't know how the first report and the second one completes each other; they look like two somehow different issues.

About the second message, calculating fields are one-shot operation, meaning that whatever changes you do later, results will not be updated (unless you use virtual field which holds dynamic data). This means that when you calculate $area, the results are calculated based on current crs and stored as any other data in your attribute table:simple values. They won't change if you switch crs or reshape the features geometry. You need to recalculate it.

#3 Updated by Geo Dev about 2 years ago

Of course the field "Area" need to be recalculated. In fact in the steps to replicate the issue is always written to recalculate the area.
The two reports are related to the same issue and the second one add further details emerged later by further testing.
The $area function doesn't work properly when the CRS is changed while the Attribute Table is open.
If you change the CRS when the Attribute Table is open, calling the function $area it continues to calculate the area in the previous reference system. The results are NOT based on current CRS.
Keep two Attribute Tables open at the same time with the first one opened before changing the CRS and the second one opened after changing the CRS.
Calculate now the areas in both the tables: they are different but they should be identical since the function $area should calculate the areas in both the tables according to the current CRS.
The areas in the second table are related to the current CRS while the areas in the first table continue to be referred to the previous CRS despite the function $area is called after the CRS has changed.

#4 Updated by Giovanni Manghi 11 months ago

  • Status changed from Open to Feedback

Please check if this issue is still valid on QGIS 3.4.5 or 3.6.

#5 Updated by Geo Dev 11 months ago

  • Status changed from Feedback to Open

The issue still occurs in QGIS 3.4.5, QGIS 3.6.0 and QGIS 3.7(master).

The issue occurs since the object QgsDistanceArea (used by the Attribute Table to calculate areas and lengths) is not synchronized with the project CRS.
The object QgsDistanceArea is instantiated when the Attribute Table is opened but all subsequent changes to the project CRS are no longer taken into account.
This is the reason why the Attribute Table must be closed and reopened in order to reload the current CRS settings.

#6 Updated by Giovanni Manghi 11 months ago

  • Affected QGIS version changed from master to 3.7(master)

Also available in: Atom PDF