Search code examples
python-3.xgisgdal

Python GDAL, SetAttributeFilter not working


I am trying to use GDAL's SetAttributeFilter() to filter the features in a layer of my shapefile, but the filter seems to have no effect.

My current data is a shapefile from the US Census Bureau, but I have tried with other shapefiles and get a similar result.

For example

from osgeo import ogr

shapefile_path = '../input/processed/shapefile/'
shapefile_ds = ogr.Open(shapefile_path)
cbsa = shapefile_ds.GetLayer('cb_2016_us_cbsa_500k')

print(cbsa.GetFeatureCount())

cbsa.SetAttributeFilter('NAME = "Chicago-Naperville-Elgin, IL-IN-WI"')
feat = cbsa.GetNextFeature()

print(feat.GetField('NAME'))
print(cbsa.GetFeatureCount())

Yields

945
Platteville, WI
945

I'm using Python 3.6 and GDAL 2.2.1


Solution

  • You can capture the return value of the SetAttributeFilter statement and make sure its 0, otherwise something went wrong.

    In this particular case, its probably due to the quoting. Single quotes refer to string literals (a value), and double quotes refer to a column/table name.

    Depending on how you run this Python code, somewhere in the stdout/stderr GDAL prints something like: ERROR 1: "Chicago-Naperville-Elgin, IL-IN-WI" not recognised as an available field.

    More details can be found at: https://trac.osgeo.org/gdal/wiki/rfc52_strict_sql_quoting

    To get it working, simply swap the single/double quoting, so:

    cbsa.SetAttributeFilter("NAME='Chicago-Naperville-Elgin, IL-IN-WI'")