如要列舉兩個集合元素之間的所有比對項目,請使用 ee.Join.inner()
。內部彙整的輸出結果是 FeatureCollection
(即使將一個 ImageCollection
彙整到另一個 ImageCollection
也是如此)。輸出結果中的每個地圖項目都代表一個相符項目,相符元素會儲存在地圖項目的兩個屬性中。舉例來說,feature.get('primary')
是主要集合中的元素,與 feature.get('secondary')
中儲存的次要集合元素相符。(這些屬性的不同名稱可指定為 inner()
的引數,但 ‘primary’
和 ‘secondary’
是預設值)。一對多關係會在輸出內容中以多個功能表示。如果任一集合中的元素沒有相符項目,就不會出現在輸出內容中。
使用 ImageCollection
輸入內容的彙整範例,可直接套用至 FeatureCollection
輸入內容,無須修改。您也可以將 FeatureCollection
與 ImageCollection
連結,反之亦然。請參考以下內部聯結的玩具範例:
程式碼編輯器 (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);
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 品質資料有時會儲存在與圖像資料不同的集合中,因此內部彙整可方便彙整這兩個集合,以便套用品質資料。在這種情況下,圖片擷取時間相同,因此相等篩選器會處理在兩個集合之間指定這項關係的工作:
程式碼編輯器 (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);
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()
的組合函式。舉例來說,您可以將相符的圖片堆疊在一起,以便將品質頻帶加入圖片資料:
程式碼編輯器 (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);
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’
) 中的所有圖片頻帶,以及次要集合中相符圖片的所有頻帶 (品質頻帶)。