Liên kết không gian

Bạn có thể kết hợp các bộ sưu tập theo vị trí không gian cũng như theo giá trị thuộc tính. Để kết hợp dựa trên vị trí không gian, hãy sử dụng bộ lọc withinDistance() với các trường kết hợp .geo được chỉ định. Trường .geo cho biết hình học của mục sẽ được dùng để tính toán chỉ số khoảng cách. Ví dụ: hãy xem xét nhiệm vụ tìm tất cả nhà máy điện trong phạm vi 100 km từ Công viên quốc gia Yosemite, Hoa Kỳ. Để làm việc đó, hãy sử dụng bộ lọc trên các trường hình học, với khoảng cách tối đa được đặt thành 100 km bằng cách sử dụng tham số distance:

Trình soạn thảo mã (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);

Thiết lập Python

Hãy xem trang Môi trường Python để biết thông tin về API Python và cách sử dụng geemap để phát triển tương tác.

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)

Xin lưu ý rằng ví dụ trước đã kết hợp một FeatureCollection với một FeatureCollection khác. Liên kết saveAll() đặt một thuộc tính (points) trên mỗi đối tượng trong tập hợp primary. Tập hợp này lưu trữ danh sách các điểm trong phạm vi 100 km của đối tượng. Khoảng cách của mỗi điểm đến đối tượng được lưu trữ trong thuộc tính distance của mỗi điểm được nối.

Bạn cũng có thể sử dụng phép nối không gian để xác định những đối tượng trong một tập hợp giao nhau với những đối tượng trong một tập hợp khác. Ví dụ: hãy xem xét hai tập hợp tính năng: một tập hợp primary chứa các đa giác biểu thị ranh giới của các tiểu bang ở Hoa Kỳ, một tập hợp secondary chứa các vị trí điểm biểu thị nhà máy điện. Giả sử cần xác định số giao nhau của mỗi trạng thái. Bạn có thể thực hiện việc này bằng cách sử dụng phép nối không gian như sau:

Trình soạn thảo mã (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);

Trong ví dụ trước, lưu ý rằng bộ lọc intersects() không lưu trữ khoảng cách như bộ lọc withinDistance(). Kết quả sẽ có dạng như Hình 1.

Kết hợp CA WRS2
Hình 1. Biểu đồ thanh cho thấy số lượng nhà máy điện giao nhau với mỗi tiểu bang ở Hoa Kỳ.