2017-09-03 467 views
0

我想使用GDAL的SetAttributeFilter()过滤我shapefile的图层中的功能,但过滤器似乎没有效果。Python的GDAL,SetAttributeFilter不工作

我目前的数据是来自美国人口普查局的shapefile,但我已经尝试过使用其他shapefile并得到类似的结果。

例如

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()) 

息率

945 
Platteville, WI 
945 

我使用Python 3.6和2.2.1 GDAL

回答

0

您可以捕获SetAttributeFilter声明的返回值,并确保其0,否则出问题了。

在这种特殊情况下,它可能是由于引用。单引号引用字符串文字(值),双引号引用列/表名称。

取决于您如何运行此Python代码,在标准输出/标准错误GDAL打印类似的地方: ERROR 1: "Chicago-Naperville-Elgin, IL-IN-WI" not recognised as an available field.

更多细节,可以发现: https://trac.osgeo.org/gdal/wiki/rfc52_strict_sql_quoting

得到它的工作,只需简单更换单/双引号,所以:

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

+0

你是完全正确的,我得到的5返回值和交换的曲otes工作。非常感谢! – Chris