כדי למנות את כל ההתאמות בין הרכיבים של שתי קולקציות, משתמשים ב-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);
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
אחד.
לדוגמה, אפשר לצרף אובייקטים מסוג ImageCollection
של MODIS. לפעמים נתוני האיכות של MODIS מאוחסנים באוסף נפרד מנתוני התמונות, ולכן כדאי להשתמש בחיבור פנימי כדי לצרף את שני האוספים ולהחיל את נתוני האיכות. במקרה הזה, זמני צילום התמונות זהים, ולכן מסנן שווה יטפל בתיאור הקשר הזה בין שני האוספים:
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);
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);
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’
) ואת כל הפסים של התמונה התואמת באוסף המשני (פסי האיכות).