OGR-矢量筛选

ops/2024/9/23 1:23:41/

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_estFROM '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_estFROM 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_popFROM ne_50m_populated_places ppLEFT JOIN ne_50m_admin_0_countries cON pp.adm0_a3 = c.adm0_a3WHERE 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_popFROM ne_50m_populated_places ppLEFT JOIN ne_50m_admin_0_countries cON pp.adm0_a3 = c.adm0_a3WHERE 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 语法介绍


http://www.ppmy.cn/ops/97995.html

相关文章

实训day34(8.22)

一、回顾 高并发集群 饿了么后端的登录模块 1、数据库 1. 主从复制(高可用) 2. 传统的主从复制 3. gtids事务型的主从复制 4. 注意 1. server_id唯一 2. 8.x版本需要get_ssl_pub_key 3. 5.x不需要 4. change master to 5. stop | start slave 5. 非交互 import pymys…

【鸿蒙学习】HarmonyOS应用开发者高级认证 - 应用性能优化一(界面层面)

学完时间:2024年8月22日 学完排名:第1801名 一、介绍 在开发HarmonyOS应用时,优化应用性能是至关重要的。通过/ArkTS高性能编程、减少丢帧卡顿、提升应用启动和响应速度 可以有效提升用户体验。本文将介绍一些优化HarmonyOS应用性能的方法。 一、Ark…

一款好看的WordPress REST API 主题

介绍: 主题特色: 使用Nuxtjs WordPress Rest Api 实现前后端分离,可完成多端部署; 主题支持自动切换黑夜模式。 使用说明: service 目录为wordpress主题文件,需要拷贝到wordpress主题目录下&#xff0…

【GH】【EXCEL】bumblebee简介:GH↔EXCEL

文章目录 bumblebeeaddressComponentAnalysisAppCellChartingDataGraphicsRangeShapesWorkbooksWorksheets Sample: Accessing_ExcelExcel ApplicationWorkbookSave Workbook (Create)Get All Workbooks from AppGet Workbook by Name Get WorkbookGet Active Workbook from Ap…

esp32c3 luaos

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、介绍二、相关介绍2.1helloworld——2.2任务框架2.3消息传递 与消息订阅2.4uart2.5二进制数据/c结构体的打包与解析2.6 zbuffer库2.8 uart 485 数据解析2.9 …

stm32单片机学习 - stm32 的命名规则

STM32命名规则: 以STM 32 F 103 C 8 T 6 A xxx为例:

go中的并发处理

. Goroutines 概念: Goroutines 是 Go 的核心并发机制。它们是由 Go 运行时管理的轻量级线程,具有比操作系统线程更少的开销。每个 goroutine 只需少量的内存(大约 2KB),并且由 Go 运行时负责调度和管理,哪怕是java发…

Redis中Sorted Set数据类型常用命令

目录 1. 添加元素 2. 获取成员 3. 获取成员的分数 4. 删除元素 5. 获取集合的大小 6. 获取成员的排名 7. 按分数范围获取成员 8. 按排名范围获取成员 9. 增减分数 10. 删除指定分数范围的成员 11. 获取分数的范围 在 Redis 中,Sorted Set(有序集合&#…