공간적 조인

컬렉션은 공간 위치뿐만 아니라 속성 값으로도 조인할 수 있습니다. 공간적 위치를 기준으로 조인하려면 .geo 조인 필드가 지정된 withinDistance() 필터를 사용하세요. .geo 필드는 항목의 도형이 거리 측정항목을 계산하는 데 사용됨을 나타냅니다. 예를 들어 미국 요세미티 국립공원에서 100km 이내에 있는 모든 발전소를 찾는 작업을 생각해 보세요. 이를 위해 distance 매개변수를 사용하여 최대 거리를 100km로 설정하고 도형 필드에 필터를 사용합니다.

코드 편집기 (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를 다른 FeatureCollection에 조인합니다. saveAll() 조인은 primary 컬렉션의 각 지형지물에 지형지물에서 100km 이내의 점 목록을 저장하는 속성(points)을 설정합니다. 각 지점과 지형지물의 거리는 각 결합된 지점의 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. 미국 각 주의 교차하는 발전소 수를 보여주는 막대 그래프입니다.