空間彙整

集合可以根據空間位置和資源屬性值進行彙整。如要根據空間位置進行彙整,請使用 withinDistance() 篩選器,並指定 .geo 彙整欄位。.geo 欄位表示項目的幾何圖形將用於計算距離指標。舉例來說,假設您要找出美國優勝美地國家公園 100 公里範圍內的所有 發電廠。為此,請在幾何圖形欄位上使用篩選器,並使用 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);

Python 設定

請參閱「 Python 環境」頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

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 公里範圍內的點清單。每個點與地物的距離會儲存在每個已彙整點的 distance 屬性中。

空間彙整也可用於找出一個集合中的哪些地圖項目與另一個集合中的地圖項目相交。舉例來說,請考慮兩個地圖項目集合:一個 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 所示。

CA WRS2 彙整
圖 1. 長條圖顯示各州交會的發電廠數量。