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

Filtering a layer by geometry


In this recipe, we'll perform a spatial operation to select a subset of a point layer based on the points contained in an overlapping polygon layer. We'll use shapefiles in both cases, with one being a point layer and the other a polygon. This kind of subset is one of the most common GIS operations.

Getting ready

We will need two new shapefiles that have not been used in previous recipes. You can download the point layer from https://geospatialpython.googlecode.com/files/MSCities_Geo_Pts.zip.

Similarly, you can download the geometry layer from https://geospatialpython.googlecode.com/files/GIS_CensusTract.zip.

Unzip these shapefiles and place them in a directory named ms within your qgis_data directory, within your root or home directory.

How to do it...

In this recipe, we will perform several steps to select features in the point layer that fall within the polygon layer, as follows:

  1. First, load the point layer:

    lyrPts = QgsVectorLayer("/qgis_data/ms/MSCities_Geo_Pts...