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 语法介绍

相关推荐
秃头佛爷3 分钟前
Python学习大纲总结及注意事项
开发语言·python·学习
深度学习lover1 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
API快乐传递者2 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃4 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
丕羽7 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng8 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
m0_594526309 小时前
Python批量合并多个PDF
java·python·pdf
工业互联网专业9 小时前
Python毕业设计选题:基于Hadoop的租房数据分析系统的设计与实现
vue.js·hadoop·python·flask·毕业设计·源码·课程设计
钱钱钱端9 小时前
【压力测试】如何确定系统最大并发用户数?
自动化测试·软件测试·python·职场和发展·压力测试·postman
慕卿扬9 小时前
基于python的机器学习(二)—— 使用Scikit-learn库
笔记·python·学习·机器学习·scikit-learn