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

Iterating over layers


For many GIS operations, you need to loop through the map layers to look for specific information or to apply a change to all the layers. In this recipe, we'll loop through the layers and get information about them.

Getting ready

We'll need two layers in the same map projection to perform this recipe. You can download the first layer as a ZIP file from https://geospatialpython.googlecode.com/files/MSCities_Geo_Pts.zip.

You can download the second zipped layer from https://geospatialpython.googlecode.com/files/Mississippi.zip.

Unzip both of these layers into a directory named ms within your qgis_data directory.

How to do it...

We will add the layers to the map through the map registry. Then, we will iterate through the map layers and print each layer's title. To do this, perform the following steps:

  1. First, let's open the polygon and the point layer using the QGIS Python Console:

    lyr_1 = QgsVectorLayer("/Users/joellawhead/qgis_data/ms/mississippi.shp", "Mississippi", "ogr")
    lyr_2...