コレクションは、空間的位置情報だけでなく、プロパティ値でも結合できます。空間的位置に基づいて結合するには、.geo
結合フィールドを指定した withinDistance()
フィルタを使用します。.geo
フィールドは、アイテムのジオメトリが距離指標の計算に使用されることを示します。たとえば、米国のヨセミテ国立公園から 100 km 以内のすべての
発電所を見つけるタスクについて考えてみましょう。そのためには、distance
パラメータを使用して最大距離を 100 キロメートルに設定し、ジオメトリ フィールドでフィルタを使用します。
コードエディタ(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()
結合は、primary
コレクション内の各特徴にプロパティ(points
)を設定し、特徴から 100 km 以内の点のリストを格納します。各ポイントから対象物までの距離は、結合された各ポイントの distance
プロパティに保存されます。
空間結合を使用して、あるコレクション内のどの特徴が別のコレクション内の特徴と交差しているかを特定することもできます。たとえば、2 つの特徴コレクション(米国の州境を表すポリゴンを含む primary
コレクションと、発電所を表すポイント ロケーションを含む secondary
コレクション)について考えてみましょう。各州を交差する数を特定する必要がある場合を考えてみましょう。これは、次のように空間結合で実現できます。
コードエディタ(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 のようになります。
