Koleksi dapat digabungkan berdasarkan lokasi spasial serta nilai properti. Untuk bergabung berdasarkan lokasi spasial, gunakan filter withinDistance()
dengan kolom join .geo
yang ditentukan. Kolom .geo
menunjukkan bahwa geometri item akan digunakan untuk menghitung metrik jarak. Misalnya, pertimbangkan tugas untuk menemukan semua
pembangkit listrik dalam radius 100 kilometer dari Taman Nasional Yosemite, Amerika Serikat. Untuk tujuan tersebut, gunakan filter pada kolom geometri, dengan jarak maksimum ditetapkan ke 100 kilometer menggunakan parameter distance
:
Editor Kode (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)
Perhatikan bahwa contoh sebelumnya menggabungkan FeatureCollection
ke FeatureCollection
lain. Join saveAll()
menetapkan properti (points
) pada setiap fitur dalam koleksi primary
yang menyimpan daftar titik dalam jarak 100 km dari fitur. Jarak setiap titik ke fitur disimpan di properti distance
dari setiap titik yang terhubung.
Join spasial juga dapat digunakan untuk mengidentifikasi fitur mana
dalam satu koleksi yang bersimpangan dengan fitur dalam koleksi lain. Misalnya, pertimbangkan dua kumpulan fitur: kumpulan primary
yang berisi poligon yang mewakili batas negara bagian AS, kumpulan secondary
yang berisi lokasi titik yang mewakili pembangkit listrik. Misalnya, ada kebutuhan untuk menentukan jumlah yang berpotongan dengan setiap
status. Hal ini dapat dilakukan dengan join spasial sebagai berikut:
Editor Kode (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);
Pada contoh sebelumnya, perhatikan bahwa filter intersects()
tidak menyimpan jarak seperti filter withinDistance()
. Output akan terlihat seperti
Gambar 1.
