I'm using the following python script to open a QGIS project, modify some things, and save it under a different name:
from PyQt4.QtCore import QFileInfo
from qgis.core import *
def main():
QgsApplication([], True)
QgsApplication.setPrefixPath('/usr/share/qgis', False) #If this is set to true qgis can't find resources..
QgsApplication.initQgis()
proj=QgsProject.instance()
proj.read(QFileInfo("source.qgs"))
#do other stuff to the project
proj.write(QFileInfo("target.qgs"))
QgsApplication.exitQgis()
if __name__ == "__main__":
main()
While this seems to work fine at first, the XML nodes <mapcanvas>
,<layer-tree-legend>
and a few others are omitted in the now smaller target file. I want to serve the target file via QGIS Server, but QGIS Server doesn't pick up on all the available layers due to these missing nodes in the XML file. The GetProjectSettings request doesn't show any of the available layers even though they're clearly still in the project.
Opening the target file in QGIS Desktop and saving it again works fine, and then the missing nodes are added again to the qgs file.
Does anyone know why this information is lost when saving a project, and what I could do to preserve or regenerate it?
Edit: added setPrefixPath
<layer-tree-legend>
tag does not exist, I suppose you mean<layer-tree-canvas>
or<layer-tree-group>
or only<legend>
. Also Do you define the map canvas somewhere in your script?<mapcanvas>
,<visibility-presets>
,<layer-tree-canvas>
,<legend>
, and various<edittype>
tags have gone missing from somewhere within various<maplayer>
tags.