OGR-矢量筛选
1.属性筛选
python
# 根据字段属性进行过滤
ds = ogr.Open(os.path.join(data_dir, 'global'))
lyr = ds.GetLayer('ne_50m_admin_0_countries')
>1
lyr.SetAttributeFilter('continent = "Asia"')
lyr.GetFeatureCount()
>2:二次筛选
# You can still get a feature that is not in Asia by using its FID.
lyr.GetFeature(2).GetField('name')
# Set a new filter that selects South American countries and show the results
# in blue. The old filter is no longer in effect.
lyr.SetAttributeFilter('continent = "South America"')
2.空间筛选
python
# Get the Germany polygon. Make sure to plot the full layer before setting the
# filter, or you'll only plot Germany (or you could clear the filter and then
# plot).
ds = ogr.Open(os.path.join(data_dir, 'global'))
country_lyr = ds.GetLayer('ne_50m_admin_0_countries')
# 根据字段进行筛选
country_lyr.SetAttributeFilter('name = "Germany"')
feat = country_lyr.GetNextFeature()
germany = feat.geometry().Clone()
# Plot world cities as yellow dots.
city_lyr = ds.GetLayer('ne_50m_populated_places')
city_lyr.GetFeatureCount()
## 根据几何进行空间过滤
city_lyr.SetSpatialFilter(germany)
city_lyr.GetFeatureCount()
## 根据属性字段的数值进行过滤
city_lyr.SetAttributeFilter('pop_min > 1000000')
city_lyr.GetFeatureCount()
# 清空空间筛选条件
city_lyr.SetSpatialFilter(None)
city_lyr.GetFeatureCount()
# 清空字段筛选选项
country_lyr.SetAttributeFilter(None)
3.SQL
3.1 OGR SQL
ExecuteSQL(sql,[spatialFilter],[dialect])
sql语句
针对结果做空间过滤
指定SQL语句采用的标准字符
语法示例
python
# 从'ne_50m_admin_0_countries'图层中选择area(ogr_geom_area自动获取面积字段)、 name、 pop_est,
# 并根据POP_EST降序排列
ds = ogr.Open(os.path.join(data_dir, 'global'))
sql = '''SELECT ogr_geom_area as area, name, pop_est
FROM 'ne_50m_admin_0_countries' ORDER BY POP_EST DESC'''
lyr = ds.ExecuteSQL(sql)
Limit
python
ds = ogr.Open(os.path.join(data_dir, 'global',
'natural_earth_50m.sqlite'))
sql = '''SELECT geometry, area(geometry) AS area, name, pop_est
FROM countries ORDER BY pop_est DESC LIMIT 3'''
# 'LIMIT 3':只保留筛选项的前三个
lyr = ds.ExecuteSQL(sql)
链接表
python
ds = ogr.Open(os.path.join(data_dir, 'global'))
sql = '''SELECT pp.name AS city, pp.pop_min AS city_pop,
c.name AS country, c.pop_est AS country_pop
FROM ne_50m_populated_places pp
LEFT JOIN ne_50m_admin_0_countries c
ON pp.adm0_a3 = c.adm0_a3
WHERE pp.adm0cap = 1'''
lyr = ds.ExecuteSQL(sql)
# 语法分析
"""
'''SELECT【字段】
FROM 【图层】ON 【字段链接操作:限定adm0_a3两个表链接的条件】
WHERE 【属性过滤:限定条件】
注:OGR SQL where 只能使用原始表的属性进行限定
"""
3.2
SQLite
python
ds = ogr.Open(os.path.join(data_dir, 'global'))
sql = '''SELECT pp.name AS city, pp.pop_min AS city_pop,
c.name AS country, c.pop_est AS country_pop
FROM ne_50m_populated_places pp
LEFT JOIN ne_50m_admin_0_countries c
ON pp.adm0_a3 = c.adm0_a3
WHERE pp.adm0cap = 1 AND c.continent = "South America"'''
# 指定 SQL 语言标准
lyr = ds.ExecuteSQL(sql, dialect='SQLite')
合并矢量
python
# 利用SQL执行是来那个合并操作
# Union the counties together and plot the result. This will only work if you
# have SpatiaLite support.
sql = 'SELECT st_union(geometry) FROM countyp010 WHERE state = "CA"'
lyr = ds.ExecuteSQL(sql, dialect='SQLite')
# Do the same with PostGIS, but only if you've set up a PostGIS server and
# loaded your data in.
conn_str = 'PG:host=localhost user=chrisg password=mypass dbname=geodata'
ds = ogr.Open(conn_str)
sql = "SELECT st_union(geom) FROM us.counties WHERE state = 'CA'"
lyr = ds.ExecuteSQL(sql)