Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
About the Author
About the Reviewers

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:

  1. First, we need the Qt core library in the QGIS Python console:

    from PyQt4.QtCore import *
  2. 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()
  3. Then, we create a file object to save our project:

    f = QFileInfo("/Users/joellawhead/qgis_data/myProject...