Thông báo: Tất cả dự án phi thương mại đã đăng ký sử dụng Earth Engine trước
ngày 15 tháng 4 năm 2025 phải
xác minh điều kiện sử dụng phi thương mại để duy trì quyền truy cập. Nếu bạn chưa xác minh trước ngày 26 tháng 9 năm 2025, quyền truy cập của bạn có thể bị tạm ngưng.
ee.Image.sample
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.
Lấy mẫu các pixel của một hình ảnh, trả về các pixel đó dưới dạng một FeatureCollection. Mỗi đối tượng sẽ có 1 thuộc tính cho mỗi dải tần trong hình ảnh đầu vào. Xin lưu ý rằng hành vi mặc định là loại bỏ các đối tượng giao với những pixel được che, dẫn đến các thuộc tính có giá trị rỗng (xem đối số dropNulls).
Cách sử dụng | Giá trị trả về |
---|
Image.sample(region, scale, projection, factor, numPixels, seed, dropNulls, tileScale, geometries) | FeatureCollection |
Đối số | Loại | Thông tin chi tiết |
---|
this: image | Hình ảnh | Hình ảnh để lấy mẫu. |
region | Hình học, mặc định: null | Khu vực lấy mẫu. Nếu không được chỉ định, sẽ sử dụng toàn bộ kích thước của hình ảnh. |
scale | Float, mặc định: null | Một tỷ lệ danh nghĩa tính bằng mét của phép chiếu để lấy mẫu. |
projection | Phép chiếu, mặc định: null | Phép chiếu để lấy mẫu. Nếu không được chỉ định, thì phép chiếu của dải tần đầu tiên của hình ảnh sẽ được dùng. Nếu được chỉ định ngoài tỷ lệ, hãy điều chỉnh tỷ lệ theo tỷ lệ đã chỉ định. |
factor | Float, mặc định: null | Hệ số lấy mẫu phụ, trong khoảng (0, 1]. Nếu được chỉ định, bạn không được chỉ định "numPixels". Mặc định là không lấy mẫu phụ. |
numPixels | Dài, mặc định: null | Số lượng pixel ước chừng cần lấy mẫu. Nếu được chỉ định, bạn không được chỉ định "hệ số". |
seed | Số nguyên, mặc định: 0 | Một dữ liệu gốc ngẫu nhiên để dùng cho việc lấy mẫu phụ. |
dropNulls | Boolean, mặc định: true | Lọc kết quả sau để loại bỏ các đối tượng có thuộc tính giá trị rỗng. |
tileScale | Số thực, mặc định: 1 | Hệ số tỷ lệ dùng để giảm kích thước ô tổng hợp; sử dụng tileScale lớn hơn (ví dụ: 2 hoặc 4) có thể cho phép các phép tính hết bộ nhớ với giá trị mặc định. |
geometries | Boolean, mặc định: false | Nếu đúng, sẽ thêm tâm của pixel được lấy mẫu làm thuộc tính hình học của đối tượng đầu ra. Nếu không, các hình học sẽ bị bỏ qua (giúp tiết kiệm bộ nhớ). |
Ví dụ
Trình soạn thảo mã (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());
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)
# 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())
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."],[],[]]