אפשר לצרף אוספים לפי מיקום מרחבי וגם לפי ערכי נכסים. כדי לבצע איחוד על סמך מיקום מרחבי, משתמשים במסנן withinDistance()
עם שדות איחוד .geo
שצוינו. השדה .geo
מציין שצריך להשתמש בגיאומטריה של הפריט כדי לחשב את מדד המרחק. לדוגמה, נניח שרוצים למצוא את כל
תחנות הכוח במרחק 100 ק"מ מהפארק הלאומי יוסמיטי, ארה"ב. לשם כך, משתמשים במסנן בשדות הגיאומטריה, כשהמרחק המקסימלי מוגדר ל-100 קילומטרים באמצעות הפרמטר distance
:
Code Editor (JavaScript)
// Load a primary collection: protected areas (Yosemite National Park). var primary = ee.FeatureCollection("WCMC/WDPA/current/polygons") .filter(ee.Filter.eq('NAME', 'Yosemite National Park')); // Load a secondary collection: power plants. var powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants'); // Define a spatial filter, with distance 100 km. var distFilter = ee.Filter.withinDistance({ distance: 100000, leftField: '.geo', rightField: '.geo', maxError: 10 }); // Define a saveAll join. var distSaveAll = ee.Join.saveAll({ matchesKey: 'points', measureKey: 'distance' }); // Apply the join. var spatialJoined = distSaveAll.apply(primary, powerPlants, distFilter); // Print the result. print(spatialJoined);
import ee import geemap.core as geemap
Colab (Python)
# Load a primary collection: protected areas (Yosemite National Park). primary = ee.FeatureCollection('WCMC/WDPA/current/polygons').filter( ee.Filter.eq('NAME', 'Yosemite National Park') ) # Load a secondary collection: power plants. power_plants = ee.FeatureCollection('WRI/GPPD/power_plants') # Define a spatial filter, with distance 100 km. dist_filter = ee.Filter.withinDistance( distance=100000, leftField='.geo', rightField='.geo', maxError=10 ) # Define a saveAll join. dist_save_all = ee.Join.saveAll(matchesKey='points', measureKey='distance') # Apply the join. spatial_joined = dist_save_all.apply(primary, power_plants, dist_filter) # Print the result. display(spatial_joined)
שימו לב שבדוגמה הקודמת FeatureCollection
מצורף ל-FeatureCollection
אחר. השילוב saveAll()
מגדיר מאפיין (points
) לכל תכונה באוסף primary
, שמאחסן רשימה של הנקודות שנמצאות בטווח של 100 ק"מ מהתכונה. המרחק של כל נקודה מהתכונה מאוחסן במאפיין distance
של כל נקודה שמצורפת.
אפשר להשתמש במיזוגים מרחביים גם כדי לזהות אילו תכונות בקולקציה אחת חופפות לאלה בקולקציה אחרת. לדוגמה, נניח שיש שתי אוספים של תכונות: אוסף primary
שמכיל פוליגונים שמייצגים את הגבולות של מדינות ארה"ב, ואוסף secondary
שמכיל מיקומי נקודות שמייצגים תחנות כוח. נניח שצריך לקבוע את המספר שמצטלב עם כל מצב. אפשר לעשות זאת באמצעות צירוף מרחבי באופן הבא:
Code Editor (JavaScript)
// Load the primary collection: US state boundaries. var states = ee.FeatureCollection('TIGER/2018/States'); // Load the secondary collection: power plants. var powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants'); // Define a spatial filter as geometries that intersect. var spatialFilter = ee.Filter.intersects({ leftField: '.geo', rightField: '.geo', maxError: 10 }); // Define a save all join. var saveAllJoin = ee.Join.saveAll({ matchesKey: 'power_plants', }); // Apply the join. var intersectJoined = saveAllJoin.apply(states, powerPlants, spatialFilter); // Add power plant count per state as a property. intersectJoined = intersectJoined.map(function(state) { // Get "power_plant" intersection list, count how many intersected this state. var nPowerPlants = ee.List(state.get('power_plants')).size(); // Return the state feature with a new property: power plant count. return state.set('n_power_plants', nPowerPlants); }); // Make a bar chart for the number of power plants per state. var chart = ui.Chart.feature.byFeature(intersectJoined, 'NAME', 'n_power_plants') .setChartType('ColumnChart') .setSeriesNames({n_power_plants: 'Power plants'}) .setOptions({ title: 'Power plants per state', hAxis: {title: 'State'}, vAxis: {title: 'Frequency'}}); // Print the chart to the console. print(chart);
בדוגמה הקודמת, חשוב לשים לב שהמסנן intersects()
לא שומר מרחק כמו המסנן withinDistance()
. הפלט אמור להיראות כמו באיור 1.
