This recipe demonstrates how to load a project from a .qgs
XML file. Loading a project will set up the map and project settings for a previously saved project within QGIS.
You will need to complete the previous recipe, Saving a map to a project, so that you have a project named myProject.qgs
in your qgis_data
folder.
For this recipe, you need to set up a file object, set a resource path, and then read the file object that references the project file. To do this, you need to perform the following steps:
Qt
library for the file object:from PyQt4.QtCore import *
f = QFileInfo("/Users/joellawhead/qgis_data/myProject.qgs")
p = QgsProject.instance()
p.readPath("/Users/joellawhead/qgis_data/")
p.read(f)
QGIS has a setting to save references to data and other files either as relative paths, which are relative to the project file, or absolute paths, which contain the full path. If the saved paths are absolute, PyQGIS will be unable to locate data sources. Setting the read path to the full system path of the project file ensures that QGIS can find all the referenced files in the project file, if they are saved as relative paths.