空间联接

集合可以按空间位置以及房源价值进行联接。如需基于空间位置进行联接,请使用指定了 .geo 联接字段的 withinDistance() 过滤条件。.geo 字段表示要使用相应项的几何图形来计算距离测量值。例如,假设有一个任务是查找美国优胜美地国家公园 100 公里范围内的所有 发电厂。为此,请对几何图形字段使用过滤器,并使用 distance 参数将最远距离设置为 100 公里:

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);

Python 设置

如需了解 Python API 以及如何使用 geemap 进行交互式开发,请参阅 Python 环境页面。

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 联接到另一个 FeatureCollectionsaveAll() 联接会在 primary 集合中的每个地图项上设置一个属性 (points),用于存储相应地图项 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() 过滤器那样存储距离。输出应如下所示。

CA WRS2 联接
图 1. 显示每个美国州内交叉电厂数量的条形图。