Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
QGIS Python Programming Cookbook, Second Edition

You're reading from   QGIS Python Programming Cookbook, Second Edition Automating geospatial development

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787124837
Length 464 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joel Lawhead Joel Lawhead
Author Profile Icon Joel Lawhead
Joel Lawhead
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Automating QGIS FREE CHAPTER 2. Querying Vector Data 3. Editing Vector Data 4. Using Raster Data 5. Creating Dynamic Maps 6. Composing Static Maps 7. Interacting with the User 8. QGIS Workflows 9. Other Tips and Tricks

Storing and reading global preferences

PyQGIS allows you to store application-level preferences and retrieve them. QGIS also has project-level preferences, which can override the application-level preferences in some cases. In this recipe, we'll focus on reading and writing the global preferences that serve as the application defaults. QGIS actually takes advantage of Qt's built-in preferences management.

Getting ready

This code can be run in any type of PyQGIS application. In this example, we'll run it in the QGIS Python console for an easy demonstration. We'll also change the default CRS for new projects and then read the value back from the global settings.

How to do it...

In this recipe, we will set the default projection used by QGIS for new projects. We'll do this using the Python console:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. We will need to import the Qt core library, as follows:
            from PyQt4.QtCore import * 
    
  4. In the Python console, run the following code:
            settings = QSettings(QSettings.NativeFormat,
                                 QSettings.UserScope,'QuantumGIS', 'QGis') 
            settings.setValue('/Projections/projectDefaultCrs', 'EPSG:2278') 
            settings.value('/Projections/projectDefaultCrs') 
            settings.sync() 
    

How it works...

This API is actually the Qt API that QGIS relies on for settings. In the QSettings object, we specify the native format for storage, which is the default format for the platform. On Windows, the format is the registry; on OS X, it's the plist files, and on Unix, it's the text files. The other QSettings parameters are the organization and the application, often used as a hierarchy to store information. Note that when you change the settings, you must call the sync() method at the end to save them and let them take effect. Note that when you are writing a standalone QGIS application, you should use your own organization and application name for settings and not QGIS.

There's more...

If you want to see all the options that you can change, call the allKeys() method of QSettings; this will return a list of all the setting names.

You have been reading a chapter from
QGIS Python Programming Cookbook, Second Edition - Second Edition
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787124837
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image