qgis / QGIS

QGIS is a free, open source, cross platform (lin/win/mac) geographical information system (GIS)
https://qgis.org
GNU General Public License v2.0
10.37k stars 2.98k forks source link

Print Layout: with a rotated map, turning on Controlled by Atlas (atlasScalingMode = Auto by default) hangs Layout Manager and corrupts project file #58245

Closed justinbb closed 5 days ago

justinbb commented 1 month ago

What is the bug or the crash?

It is not possible to create an atlas with a rotated map in recent QGIS versions (in the past year or so), because atlasScalingMode = Auto ("Margin around feature") is fragile, but is set by default.

If the map is rotated, turning on Controlled by Atlas hangs Layout Manager. If the project file is saved, it will be corrupt (though not too difficult to fix by manual editing), as numerous dimensions are saved as "nan". When the project is next opened, Layout Manager cannot recover from these bad dimensions.

To avoid failure, the atlas must be set up with no map rotation and atlasScalingMode can then be set to something other than Auto. Then it is safe to rotate the map, and things function normally thereafter, as long as the scaling mode does not return to Auto.

Steps to reproduce the issue

  1. Create a new project. Add a map layer (OpenStreetMap, for example, though it does not matter what the content is).
  2. Add a new scratch layer with polygon geometry – this will serve as the atlas coverage layer. Add a rectangular feature more or less covering the map display.
  3. Create a new Print Layout. Add a Map item covering the page. (Optional: add another element, say a scale bar, and set its reference point to the lower right corner.)
  4. Display the Atlas tab, check Generate an atlas, and set the Coverage layer to the New scratch layer.
  5. Return to the Item Properties of the map. Set Map rotation to a non-zero value (e.g. 30°). At this point everything should still be functional, and the map should be redrawn with the rotation. You may wish to save the project, as this is the last chance before corruption occurs.
  6. Click Controlled by Atlas. Redraw never completes. If you uncheck Controlled by Atlas, you can see that the scale and the extents of the map have changed to "nan". If you added the optional element, you will see that some of its dimensions have also changed to "nan".

Versions

QGIS version    3.38.0-Grenoble QGIS code branch    [Release 3.38](https://github.com/qgis/QGIS/tree/release-3_38)
Qt version  5.15.12     
Python version  3.12.4      
Compiled against GDAL/OGR   3.9.0   Running against GDAL/OGR    3.9.1
PROJ version    9.4.1       
EPSG Registry database version  v11.006 (2024-03-13)        
Compiled against GEOS   3.12.1-CAPI-1.18.1  Running against GEOS    3.12.2-CAPI-1.18.2
SQLite version  3.46.0      
Compiled against PDAL   2.7.1   Running against PDAL    2.7.2
PostgreSQL client version   unknown     
SpatiaLite version  5.1.0       
QWT version 6.1.6       
QScintilla2 version 2.13.3      
OS version  macOS 14.5      

Active Python plugins           
processing_saga_nextgen 1.0.0       
processing  2.12.99     
db_manager  0.1.20      
MetaSearch  0.3.6

Supported QGIS version

New profile

Additional context

This issue may be related to #57872.

atlasScalingMode = Auto is a DWIM feature that sets off calculations based on assumptions that are neither tested nor even asserted in qgslayoutitemmap.cpp – rather perilous for something turned on by default. Once an arithmetic error occurs (nan), undetected, it spreads through the layout, like a spreadsheet with formulas referring to a cell that is missing or contains a bad value. Even saving the project is done naively, so the errors become persistent in the project. At this point all a regular user can do is delete the entire print layout and start over (taking great care to set up the atlas with an unrotated map and to set the scaling mode to fixed scale, only then rotating the map). Someone with the ability to extract the qgs file and edit it by hand can find all the "nan" values and replace them with valid numbers, and also change any occurrences of AtlasMap scalingMode="2" to scalingMode="0", and then the damage is contained and repaired. But none of this corruption should be possible in the first place…

justinbb commented 1 month ago

Problem occurs with official QGIS build, both PR 3.38.1 and LTR 3.34.9. I was not able to test older versions because Mac binary archiving at https://download.qgis.org/downloads/macOS/ stopped in 2019. Problem was not so obvious when I was doing similar work in August 2023, most likely with the latest point release. Problem discovered when returning to an atlas configured in 2023 which now demonstrates the problem. I have not been able to figure out why it happens now and didn't before (my configuration has Main Properties / Scale bound to an atlas variable, so one wonders why the scalingMode was now being forced to auto in contradiction with this setting).

agiudiceandrea commented 1 month ago

@justinbb , thanks for reporting. Please provide a minimal reproducible example with a sample project a layer.

justinbb commented 1 month ago

atlas bug sample.zip This sample project has a print layout with the map already rotated. Simply checking "Controlled by atlas" will provoke the bug.

agiudiceandrea commented 1 month ago

I can confirm the issue also on Windows 10 using either QGIS 3.34.9, QGIS 3.36.1 or QGIS 3.38.1. The issue doesn't occur using QGIS 3.32.1 or QGIS 3.28.15.

nirvn commented 6 days ago

@justinbb , thanks for taking the time to provide a sample project; it did wonders in figuring out what's wrong and fixing the underlying issue. You can expect it fixed in QGIS' next point release.