公告 :凡是在
2025 年 4 月 15 日前 註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格 ,才能繼續存取。如未在 2025 年 9 月 26 日前完成驗證,存取權可能會暫停。
提供意見
ee.Image.sampleRegions
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
將與一或多個區域相交的圖片像素 (以指定比例) 轉換為特徵,並以 FeatureCollection 形式傳回。每個輸出特徵都會有輸入圖片每個波段的屬性,以及從輸入特徵複製的任何指定屬性。
請注意,幾何圖形會對齊像素中心。
用量 傳回 Image. sampleRegions (collection, properties , scale , projection , tileScale , geometries )
FeatureCollection
引數 類型 詳細資料 這個:image
圖片 要取樣的圖片。 collection
FeatureCollection 要取樣的區域。 properties
清單,預設值為空值 要從每個輸入特徵複製的屬性清單。預設為所有非系統屬性。 scale
浮點值,預設值為空值 投影的公尺名義比例,用於取樣。如未指定,系統會使用圖片第一個波段的比例。 projection
投影,預設值:null 要取樣的投影。如未指定,系統會使用圖片第一個波段的投影。如果除了比例外還指定了其他值,系統會重新調整為指定比例。 tileScale
浮點值,預設值為 1 用於縮小聚合圖塊大小的縮放比例因數;使用較大的 tileScale (例如 2 或 4) 可能會啟用運算,導致記憶體不足。 geometries
布林值,預設值為 false 如果設為 true,結果會包含每個取樣像素的點幾何。否則系統會省略幾何圖形 (節省記憶體)。
範例
程式碼編輯器 (JavaScript)
// A Sentinel-2 surface reflectance image.
var img = ee . Image ( 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG' );
Map . setCenter ( - 122.503881 , 37.765588 , 18 );
Map . addLayer ( img , { bands : [ 'B11' , 'B8' , 'B3' ], min : 100 , max : 4500 }, 'img' );
// A feature collection with two polygon regions each intersecting 36
// pixels at 10 m scale.
var fcPolygon = ee . FeatureCollection ([
ee . Feature ( ee . Geometry . Rectangle (
- 122.50620929 , 37.76502806 , - 122.50552264 , 37.76556663 ), { id : 0 }),
ee . Feature ( ee . Geometry . Rectangle (
- 122.50530270 , 37.76565568 , - 122.50460533 , 37.76619425 ), { id : 1 })
]);
Map . addLayer ( fcPolygon , { color : 'yellow' }, 'fcPolygon' );
var fcPolygonSamp = img . sampleRegions ({
collection : fcPolygon ,
scale : 10 ,
geometries : true
});
// Note that 7 pixels are missing from the sample. If a pixel contains a masked
// band value it will be excluded from the sample. In this case, the TCI_B band
// is masked for each unsampled pixel.
print ( 'A feature per pixel (at given scale) in each region' , fcPolygonSamp );
Map . addLayer ( fcPolygonSamp , { color : 'purple' }, 'fcPolygonSamp' );
// A feature collection with two points intersecting two different pixels.
// This example is included to show the behavior for point geometries. In
// practice, if the feature collection is all points, ee.Image.reduceRegions
// should be used instead to save memory.
var fcPoint = ee . FeatureCollection ([
ee . Feature ( ee . Geometry . Point ([ - 122.50309256 , 37.76605006 ]), { id : 0 }),
ee . Feature ( ee . Geometry . Point ([ - 122.50344661 , 37.76560903 ]), { id : 1 })
]);
Map . addLayer ( fcPoint , { color : 'cyan' }, 'fcPoint' );
var fcPointSamp = img . sampleRegions ({
collection : fcPoint ,
scale : 10
});
print ( 'A feature per point' , fcPointSamp );
Python 設定
請參閱
Python 環境 頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
import ee
import geemap.core as geemap
Colab (Python)
# A Sentinel-2 surface reflectance image.
img = ee . Image ( 'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG' )
m = geemap . Map ()
m . set_center ( - 122.503881 , 37.765588 , 18 )
m . add_layer (
img , { 'bands' : [ 'B11' , 'B8' , 'B3' ], 'min' : 100 , 'max' : 4500 }, 'img'
)
display ( m )
# A feature collection with two polygon regions each intersecting 36
# pixels at 10 m scale.
fc_polygon = ee . FeatureCollection ([
ee . Feature (
ee . Geometry . Rectangle (
- 122.50620929 , 37.76502806 , - 122.50552264 , 37.76556663
),
{ 'id' : 0 },
),
ee . Feature (
ee . Geometry . Rectangle (
- 122.50530270 , 37.76565568 , - 122.50460533 , 37.76619425
),
{ 'id' : 1 },
),
])
m . add_layer ( fc_polygon , { 'color' : 'yellow' }, 'fc_polygon' )
fc_polygon_samp = img . sampleRegions (
collection = fc_polygon , scale = 10 , geometries = True
)
# Note that 7 pixels are missing from the sample. If a pixel contains a masked
# band value it will be excluded from the sample. In this case, the TCI_B band
# is masked for each unsampled pixel.
display ( 'A feature per pixel (at given scale) in each region' , fc_polygon_samp )
m . add_layer ( fc_polygon_samp , { 'color' : 'purple' }, 'fc_polygon_samp' )
# A feature collection with two points intersecting two different pixels.
# This example is included to show the behavior for point geometries. In
# practice, if the feature collection is all points, ee.Image.reduceRegions
# should be used instead to save memory.
fc_point = ee . FeatureCollection ([
ee . Feature ( ee . Geometry . Point ([ - 122.50309256 , 37.76605006 ]), { 'id' : 0 }),
ee . Feature ( ee . Geometry . Point ([ - 122.50344661 , 37.76560903 ]), { 'id' : 1 }),
])
m . add_layer ( fc_point , { 'color' : 'cyan' }, 'fc_point' )
fc_point_samp = img . sampleRegions ( collection = fc_point , scale = 10 )
display ( 'A feature per point' , fc_point_samp )
提供意見
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權 ,程式碼範例則為阿帕契 2.0 授權 。詳情請參閱《Google Developers 網站政策 》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],["The `Image.sampleRegions` method converts image pixels intersecting specified regions into a `FeatureCollection`. Each output feature contains properties from the input image bands and any designated input feature properties. Geometries are snapped to pixel centers. The sampling scale and projection can be specified; otherwise, the image's first band defaults are used. Optionally, geometries of the sampled pixels can be included, and tile scaling can be used for memory management.\n"]]