GEE python:按照矢量中的几何位置、属性名称和字符串去筛选矢量集合

本文涉及的产品
函数计算FC,每月15万CU 3个月
简介: GEE python:按照矢量中的几何位置、属性名称和字符串去筛选矢量集合

要按照矢量中的几何位置去筛选矢量集合,您可以使用空间查询或选择工具。以下是一些示例:

  1. 空间查询工具:许多GIS软件都具有空间查询工具,可帮助您筛选矢量。您可以使用矩形选择工具、圆形选择工具或多边形选择工具选择特定区域的矢量。还可以使用空间查询语言(例如SQL)编写复杂的查询来识别满足特定条件的矢量,例如在指定距离内的点或多边形。
  2. 编写自定义脚本或程序:如果您需要更复杂的筛选,可以编写自定义脚本或程序来筛选矢量。可以使用Python、C ++或其他编程语言来构建您的脚本或程序,以根据坐标、属性或其他条件筛选矢量。
  3. 使用地图编辑器:一些GIS软件具有地图编辑器,其中包括选择和编辑矢量的工具。这些工具可帮助您在地图上选择特定区域的矢量,并进行编辑或删除。

无论您选择哪种方法,都应该先确定筛选条件,然后使用适当的工具来筛选矢量集合。

安装地球引擎API和geemap

安装地球引擎的Python API和geemap。geemap Python包是建立在ipyleaflet和folium包之上的,它实现了几个与地球引擎数据层交互的方法,比如Map.addLayer()、Map.setCenter()和Map.centerObject()。下面的脚本检查geemap包是否已经安装。如果没有,它将安装geemap,它会自动安装其依赖项,包括earthengine-api、folium和ipyleaflet。

Installs geemap package

import subprocess
try:
    import geemap
except ImportError:
    print('Installing geemap ...')
    subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
import ee
import geemap

函数:

ee.Filter.contains(leftField, rightValue, rightField, leftValue, maxError)

Creates a unary or binary filter that passes if the left geometry contains the right geometry (empty geometries are not contained in anything).

这里需要明确的一点就是这里的Filed就是我们集合中的属性名称,value就是值,这里一般会设定,按照名称或者是属性值的后缀来筛选

Arguments:

leftField (String, default: null):

A selector for the left operand. Should not be specified if leftValue is specified.

rightValue (Object, default: null):

The value of the right operand. Should not be specified if rightField is specified.

rightField (String, default: null):

A selector for the right operand. Should not be specified if rightValue is specified.

leftValue (Object, default: null):

The value of the left operand. Should not be specified if leftField is specified.

maxError (ErrorMargin, optional):

The maximum reprojection error allowed during filter application.

Returns: Filter

代码:

# Add Earth Engine dataset
HUC10 = ee.FeatureCollection("USGS/WBD/2017/HUC10")
HUC08 = ee.FeatureCollection('USGS/WBD/2017/HUC08')
roi = HUC08.filter(ee.Filter.eq('name', 'Pipestem'))
Map.centerObject(roi, 10)
Map.addLayer(ee.Image().paint(roi, 0, 3), {}, 'HUC08')
# select polygons intersecting the roi
roi2 = HUC10.filter(ee.Filter.contains(**{'leftValue': roi.geometry(), 'rightField': '.geo'}))
Map.addLayer(ee.Image().paint(roi2, 0, 2), {'palette': 'blue'}, 'HUC10')
roi3 = HUC10.filter(ee.Filter.stringContains(**{'leftField': 'huc10', 'rightValue': '10160002'}))
print(roi3.getInfo())
Map.addLayer(roi3)

结果:

