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 架构模式
相关文章
|
4月前
|
安全 网络安全 文件存储
思科设备巡检命令Python脚本大集合
【10月更文挑战第18天】
144 1
思科设备巡检命令Python脚本大集合
|
1月前
|
物联网 Python
请问:如何使用python对物联网平台上设备的属性进行更改?
为验证项目可行性,本实验利用阿里云物联网平台创建设备并定义电流、电压两个整型属性。通过Python与平台交互,实现对设备属性的控制,确保后续项目的顺利进行。此过程涵盖设备连接、数据传输及属性调控等功能。
|
3月前
|
Python
在 Python 中,如何将日期时间类型转换为字符串?
在 Python 中,如何将日期时间类型转换为字符串?
148 64
|
2月前
|
SQL 存储 数据挖掘
使用Python和PDFPlumber进行简历筛选:以SQL技能为例
本文介绍了一种使用Python和`pdfplumber`库自动筛选简历的方法,特别是针对包含“SQL”技能的简历。通过环境准备、代码解析等步骤,实现从指定文件夹中筛选出含有“SQL”关键词的简历,并将其移动到新的文件夹中,提高招聘效率。
72 8
使用Python和PDFPlumber进行简历筛选:以SQL技能为例
|
30天前
|
存储 人工智能 Python
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
16 0
|
2月前
|
存储 数据处理 Python
Python如何显示对象的某个属性的所有值
本文介绍了如何在Python中使用`getattr`和`hasattr`函数来访问和检查对象的属性。通过这些工具,可以轻松遍历对象列表并提取特定属性的所有值,适用于数据处理和分析任务。示例包括获取对象列表中所有书籍的作者和检查动物对象的名称属性。
43 2
|
2月前
|
存储 测试技术 Python
Python 中别再用 ‘+‘ 拼接字符串了!
通过选择合适的字符串拼接方法,可以显著提升 Python 代码的效率和可读性。在实际开发中,根据具体需求和场景选择最佳的方法,避免不必要的性能损失。
62 5
|
2月前
|
Python
使用Python计算字符串的SHA-256散列值
使用Python计算字符串的SHA-256散列值
72 7
|
3月前
|
Python
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
61 6
|
4月前
|
Python
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
本篇将详细介绍Python中的字符串类型及其常见操作,包括字符串的定义、转义字符的使用、字符串的连接与格式化、字符串的重复和切片、不可变性、编码与解码以及常用内置方法等。通过本篇学习,用户将掌握字符串的操作技巧,并能灵活处理文本数据。
70 1
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解

热门文章

最新文章