前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GEE python:按照矢量中的几何位置、属性名称和字符串去筛选矢量集合

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

作者头像
此星光明
发布2024-02-02 13:23:37
2250
发布2024-02-02 13:23:37
举报

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

  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

代码语言:javascript
复制
import subprocess

try:
    import geemap
except ImportError:
    print('Installing geemap ...')
    subprocess.check_call(["python", '-m', 'pip', 'install', 'geemap'])
代码语言:javascript
复制
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 代码:

代码语言:javascript
复制
# 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’},。。。。

代码:

代码语言:javascript
复制
# 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

按照字符串去筛选:

代码语言:javascript
复制
# 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*')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Installs geemap package
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档