SetSpatialFilter函数
SetSpatialFitler函数在osgeo.ogr模块下的Layer类中。
SetSpatialFilter(Geometry filter)
输入中的Geometry作为过滤的参数。
示例
Python
from osgeo imiport ogr
import os
shp_file = 'sates.shp'
drv = ogr.GetDriverByName('ESRI Shapefile')
ds = drv.Open(shp_file)
lyr = ds.GetLayer()
wkt = 'POLYGON ((-103.81402655265633 50.253951270672125,-102.94583419409656 51.535568561879401,-100.34125711841725 51.328856095555651,-100.34125711841725 51.328856095555651,-93.437060743203844 50.460663736995883,-93.767800689321859 46.450441890315041,-94.635993047881612 41.613370178339181,-100.75468205106476 41.365315218750681,-106.12920617548238 42.564247523428456,-105.96383620242338 47.277291755610058,-103.81402655265633 50.253951270672125))'
geom = orgr.CreateGeometryFromWkt(wkt)
lyr.SetSpatialFilter(geom)
for feat in lyr:
print feat.GetField('STATE_NAME')
lyr.SetSpatialFilter(None)
lyr.ResetReading()
重点
- 参数Geometry的构造;
- lyr在执行空间检索后,lyr中包含的要素即为检索的结果;
- lyr循环后,如果要再使用lyr,则需要清除过滤条件,图层重新开始读取(ResetReading())
注意
在官方关于该函数的文档中,有些值得注意的内容。
- This function set the geometry to be used as a spatial filter when fetching features via the OGR_L_GetNextFeature() function. Only features that geometrically intersect the filter geometry will be returned.
- Currently this test is may be inaccurately implemented(实现上不够精确), but it is guaranteed that all features whose envelope (as returned by OGR_G_GetEnvelope()) overlaps the envelope of the spatial filter will be returned(要素的外封与过滤要素的外封相叠). This can result in more shapes being returned that should strictly be the case(会返回更多的要素).