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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a layer style file


Layer styling is one of the most complex aspects of the QGIS Python API. Once you've developed the style for a layer, it is often useful to save the styling to the QGIS Markup Language (QML) in the XML format.

Getting ready

You will need to download the zipped directory named saveqml and decompress it to your qgis_data/rasters directory from https://geospatialpython.googlecode.com/svn/saveqml.zip.

How to do it...

We will create a color ramp for a DEM and make it semi transparent to overlay a hillshaded tiff of the DEM. We'll save the style we create to a QML file. To do this, we need to perform the following steps:

  1. First, we'll need the following Python Qt libraries:

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    
  2. Next, we'll load our two raster layers:

    hs = QgsRasterLayer("/qgis_data/saveqml/hillshade.tif", "Hillshade")
    dem = QgsRasterLayer("/qgis_data/saveqml/dem.asc", "DEM")
    
  3. Next, we'll perform a histogram stretch on our DEM for better visualization:

    algorithm...