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

相关推荐
FL16238631296 分钟前
基于yolov5的混凝土缺陷检测系统python源码+onnx模型+评估指标曲线+精美GUI界面
人工智能·python·yolo
立黄昏粥可温18 分钟前
Python 从入门到实战22(类的定义、使用)
开发语言·python
今天也要加油丫1 小时前
`re.compile(r“(<.*?>)“)` 如何有效地从给定字符串中提取出所有符合 `<...>` 格式的引用
python
农民小飞侠2 小时前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
战神刘玉栋2 小时前
《程序猿之设计模式实战 · 观察者模式》
python·观察者模式·设计模式
敲代码不忘补水2 小时前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践
鸽芷咕3 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
子午3 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
风等雨归期3 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序