from osgeo import ogr inDataSource = ogr.Open( "parcels.shp" ) lyr = inDataSource.GetLayer() # this is a unique attribute filter targeting only one record lyr.SetAttributeFilter("PIN = '0000200001'") for i in range( 0, lyr.GetFeatureCount() ): feat = lyr.GetFeature( i ) # this will print one feat, but it's the first feat in the Layer and NOT our target filtered feat print (feat)
有两种方法可以访问一个图层的功能。你可以使用 getfeature()
和传递功能或使用 getnextfeature()
传回来以后的特征。如果你正在使用一个属性过滤器( setattributefilter()
)或空间滤波器
( setspatialfilter()
或 setspatialfilterrect()
)然后你必须使用getnextfeature()。
如果你读过任何的过滤者你会看到ogr_l_getnextfeature()警告文件。这意味着,如果你使用getfeature(),代替getnextfeature(),那么你仍然可以访问与不被滤层功能的工作。getfeaturecount()将尊重过滤器和显示正确的数字滤波功能。然而,在一个循环getfeaturecount()工作会导致一些有趣的结果。使用层对象作为一个功能的迭代器或使用getnextfeature()明确应该访问功能的默认方法:
Copyright © Since 2014.
开源地理空间基金会中文分会
吉ICP备05002032号
Powered by TorCMS