OGR-矢量筛选

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)

OGR SQL dialect 语法介绍
SQL Lite dialect 语法介绍

相关推荐
007php00720 分钟前
GoZero 上传文件File到阿里云 OSS 报错及优化方案
服务器·开发语言·数据库·python·阿里云·架构·golang
Tech Synapse22 分钟前
Python网络爬虫实践案例:爬取猫眼电影Top100
开发语言·爬虫·python
一行玩python1 小时前
SQLAlchemy,ORM的Python标杆!
开发语言·数据库·python·oracle
数据小爬虫@2 小时前
利用Python爬虫获取淘宝店铺详情
开发语言·爬虫·python
编程修仙3 小时前
Collections工具类
linux·windows·python
芝麻团坚果3 小时前
对subprocess启动的子进程使用VSCode python debugger
linux·ide·python·subprocess·vscode debugger
EterNity_TiMe_3 小时前
【论文复现】神经网络的公式推导与代码实现
人工智能·python·深度学习·神经网络·数据分析·特征分析
Stara05113 小时前
Git推送+拉去+uwsgi+Nginx服务器部署项目
git·python·mysql·nginx·gitee·github·uwsgi
hence..4 小时前
Vscode写markdown快速插入python代码
ide·vscode·python
DanielYQ5 小时前
LCR 001 两数相除
开发语言·python·算法