内联接

如需枚举两个集合元素之间的所有匹配项,请使用 ee.Join.inner()。内连接的输出是 FeatureCollection(即使将一个 ImageCollection 连接到另一个 ImageCollection)。输出中的每个地图项都代表一个匹配项,其中匹配元素存储在地图项的两个属性中。例如,feature.get('primary') 是主集合中的元素,与存储在 feature.get('secondary') 中的次要集合中的元素匹配。(可以将这些属性的其他名称指定为 inner() 的参数,但 ‘primary’‘secondary’ 是默认值)。输出中的多个地图项表示一对多关系。如果任一集合中的元素没有匹配项,则该元素不会出现在输出中。

使用 ImageCollection 输入的联接示例可直接应用于 FeatureCollection 输入。您还可以将 FeatureCollection 联接到 ImageCollection,反之亦然。请考虑以下内部联接示例:

Code Editor (JavaScript)

// Create the primary collection.
var primaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {foo: 0, label: 'a'}),
  ee.Feature(null, {foo: 1, label: 'b'}),
  ee.Feature(null, {foo: 1, label: 'c'}),
  ee.Feature(null, {foo: 2, label: 'd'}),
]);

// Create the secondary collection.
var secondaryFeatures = ee.FeatureCollection([
  ee.Feature(null, {bar: 1, label: 'e'}),
  ee.Feature(null, {bar: 1, label: 'f'}),
  ee.Feature(null, {bar: 2, label: 'g'}),
  ee.Feature(null, {bar: 3, label: 'h'}),
]);

// Use an equals filter to specify how the collections match.
var toyFilter = ee.Filter.equals({
  leftField: 'foo',
  rightField: 'bar'
});

// Define the join.
var innerJoin = ee.Join.inner('primary', 'secondary');

// Apply the join.
var toyJoin = innerJoin.apply(primaryFeatures, secondaryFeatures, toyFilter);

// Print the result.
print('Inner join toy example:', toyJoin);

Python 设置

如需了解 Python API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Create the primary collection.
primary_features = ee.FeatureCollection([
    ee.Feature(None, {'foo': 0, 'label': 'a'}),
    ee.Feature(None, {'foo': 1, 'label': 'b'}),
    ee.Feature(None, {'foo': 1, 'label': 'c'}),
    ee.Feature(None, {'foo': 2, 'label': 'd'}),
])

# Create the secondary collection.
secondary_features = ee.FeatureCollection([
    ee.Feature(None, {'bar': 1, 'label': 'e'}),
    ee.Feature(None, {'bar': 1, 'label': 'f'}),
    ee.Feature(None, {'bar': 2, 'label': 'g'}),
    ee.Feature(None, {'bar': 3, 'label': 'h'}),
])

# Use an equals filter to specify how the collections match.
toy_filter = ee.Filter.equals(leftField='foo', rightField='bar')

# Define the join.
inner_join = ee.Join.inner('primary', 'secondary')

# Apply the join.
toy_join = inner_join.apply(primary_features, secondary_features, toy_filter)

# Print the result.
display('Inner join toy example:', toy_join)

在前面的示例中,请注意,表之间的关系是在过滤条件中定义的,这表示字段 ‘foo’‘bar’ 是联接字段。然后,指定并应用内连接到集合。检查输出,您会发现每个可能的匹配都表示为一个 Feature

下面是一个有益的示例,请考虑联接 MODIS ImageCollection 对象。MODIS 质量数据有时存储在与图像数据分开的集合中,因此内连接非常适合用于联接这两个集合,以便应用质量数据。在本例中,图片获取时间相同,因此 equals 过滤器会负责指定这两个集合之间的这种关系:

Code Editor (JavaScript)

// Make a date filter to get images in this date range.
var dateFilter = ee.Filter.date('2014-01-01', '2014-02-01');

// Load a MODIS collection with EVI data.
var mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI')
    .filter(dateFilter);

// Load a MODIS collection with quality data.
var mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2')
    .filter(dateFilter);

// Define an inner join.
var innerJoin = ee.Join.inner();

// Specify an equals filter for image timestamps.
var filterTimeEq = ee.Filter.equals({
  leftField: 'system:time_start',
  rightField: 'system:time_start'
});

// Apply the join.
var innerJoinedMODIS = innerJoin.apply(mcd43a4, mcd43a2, filterTimeEq);

// Display the join result: a FeatureCollection.
print('Inner join output:', innerJoinedMODIS);

Python 设置

如需了解 Python API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Make a date filter to get images in this date range.
date_filter = ee.Filter.date('2014-01-01', '2014-02-01')

# Load a MODIS collection with EVI data.
mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI').filter(date_filter)

# Load a MODIS collection with quality data.
mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2').filter(date_filter)

# Define an inner join.
inner_join = ee.Join.inner()

# Specify an equals filter for image timestamps.
filter_time_eq = ee.Filter.equals(
    leftField='system:time_start', rightField='system:time_start'
)

# Apply the join.
inner_joined_modis = inner_join.apply(mcd43a4, mcd43a2, filter_time_eq)

# Display the join result: a FeatureCollection.
display('Inner join output:', inner_joined_modis)

如需使用输出 FeatureCollection 中的联接图片,请对输出使用组合函数 map()。例如,匹配的图片可以堆叠在一起,以便将质量带添加到图片数据中:

Code Editor (JavaScript)

// Map a function to merge the results in the output FeatureCollection.
var joinedMODIS = innerJoinedMODIS.map(function(feature) {
  return ee.Image.cat(feature.get('primary'), feature.get('secondary'));
});

// Print the result of merging.
print('Inner join, merged bands:', joinedMODIS);

Python 设置

如需了解 Python API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

import ee
import geemap.core as geemap

Colab (Python)

# Map a function to merge the results in the output FeatureCollection.
joined_modis = inner_joined_modis.map(
    lambda feature: ee.Image.cat(
        feature.get('primary'), feature.get('secondary')
    )
)

# Print the result of merging.
display("Inner join, merged 'bands':", joined_modis)

虽然此函数会映射到 FeatureCollection,但结果是 ImageCollection。生成的 ImageCollection 中的每个图片都包含主集合(在此示例中仅为 ‘EVI’)中的所有图片的波段,以及次级集合中匹配图片的所有波段(质量波段)。