2017-09-03 34 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