ee.FeatureCollection.merge

Hợp nhất hai bộ sưu tập thành một. Kết quả chứa tất cả các phần tử có trong một trong hai bộ sưu tập.

Các phần tử trong tập hợp đầu tiên sẽ có mã nhận dạng bắt đầu bằng "1" và các phần tử trong tập hợp thứ hai sẽ có mã nhận dạng bắt đầu bằng "2".

Lưu ý: Nếu bạn cần hợp nhất nhiều bộ sưu tập, hãy cân nhắc việc đặt tất cả các bộ sưu tập đó vào một bộ sưu tập và sử dụng FeatureCollection.flatten(). Việc sử dụng FeatureCollection.merge() nhiều lần sẽ dẫn đến việc tăng dần độ dài của mã nhận dạng phần tử và giảm hiệu suất.

Cách sử dụngGiá trị trả về
FeatureCollection.merge(collection2)FeatureCollection
Đối sốLoạiThông tin chi tiết
this: collection1FeatureCollectionBộ sưu tập đầu tiên cần hợp nhất.
collection2FeatureCollectionBộ sưu tập thứ hai cần hợp nhất.

Ví dụ

Trình soạn thảo mã (JavaScript)

// FeatureCollection of points representing forest cover.
var fcForest = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([-122.248, 37.238]),
             {'id': 0, 'cover_class': 'forest'}),
  ee.Feature(ee.Geometry.Point([-122.204, 37.222]),
             {'id': 1, 'cover_class': 'forest'}),
  ee.Feature(ee.Geometry.Point([-122.110, 37.199]),
             {'id': 2, 'cover_class': 'forest'})
]);

// FeatureCollection of points representing urban cover.
var fcUrban = ee.FeatureCollection([
  ee.Feature(ee.Geometry.Point([-121.953, 37.372]),
             {'id': 0, 'cover_class': 'urban'}),
  ee.Feature(ee.Geometry.Point([-121.861, 37.308]),
             {'id': 1, 'cover_class': 'urban'}),
  ee.Feature(ee.Geometry.Point([-121.984, 37.372]),
             {'id': 2, 'cover_class': 'urban'})
]);

// Merge the two FeatureCollections into one.
var fcMerged = fcForest.merge(fcUrban);

// Display FeatureCollections on the map.
Map.setCenter(-122.034, 37.296, 11);
Map.addLayer(fcForest, {color: 'green'}, 'Forest points');
Map.addLayer(fcUrban, {color: 'grey'}, 'Urban points');
Map.addLayer(fcMerged, {color: 'yellow'}, 'Protected areas merged');

Thiết lập Python

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

import ee
import geemap.core as geemap

Colab (Python)

# FeatureCollection of points representing forest cover.
fc_forest = ee.FeatureCollection([
    ee.Feature(
        ee.Geometry.Point([-122.248, 37.238]),
        {
            'id': 0,
            'cover_class': 'forest',
            'id': 0,
            'cover_class': 'forest',
        },
    ),
    ee.Feature(
        ee.Geometry.Point([-122.204, 37.222]),
        {
            'id': 1,
            'cover_class': 'forest',
            'id': 1,
            'cover_class': 'forest',
        },
    ),
    ee.Feature(
        ee.Geometry.Point([-122.110, 37.199]),
        {
            'id': 2,
            'cover_class': 'forest',
            'id': 2,
            'cover_class': 'forest',
        },
    ),
])

# FeatureCollection of points representing urban cover.
fc_urban = ee.FeatureCollection([
    ee.Feature(
        ee.Geometry.Point([-121.953, 37.372]),
        {'id': 0, 'cover_class': 'urban', 'id': 0, 'cover_class': 'urban'},
    ),
    ee.Feature(
        ee.Geometry.Point([-121.861, 37.308]),
        {'id': 1, 'cover_class': 'urban', 'id': 1, 'cover_class': 'urban'},
    ),
    ee.Feature(
        ee.Geometry.Point([-121.984, 37.372]),
        {'id': 2, 'cover_class': 'urban', 'id': 2, 'cover_class': 'urban'},
    ),
])

# Merge the two FeatureCollections into one.
fc_merged = fc_forest.merge(fc_urban)

# Display FeatureCollections on the map.
m = geemap.Map()
m.set_center(-122.034, 37.296, 11)
m.add_layer(fc_forest, {'color': 'green'}, 'Forest points')
m.add_layer(fc_urban, {'color': 'grey'}, 'Urban points')
m.add_layer(fc_merged, {'color': 'yellow'}, 'Protected areas merged')
m