Liên kết nội bộ

Để liệt kê tất cả các kết quả trùng khớp giữa các phần tử của hai bộ sưu tập, hãy sử dụng ee.Join.inner(). Kết quả của một phép nối nội bộ là một FeatureCollection (ngay cả khi nối một ImageCollection với một ImageCollection khác). Mỗi đặc điểm trong kết quả đại diện cho một kết quả so khớp, trong đó các phần tử trùng khớp được lưu trữ trong hai thuộc tính của đặc điểm. Ví dụ: feature.get('primary') là phần tử trong bộ sưu tập chính khớp với phần tử từ bộ sưu tập phụ được lưu trữ trong feature.get('secondary'). (Bạn có thể chỉ định tên khác cho các thuộc tính này làm đối số cho inner(), nhưng ‘primary’‘secondary’ là mặc định). Mối quan hệ một với nhiều được biểu thị bằng nhiều đặc điểm trong kết quả. Nếu một phần tử trong một trong hai tập hợp không có phần tử trùng khớp, thì phần tử đó sẽ không xuất hiện trong kết quả.

Các ví dụ về việc kết hợp sử dụng dữ liệu đầu vào ImageCollection sẽ áp dụng mà không cần sửa đổi dữ liệu đầu vào FeatureCollection. Bạn cũng có thể kết hợp FeatureCollection với ImageCollection và ngược lại. Hãy xem xét ví dụ sau về phép nối nội bộ:

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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)

Trong ví dụ trước, hãy lưu ý rằng mối quan hệ giữa các bảng được xác định trong bộ lọc, cho biết rằng các trường ‘foo’‘bar’ là các trường nối. Sau đó, một phép nối nội bộ sẽ được chỉ định và áp dụng cho các bộ sưu tập. Kiểm tra kết quả và quan sát thấy mỗi kết quả trùng khớp có thể được biểu thị dưới dạng một Feature.

Để biết ví dụ về động lực, hãy cân nhắc việc kết hợp các đối tượng ImageCollection MODIS. Dữ liệu chất lượng MODIS đôi khi được lưu trữ trong một tập hợp riêng biệt với dữ liệu hình ảnh, vì vậy, một phép nối nội bộ sẽ thuận tiện cho việc kết hợp hai tập hợp này để áp dụng dữ liệu chất lượng. Trong trường hợp này, thời gian thu thập hình ảnh giống hệt nhau, vì vậy, bộ lọc bằng nhau sẽ xử lý việc chỉ định mối quan hệ này giữa hai bộ sưu tập:

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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)

Để sử dụng các hình ảnh đã kết hợp trong FeatureCollection đầu ra, map() là một hàm kết hợp trên đầu ra. Ví dụ: bạn có thể xếp chồng các hình ảnh trùng khớp với nhau để thêm các dải chất lượng vào dữ liệu hình ảnh:

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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)

Mặc dù hàm này được liên kết qua FeatureCollection, nhưng kết quả là một ImageCollection. Mỗi hình ảnh trong ImageCollection kết quả có tất cả các dải của hình ảnh trong bộ sưu tập chính (trong ví dụ này chỉ là ‘EVI’) và tất cả các dải của hình ảnh trùng khớp trong bộ sưu tập phụ (các dải chất lượng).