公告 :所有在
2025 年 4 月 15 日 之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件 ,才能继续使用 Earth Engine。
发送反馈
ee.FeatureCollection.cluster
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对集合中的每个特征进行聚类,并为每个特征添加一个新列,其中包含已分配的聚类编号。
用法 返回 FeatureCollection. cluster (clusterer, outputName )
FeatureCollection
参数 类型 详细信息 此:features
FeatureCollection 要聚类的特征集合。每个特征都必须包含聚类器架构中的所有属性。 clusterer
聚类器 要使用的聚类器。 outputName
字符串,默认值:“cluster” 要添加的输出属性的名称。
示例
代码编辑器 (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' );
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境 页面。
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
发送反馈
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可 获得了许可,并且代码示例已根据 Apache 2.0 许可 获得了许可。有关详情,请参阅 Google 开发者网站政策 。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
需要向我们提供更多信息?
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-26。"],[[["Groups features within a collection into clusters based on a provided clusterer."],["Assigns each feature a cluster ID, stored in a new property with a user-defined name (defaults to \"cluster\")."],["Requires a trained clusterer and a FeatureCollection where each feature contains the necessary properties for clustering."],["Returns a new FeatureCollection with the added cluster ID property."]]],[]]