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

Using NULL values in PyQGIS


QGIS can use NULL values as field values. Python has no concept of NULL values. The closest type it has is the None type. You must be aware of this fact when working with Python in QGIS. In this recipe, we'll explore the implications of QGIS's NULL values in Python. The computing of a NULL value involves a pointer that is an uninitialized, undefined, empty, or meaningless value.

Getting ready

In your qgis_data/shapes directory, download the shapefile from https://geospatialpython.googlecode.com/svn/NullExample.zip, which contains some NULL field values, and unzip it.

How to do it...

We will load the shapefile and grab its first feature. Then, we'll access one of its NULL field values. Next, we'll run through some tests that allow you to see how the NULL values behave in Python. To do this, we need to perform the following steps:

  1. First, we'll load the shapefile and access its first feature:

    lyrPth = "/qgis_data/shapes/NullExample.shp"
    lyr = QgsVectorLayer(lyrPth, "Null...