公告 :所有在
2025 年 4 月 15 日 之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件 ,才能继续使用 Earth Engine。
发送反馈
ee.Image.sample
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对图片的像素进行采样,并以 FeatureCollection 形式返回。每个特征在输入图片中每个波段都将有 1 个属性。请注意,默认行为是舍弃与遮盖像素相交的特征,这会导致属性值为 null(请参阅 dropNulls 实参)。
用法 返回 Image. sample (region , scale , projection , factor , numPixels , seed , dropNulls , tileScale , geometries )
FeatureCollection
参数 类型 详细信息 此:image
图片 要抽样的图片。 region
几何图形,默认值:null 要从中抽样的区域。如果未指定,则使用映像的整个占用空间。 scale
浮点数,默认值:null 要采样的投影的标称比例(以米为单位)。 projection
投影,默认值:null 要进行抽样的投影。如果未指定,则使用映像第一个波段的投影。如果除了缩放比例之外还指定了此参数,则会重新缩放到指定的缩放比例。 factor
浮点数,默认值:null 一个介于 (0, 1] 范围内的子采样率。如果指定了此参数,则不得指定“numPixels”。默认值为不进行子采样。 numPixels
Long,默认值:null 要采样的像素的大致数量。如果指定了此字段,则不得指定“factor”。 seed
整数,默认值:0 用于子采样的随机化种子。 dropNulls
布尔值,默认值:true 对结果进行后过滤,以舍弃具有 null 值属性的特征。 tileScale
浮点数,默认值:1 用于减小聚合图块大小的缩放比例;使用较大的 tileScale(例如,2 或 4)可能会启用内存不足的计算(使用默认值)。 geometries
布尔值,默认值:false 如果为 true,则将采样像素的中心添加为输出要素的几何属性。否则,系统会省略几何图形(节省内存)。
示例
代码编辑器 (JavaScript)
// Demonstrate extracting pixels from an image as features with
// ee.Image.sample(), and show how the features are aligned with the pixels.
// An image with one band of elevation data.
var image = ee . Image ( 'CGIAR/SRTM90_V4' );
var VIS_MIN = 1620 ;
var VIS_MAX = 1650 ;
Map . addLayer ( image , { min : VIS_MIN , max : VIS_MAX }, 'SRTM' );
// Region to sample.
var region = ee . Geometry . Polygon (
[[[ - 110.006 , 40.002 ],
[ - 110.006 , 39.999 ],
[ - 109.995 , 39.999 ],
[ - 109.995 , 40.002 ]]], null , false );
// Show region on the map.
Map . setCenter ( - 110 , 40 , 16 );
Map . addLayer ( ee . FeatureCollection ([ region ]). style ({ "color" : "00FF0022" }));
// Perform sampling; convert image pixels to features.
var samples = image . sample ({
region : region ,
// Default (false) is no geometries in the output.
// When set to true, each feature has a Point geometry at the center of the
// image pixel.
geometries : true ,
// The scale is not specified, so the resolution of the image will be used,
// and there is a feature for every pixel. If we give a scale parameter, the
// image will be resampled and there will be more or fewer features.
//
// scale: 200,
});
// Visualize sample data using ee.FeatureCollection.style().
var styled = samples
. map ( function ( feature ) {
return feature . set ( 'style' , {
pointSize : feature . getNumber ( 'elevation' ). unitScale ( VIS_MIN , VIS_MAX )
. multiply ( 15 ),
});
})
. style ({
color : '000000FF' ,
fillColor : '00000000' ,
styleProperty : 'style' ,
neighborhood : 6 , // increase to correctly draw large points
});
Map . addLayer ( styled );
// Each sample feature has a point geometry and a property named 'elevation'
// corresponding to the band named 'elevation' of the image. If there are
// multiple bands they will become multiple properties. This will print:
//
// geometry: Point (-110.01, 40.00)
// properties:
// elevation: 1639
print ( samples . first ());
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境 页面。
import ee
import geemap.core as geemap
Colab (Python)
# Demonstrate extracting pixels from an image as features with
# ee.Image.sample(), and show how the features are aligned with the pixels.
# An image with one band of elevation data.
image = ee . Image ( 'CGIAR/SRTM90_V4' )
vis_min = 1620
vis_max = 1650
m = geemap . Map ()
m . add_layer ( image , { 'min' : vis_min , 'max' : vis_max }, 'SRTM' )
# Region to sample.
region = ee . Geometry . Polygon (
[[
[ - 110.006 , 40.002 ],
[ - 110.006 , 39.999 ],
[ - 109.995 , 39.999 ],
[ - 109.995 , 40.002 ],
]],
None ,
False ,
)
# Show region on the map.
m . set_center ( - 110 , 40 , 16 )
m . add_layer ( ee . FeatureCollection ([ region ]) . style ( color = '00FF0022' ))
# Perform sampling convert image pixels to features.
samples = image . sample (
region = region ,
# Default (False) is no geometries in the output.
# When set to True, each feature has a Point geometry at the center of the
# image pixel.
geometries = True ,
# The scale is not specified, so the resolution of the image will be used,
# and there is a feature for every pixel. If we give a scale parameter, the
# image will be resampled and there will be more or fewer features.
#
# scale=200,
)
def scale_point_size ( feature ):
elevation = feature . getNumber ( 'elevation' )
point_size = elevation . unitScale ( vis_min , vis_max ) . multiply ( 15 )
feature . set ( 'style' , { 'pointSize' : point_size })
return feature
# Visualize sample data using ee.FeatureCollection.style().
styled = samples . map ( scale_point_size ) . style (
color = '000000FF' ,
fillColor = '00000000' ,
styleProperty = 'style' ,
neighborhood = 6 , # increase to correctly draw large points
)
m . add_layer ( styled )
display ( m )
# Each sample feature has a point geometry and a property named 'elevation'
# corresponding to the band named 'elevation' of the image. If there are
# multiple bands they will become multiple properties. This will print:
#
# geometry: Point (-110.01, 40.00)
# properties:
# elevation: 1639
display ( samples . first ())
发送反馈
如未另行说明,那么本页面中的内容已根据知识共享署名 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。"],[[["`Image.sample()` extracts pixel values from an image and converts them into a FeatureCollection, with each feature representing a pixel and its properties corresponding to the band values."],["You can define a region of interest, control the sampling scale and projection, and adjust the number of sampled pixels using arguments like `region`, `scale`, `projection`, `factor`, and `numPixels`."],["Sampled features can optionally include point geometries representing pixel centers using the `geometries` argument."],["By default, features associated with masked pixels (resulting in null-valued properties) are excluded, which can be controlled using the `dropNulls` argument."]]],[]]