[QGIS-Developer] QgsLayerTreeLayer source vs. QgsMapLayer source?

Thomas Larsen Wessel mrvelle at gmail.com
Wed Jan 3 14:11:17 PST 2024


*QgsLayerTreeLayer*
https://qgis.org/pyqgis/3.34/core/QgsLayerTreeLayer.html
No setters or getters for source, but the constructor does take a source,
and the XML export has a "source" attribute.

*QgsMapLayer*
https://qgis.org/pyqgis/3.34/core/QgsMapLayer.html
Constructor takes a source, and there are several getters / setters for
source. The XML export has a "datasource" element.

*Isnt the source redundant? Or can they have different values? *
Are there situations where the QgsLayerTreeLayer source and the QgsMapLayer
source (for corresponding pairs) have different values?

It seems to me like the source should reside only in one of the two
objects, and it should be serialized only for that object, and not in two
places. Am I missing something?

*Is the following a bug?*
I have observed the following behavior. I have a Qgz file created with QGIS
v. 3.34, where the two source values for a layer are identical (as I would
expect, though it seems redundant to have two values in the first place). I
move the folder with the Qgz file and a Shapefile (using relative link) to
a different location.

Now I run a pyqgis standalone script, that simply opens the Qgz file, does
absolutely nothing, and write the data back to the Qgz file.


print(Qgis.version()) # '3.22.4-Białowieża'
QgsApplication.setPrefixPath("/usr", True)
project = QgsProject.instance()
qgs = QgsApplication([], False)
qgs.initQgis()
project.read('test.qgz')

project.write('test.qgz')


Now the two source values are no longer the same. The QgsMapLayer source
has been changed to point to the old location of the Shapefile (before
moving the project).

I have not been able to make the same thing happen by opening and saving
using the QGIS GUI.

Relevant excerpts from XML after running script. The two paths are no
longer identical:
* <layer-tree-layer source="./shapefiles/skab/skab.shp" (...)>
* <datasource>../shapefiles/skab/skab.shp</datasource>

Sincerely, Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20240103/b380dd10/attachment.htm>


More information about the QGIS-Developer mailing list