Skip to main content
added 53 characters in body
Source Link

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', TrueFalse) #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

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', True)
    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

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

added 92 characters in body
Source Link

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', True)
    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

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.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?

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', True)
    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

Source Link

Saving a QGIS project using the API loses mapcanvas (and other) XML nodes

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.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?