Saving a project automatically can be useful for autosave features or as part of a process to autogenerate projects from dynamically updated data. In this recipe, we'll save a QGIS project to a .qgs
project file.
You will need to download the following zipped shapefile and extract it to your qgis_data
directory, to a subdirectory named ms
:
We will create a simple QGIS project by loading a shapefile layer, then we'll access the project object, and save the map project to a file, as follows:
from PyQt4.QtCore import *
lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/ms/mississippi.shp", "Mississippi", "ogr") reg = QgsMapLayerRegistry.instance() reg.addMapLayer(lyr)
file
object to save our project:f = QFileInfo("/Users/joellawhead/qgis_data/myProject.qgs")
p = QgsProject.instance()
p.write(f)