Bug report #16149

project loading a "feature count"-enabled layer can mess project CRS

Added by Mathieu Pellerin - nIRV almost 6 years ago. Updated over 5 years ago.

Category:Project Loading/Saving
Affected QGIS version: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 #:24061


Here's an obscure (and extremely serious) issue I've been able to narrow down (gdb + breakpoint + 3 hours).

The long story short here is that loading a project containing a layer with "feature count" activated leads to a chain reaction that ends up triggering the QgsLayerTreeMapCanvasBridge::setCanvasLayers() function during project load, which under a number of circumstances will call QgsProject::instance()->setCrs( layerNode->layer()->crs() ).

I'm not an expert in the inner workings of Qt, but what seems to be happening here is that the QgsVectorLayer::countSymbolFeatures() function (called during the project load via QgsLayerTreeModel::addLegendToLayer) calls QCoreApplication::processEvents(), which I think ends up emmiting a bunch of signals before due time. If I disable the QCoreApplication::processEvents() line, project load is fine and the project CRS value isn't changed by a undue setCanvasLayers() call.

Steps to reproduce
  1. Open QGIS, and load the test2.qgs project file (attahced to this bug)
  2. Open the Python console, and type the following: QgsProject.instance().crs().authid()
  3. It'll properly return EPSG:3148
  4. Right click on the khm_admbnda_adm3_gov_reg vector layer, and check the "Show feature count" menu item
  5. Save the project, leave QGIS
  6. Re-open QGIS, and load the test2.qgs project file you've just saved
  7. Open the Python console, and type the following: QgsProject.instance().crs().authid()
  8. It'll wrongly return EPSG:4326

To help debug this, I've added a breakpoint within the setCanvasLayers() function where the project CRS is changed. It revealed the link between countSymbolFeatures() and the bogus setCrs() call which wrongly changes the project CRS while the project is being loaded.

The gdb "where" at the breakpoint:

(gdb) where
#0  QgsLayerTreeMapCanvasBridge::setCanvasLayers (this=0x555556255010) at /home/webmaster/dev/cpp/QGIS/src/gui/layertree/qgslayertreemapcanvasbridge.cpp:121
#1  0x00007ffff6b1255d in QgsLayerTreeMapCanvasBridge::qt_static_metacall (_o=0x555556255010, _c=QMetaObject::InvokeMetaMethod, _id=5, _a=0x55555c7e53a0)
    at /home/webmaster/dev/cpp/QGIS/bm-qt5/src/gui/layertree/moc_qgslayertreemapcanvasbridge.cpp:135
#2  0x00007ffff45c3699 in QObject::event(QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#3  0x00007ffff4ed08ac in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#4  0x00007ffff4ed5d4f in QApplication::notify(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#5  0x00007ffff57ef671 in QgsApplication::notify (this=0x7fffffffd990, receiver=0x555556255010, event=0x55555c73fd60)
    at /home/webmaster/dev/cpp/QGIS/src/core/qgsapplication.cpp:336
#6  0x00007ffff45953b0 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#7  0x00007ffff459733c in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#8  0x00007ffff45eb083 in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#9  0x00007fffed61c7d7 in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#10 0x00007fffed61ca40 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#11 0x00007fffed61caec in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#12 0x00007ffff45eb48f in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#13 0x00007ffff5b113a2 in QgsVectorLayer::countSymbolFeatures (this=0x55555c7cb640, showProgress=true) at /home/webmaster/dev/cpp/QGIS/src/core/qgsvectorlayer.cpp:785
#14 0x00007ffff597aa6f in QgsDefaultVectorLayerLegend::createLayerTreeModelLegendNodes (this=0x55555c5c7eb0, nodeLayer=0x55555c73fc50)
    at /home/webmaster/dev/cpp/QGIS/src/core/qgsmaplayerlegend.cpp:195
#15 0x00007ffff578ba87 in QgsLayerTreeModel::addLegendToLayer (this=0x5555564e3940, nodeL=0x55555c73fc50)
    at /home/webmaster/dev/cpp/QGIS/src/core/layertree/qgslayertreemodel.cpp:1201
#16 0x00007ffff578958b in QgsLayerTreeModel::connectToLayer (this=0x5555564e3940, nodeLayer=0x55555c73fc50)
    at /home/webmaster/dev/cpp/QGIS/src/core/layertree/qgslayertreemodel.cpp:851
#17 0x00007ffff5788f36 in QgsLayerTreeModel::nodeLayerLoaded (this=0x5555564e3940) at /home/webmaster/dev/cpp/QGIS/src/core/layertree/qgslayertreemodel.cpp:775
#18 0x00007ffff5e18f66 in QgsLayerTreeModel::qt_static_metacall (_o=0x5555564e3940, _c=QMetaObject::InvokeMetaMethod, _id=7, _a=0x7fffffffb570)
    at /home/webmaster/dev/cpp/QGIS/bm-qt5/src/core/layertree/moc_qgslayertreemodel.cpp:127
#19 0x00007ffff45c2b49 in QMetaObject::activate(QObject*, int, int, void**) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#20 0x00007ffff5e18cd1 in QgsLayerTreeLayer::layerLoaded (this=0x55555c73fc50) at /home/webmaster/dev/cpp/QGIS/bm-qt5/src/core/layertree/moc_qgslayertreelayer.cpp:150
#21 0x00007ffff5782d3e in QgsLayerTreeLayer::resolveReferences (this=0x55555c73fc50, project=0x555555c85c10)
    at /home/webmaster/dev/cpp/QGIS/src/core/layertree/qgslayertreelayer.cpp:58

test2-projectcrs.zip (2.73 MB) Mathieu Pellerin - nIRV, 2017-02-06 12:10 AM

Associated revisions

Revision 21df6252
Added by Nyall Dawson over 5 years ago

Fix incorrect project CRS when loading project (fix #16149)

This commit fixes a situation where loading a project results
in incorrect project & canvas CRS. The bug is trigerred whenever
something in the project load calls a processEvents() call, eg
restoring a project with layer count enabled on a layer
or with a composer html item.

When this occurs, the "auto-set CRS to first added layer" code
would kick in early and replace the project's CRS with that
of the first layer loaded.

To avoid this disable the "auto-set CRS" code when loading
layers from a project.

Revision 31762193
Added by Nyall Dawson over 5 years ago

Merge pull request #4290 from nyalldawson/fix_16149

Fix incorrect project CRS when loading project (fix #16149)


#1 Updated by Nyall Dawson over 5 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF