ee.FeatureCollection.cluster
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nhóm từng đối tượng trong một tập hợp, thêm một cột mới vào từng đối tượng chứa số nhóm mà đối tượng đó được chỉ định.
Cách sử dụng | Giá trị trả về |
---|
FeatureCollection.cluster(clusterer, outputName) | FeatureCollection |
Đối số | Loại | Thông tin chi tiết |
---|
this: features | FeatureCollection | Tập hợp các đối tượng cần phân cụm. Mỗi đối tượng phải chứa tất cả các thuộc tính trong giản đồ của trình phân cụm. |
clusterer | Clusterer | Trình phân cụm để sử dụng. |
outputName | Chuỗi, mặc định: "cluster" | Tên của thuộc tính đầu ra sẽ được thêm. |
Ví dụ
Trình soạn thảo mã (JavaScript)
// Import a Sentinel-2 surface reflectance image.
var image = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
// Get the image geometry to define the geographical bounds of a point sample.
var imageBounds = image.geometry();
// Sample the image at a set of random points; a feature collection is returned.
var pointSampleFc = image.sample(
{region: imageBounds, scale: 20, numPixels: 1000, geometries: true});
// Instantiate a k-means clusterer and train it.
var clusterer = ee.Clusterer.wekaKMeans(5).train(pointSampleFc);
// Cluster the input using the trained clusterer; optionally specify the name
// of the output cluster ID property.
var clusteredFc = pointSampleFc.cluster(clusterer, 'spectral_cluster');
print('Note added "spectral_cluster" property for an example feature',
clusteredFc.first().toDictionary());
// Visualize the clusters by applying a unique color to each cluster ID.
var palette = ee.List(['8dd3c7', 'ffffb3', 'bebada', 'fb8072', '80b1d3']);
var clusterVis = clusteredFc.map(function(feature) {
return feature.set('style', {
color: palette.get(feature.get('spectral_cluster')),
});
}).style({styleProperty: 'style'});
// Display the points colored by cluster ID with the S2 image.
Map.setCenter(-122.35, 37.47, 9);
Map.addLayer(image, {bands: ['B4', 'B3', 'B2'], min: 0, max: 1500}, 'S2 image');
Map.addLayer(clusterVis, null, 'Clusters');
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
cho quá trình phát triển tương tác.
import ee
import geemap.core as geemap
Colab (Python)
# Import a Sentinel-2 surface reflectance image.
image = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
# Get the image geometry to define the geographical bounds of a point sample.
image_bounds = image.geometry()
# Sample the image at a set of random points a feature collection is returned.
point_sample_fc = image.sample(
region=image_bounds, scale=20, numPixels=1000, geometries=True
)
# Instantiate a k-means clusterer and train it.
clusterer = ee.Clusterer.wekaKMeans(5).train(point_sample_fc)
# Cluster the input using the trained clusterer optionally specify the name
# of the output cluster ID property.
clustered_fc = point_sample_fc.cluster(clusterer, 'spectral_cluster')
display(
'Note added "spectral_cluster" property for an example feature',
clustered_fc.first().toDictionary(),
)
# Visualize the clusters by applying a unique color to each cluster ID.
palette = ee.List(['8dd3c7', 'ffffb3', 'bebada', 'fb8072', '80b1d3'])
cluster_vis = clustered_fc.map(
lambda feature: feature.set(
'style', {'color': palette.get(feature.get('spectral_cluster'))}
)
).style(styleProperty='style')
# Display the points colored by cluster ID with the S2 image.
m = geemap.Map()
m.set_center(-122.35, 37.47, 9)
m.add_layer(
image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 1500}, 'S2 image'
)
m.add_layer(cluster_vis, None, 'Clusters')
m
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eGroups features within a collection into clusters based on a provided clusterer.\u003c/p\u003e\n"],["\u003cp\u003eAssigns each feature a cluster ID, stored in a new property with a user-defined name (defaults to "cluster").\u003c/p\u003e\n"],["\u003cp\u003eRequires a trained clusterer and a FeatureCollection where each feature contains the necessary properties for clustering.\u003c/p\u003e\n"],["\u003cp\u003eReturns a new FeatureCollection with the added cluster ID property.\u003c/p\u003e\n"]]],[],null,["# ee.FeatureCollection.cluster\n\nClusters each feature in a collection, adding a new column to each feature containing the cluster number to which it has been assigned.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|--------------------------------------------------------|-------------------|\n| FeatureCollection.cluster`(clusterer, `*outputName*`)` | FeatureCollection |\n\n| Argument | Type | Details |\n|------------------|----------------------------|----------------------------------------------------------------------------------------------------------------|\n| this: `features` | FeatureCollection | The collection of features to cluster. Each feature must contain all the properties in the clusterer's schema. |\n| `clusterer` | Clusterer | The clusterer to use. |\n| `outputName` | String, default: \"cluster\" | The name of the output property to be added. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// Import a Sentinel-2 surface reflectance image.\nvar image = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');\n\n// Get the image geometry to define the geographical bounds of a point sample.\nvar imageBounds = image.geometry();\n\n// Sample the image at a set of random points; a feature collection is returned.\nvar pointSampleFc = image.sample(\n {region: imageBounds, scale: 20, numPixels: 1000, geometries: true});\n\n// Instantiate a k-means clusterer and train it.\nvar clusterer = ee.Clusterer.wekaKMeans(5).train(pointSampleFc);\n\n// Cluster the input using the trained clusterer; optionally specify the name\n// of the output cluster ID property.\nvar clusteredFc = pointSampleFc.cluster(clusterer, 'spectral_cluster');\n\nprint('Note added \"spectral_cluster\" property for an example feature',\n clusteredFc.first().toDictionary());\n\n// Visualize the clusters by applying a unique color to each cluster ID.\nvar palette = ee.List(['8dd3c7', 'ffffb3', 'bebada', 'fb8072', '80b1d3']);\nvar clusterVis = clusteredFc.map(function(feature) {\n return feature.set('style', {\n color: palette.get(feature.get('spectral_cluster')),\n });\n}).style({styleProperty: 'style'});\n\n// Display the points colored by cluster ID with the S2 image.\nMap.setCenter(-122.35, 37.47, 9);\nMap.addLayer(image, {bands: ['B4', 'B3', 'B2'], min: 0, max: 1500}, 'S2 image');\nMap.addLayer(clusterVis, null, 'Clusters');\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# Import a Sentinel-2 surface reflectance image.\nimage = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\n\n# Get the image geometry to define the geographical bounds of a point sample.\nimage_bounds = image.geometry()\n\n# Sample the image at a set of random points a feature collection is returned.\npoint_sample_fc = image.sample(\n region=image_bounds, scale=20, numPixels=1000, geometries=True\n)\n\n# Instantiate a k-means clusterer and train it.\nclusterer = ee.Clusterer.wekaKMeans(5).train(point_sample_fc)\n\n# Cluster the input using the trained clusterer optionally specify the name\n# of the output cluster ID property.\nclustered_fc = point_sample_fc.cluster(clusterer, 'spectral_cluster')\n\ndisplay(\n 'Note added \"spectral_cluster\" property for an example feature',\n clustered_fc.first().toDictionary(),\n)\n\n# Visualize the clusters by applying a unique color to each cluster ID.\npalette = ee.List(['8dd3c7', 'ffffb3', 'bebada', 'fb8072', '80b1d3'])\ncluster_vis = clustered_fc.map(\n lambda feature: feature.set(\n 'style', {'color': palette.get(feature.get('spectral_cluster'))}\n )\n).style(styleProperty='style')\n\n# Display the points colored by cluster ID with the S2 image.\nm = geemap.Map()\nm.set_center(-122.35, 37.47, 9)\nm.add_layer(\n image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 1500}, 'S2 image'\n)\nm.add_layer(cluster_vis, None, 'Clusters')\nm\n```"]]