{‘type’: ‘FeatureCollection’, ‘columns’: {‘areaacres’: ‘String’, ‘areasqkm’: ‘String’, ‘gnis_id’: ‘String’, ‘huc10’: ‘String’, ‘humod’: ‘String’, ‘hutype’: ‘String’, ‘loaddate’: ‘String’, ‘metasource’: ‘String’, ‘name’: ‘String’, ‘shape_area’: ‘String’, ‘shape_leng’: ‘String’, ‘sourcedata’: ‘String’, ‘sourcefeat’: ‘String’, ‘sourceorig’: ‘String’, ‘states’: ‘String’, ‘system:index’: ‘String’, ‘tnmid’: ‘String’},。。。。

代码:

# Add Earth Engine dataset
# Select North Dakota and South Dakota
fc = ee.FeatureCollection('TIGER/2018/States') \
    .filter(ee.Filter.Or(
        ee.Filter.eq('STUSPS', 'ND'),
        ee.Filter.eq('STUSPS', 'SD'),
    ))
image = ee.Image().paint(fc, 0, 2)
# Map.setCenter(-99.844, 37.649, 5)
Map.centerObject(fc, 6)
Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States')

ee.Filter.stringContains(leftField, rightValue, rightField, leftValue)

Creates a unary or binary filter that passes if the left operand, a string, contains the right operand, also a string.

Arguments:

leftField (String, default: null):

A selector for the left operand. Should not be specified if leftValue is specified.

rightValue (Object, default: null):

The value of the right operand. Should not be specified if rightField is specified.

rightField (String, default: null):

A selector for the right operand. Should not be specified if rightValue is specified.

leftValue (Object, default: null):

The value of the left operand. Should not be specified if leftField is specified.

Returns: Filter

ee.Filter.stringStartsWith(leftField, rightValue, rightField, leftValue)

Creates a unary or binary filter that passes if the left operand, a string, starts with the right operand, also a string.

Arguments:

leftField (String, default: null):

A selector for the left operand. Should not be specified if leftValue is specified.

rightValue (Object, default: null):

The value of the right operand. Should not be specified if rightField is specified.

rightField (String, default: null):

A selector for the right operand. Should not be specified if rightValue is specified.

leftValue (Object, default: null):

The value of the left operand. Should not be specified if leftField is specified.

Returns: Filter

按照字符串去筛选:

# Add Earth Engine dataset
#!/usr/bin/env python
"""Select by strings
"""
# Select states with "A" in its name
fc = ee.FeatureCollection('TIGER/2018/States') \
    .filter(ee.Filter.stringContains('STUSPS', 'A'))
image = ee.Image().paint(fc, 0, 2)
Map.centerObject(fc, 6)
Map.addLayer(image, {'palette': 'FF0000'}, '*A*')
# Select states its name starting with 'A'
fc = ee.FeatureCollection('TIGER/2018/States') \
    .filter(ee.Filter.stringStartsWith('STUSPS', 'A'))
image = ee.Image().paint(fc, 0, 2)
Map.addLayer(image, {'palette': '0000FF'}, 'A*')
相关实践学习
【文生图】一键部署Stable Diffusion基于函数计算
本实验教你如何在函数计算FC上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。函数计算提供一定的免费额度供用户使用。本实验答疑钉钉群:29290019867
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
相关文章
|
5天前
|
存储 API 索引
Python 的集合是怎么实现的?
Python 的集合是怎么实现的?
28 9
|
7天前
|
存储 索引 Python
Python常用数据结构——集合
Python常用数据结构——集合
21 3
|
8天前
|
Windows Python
python获取windows机子上运行的程序名称
python获取windows机子上运行的程序名称
|
9天前
|
网络协议 网络安全 开发者
Python 向IP地址发送字符串
Python 向IP地址发送字符串
25 2
|
9天前
|
Python
Python 中取字符串中等于号后面的内容
Python 中取字符串中等于号后面的内容在编程过程中,我们经常需要从字符串中提取特定的信息。一个常见的任务是在给定的字符串中查找等于号(=)后面的内容。这种需求在解析配置文件、处理查询字符串或分析日志数据时尤其常见。 如何实现 在Python中,我们可以使用多种方法来实现此功能。以下是几种常用的方法,包括字符串操作和正则表达式。 方法 1:使用字符串分割 我们可以使用字符串的 split() 方法将字符串拆分为两个部分,然后提取等于号后的值。 示例代码 ----------------------------------- ©著作权归作者所有:来自51CTO博客作者bruce_xiao
23 1
|
9天前
|
Python
Python批量复制指定名称文件的技巧
通过上述步骤和示例代码,你可以轻松实现批量复制特定名称文件的功能。这种技术不仅节省了时间,而且通过脚本自动化,提高了工作效率。
23 2
|
7天前
|
存储 数据处理 Python
Python中的Set集合:高效数据处理的利器
Python中的Set集合:高效数据处理的利器
16 0
|
7天前
|
物联网 Python
python向IP地址发送字符串
python向IP地址发送字符串
17 0
|
8天前
|
JSON 数据格式 Python
6-1|Python如何将json转化为字符串写到文件内 还保留json格式
6-1|Python如何将json转化为字符串写到文件内 还保留json格式
|
8天前
|
Python
python编程获取续蜀山剑侠传:从目录名称、网址到内容
python编程获取续蜀山剑侠传:从目录名称、网址到内容
下一篇
无影云桌面