要按照矢量中的几何位置去筛选矢量集合,您可以使用空间查询或选择工具。以下是一些示例:
无论您选择哪种方法,都应该先确定筛选条件,然后使用适当的工具来筛选矢量集合。
安装地球引擎API和geemap 安装地球引擎的Python API和geemap。geemap Python包是建立在ipyleaflet和folium包之上的,它实现了几个与地球引擎数据层交互的方法,比如Map.addLayer()、Map.setCenter()和Map.centerObject()。下面的脚本检查geemap包是否已经安装。如果没有,它将安装geemap,它会自动安装其依赖项,包括earthengine-api、folium和ipyleaflet。
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*')