Saving a map to a project
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.
Getting ready
You will need to download the following zipped shapefile and extract it to your qgis_data
directory, to a subdirectory named ms
:
How to do it...
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:
- First, we need the Qt core library in the QGIS Python console:
from PyQt4.QtCore import *
- Next, we load the shapefile and add it to the map:
lyr = QgsVectorLayer("/Users/joellawhead/qgis_data/ms/mississippi.shp", "Mississippi", "ogr") reg = QgsMapLayerRegistry.instance() reg.addMapLayer(lyr)
- Then, we create a
file
object to save our project:f = QFileInfo...