公告:凡是在
2025 年 4 月 15 日前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格,才能繼續存取 Earth Engine。
ee.Image.clip
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
將圖片裁剪為幾何圖形或特徵。
輸出波段與輸入波段完全一致,但幾何圖形未涵蓋的資料會遭到遮蓋。輸出圖片會保留輸入圖片的中繼資料。
使用 clipToCollection 將圖片剪裁為 FeatureCollection。
傳回裁剪後的圖片。
用量 | 傳回 |
---|
Image.clip(geometry) | 圖片 |
引數 | 類型 | 詳細資料 |
---|
這個:image | 圖片 | Image 執行個體。 |
geometry | 特徵|幾何|物件 | 要剪輯的幾何圖形或特徵。 |
範例
程式碼編輯器 (JavaScript)
// A digital elevation model.
var dem = ee.Image('NASA/NASADEM_HGT/001');
var demVis = {bands: 'elevation', min: 0, max: 1500};
// Clip the DEM by a polygon geometry.
var geomPoly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38);
var demClip = dem.clip(geomPoly);
print('Clipped image retains metadata and band names', demClip);
Map.setCenter(-121.12, 38.13, 8);
Map.addLayer(demClip, demVis, 'Polygon clip');
Map.addLayer(geomPoly, {color: 'green'}, 'Polygon geometry', false);
// Clip the DEM by a line geometry.
var geomLine = ee.Geometry.LinearRing(
[[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]);
Map.addLayer(dem.clip(geomLine), demVis, 'Line clip');
Map.addLayer(geomLine, {color: 'orange'}, 'Line geometry', false);
// Images have geometry; clip the dem image by the geometry of an S2 image.
var s2Img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
var geomS2Img = s2Img.geometry();
Map.addLayer(dem.clip(geomS2Img), demVis, 'Image geometry clip');
Map.addLayer(geomS2Img, {color: 'blue'}, 'Image geometry', false);
// Don't use ee.Image.clip prior to ee.Image.regionReduction, the "geometry"
// parameter handles it more efficiently.
var zonalMax = dem.select('elevation').reduceRegion({
reducer: ee.Reducer.max(),
geometry: geomPoly
});
print('Max elevation (m)', zonalMax.get('elevation'));
// Don't use ee.Image.clip to clip an image by a FeatureCollection, use
// ee.Image.clipToCollection(collection).
var watersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10')
.filterBounds(ee.Geometry.Point(-122.754, 38.606).buffer(2e4));
Map.addLayer(dem.clipToCollection(watersheds), demVis, 'Watersheds clip');
Map.addLayer(watersheds, {color: 'red'}, 'Watersheds', false);
Python 設定
請參閱
Python 環境頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
import ee
import geemap.core as geemap
Colab (Python)
# A digital elevation model.
dem = ee.Image('NASA/NASADEM_HGT/001')
dem_vis = {'bands': 'elevation', 'min': 0, 'max': 1500}
# Clip the DEM by a polygon geometry.
geom_poly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38)
dem_clip = dem.clip(geom_poly)
display('Clipped image retains metadata and band names', dem_clip)
m = geemap.Map()
m.set_center(-121.12, 38.13, 8)
m.add_layer(dem_clip, dem_vis, 'Polygon clip')
m.add_layer(geom_poly, {'color': 'green'}, 'Polygon geometry', False)
# Clip the DEM by a line geometry.
geom_line = ee.Geometry.LinearRing(
[[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]
)
m.add_layer(dem.clip(geom_line), dem_vis, 'Line clip')
m.add_layer(geom_line, {'color': 'orange'}, 'Line geometry', False)
# Images have geometry clip the dem image by the geometry of an S2 image.
s_2_img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')
geom_s_2_img = s_2_img.geometry()
m.add_layer(dem.clip(geom_s_2_img), dem_vis, 'Image geometry clip')
m.add_layer(geom_s_2_img, {'color': 'blue'}, 'Image geometry', False)
# Don't use ee.Image.clip prior to ee.Image.regionReduction, the "geometry"
# parameter handles it more efficiently.
zonal_max = dem.select('elevation').reduceRegion(
reducer=ee.Reducer.max(), geometry=geom_poly
)
display('Max elevation (m)', zonal_max.get('elevation'))
# Don't use ee.Image.clip to clip an image by a FeatureCollection, use
# ee.Image.clipToCollection(collection).
watersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10').filterBounds(
ee.Geometry.Point(-122.754, 38.606).buffer(2e4)
)
m.add_layer(dem.clipToCollection(watersheds), dem_vis, 'Watersheds clip')
m.add_layer(watersheds, {'color': 'red'}, 'Watersheds', False)
m
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-29 (世界標準時間)。
[null,null,["上次更新時間:2025-07-29 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eImage.clip()\u003c/code\u003e masks an image by a Geometry or Feature, retaining the input image's metadata and bands within the clipped area.\u003c/p\u003e\n"],["\u003cp\u003eThe output of \u003ccode\u003eImage.clip()\u003c/code\u003e is an Image with the same bands as the input, but with data outside the clip geometry masked.\u003c/p\u003e\n"],["\u003cp\u003eWhen clipping to a FeatureCollection, use \u003ccode\u003eclipToCollection()\u003c/code\u003e instead of \u003ccode\u003eImage.clip()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eFor \u003ccode\u003eregionReduction\u003c/code\u003e operations, specifying the geometry directly in the \u003ccode\u003ereduceRegion\u003c/code\u003e call is more efficient than pre-clipping the image.\u003c/p\u003e\n"]]],[],null,["# ee.Image.clip\n\n\u003cbr /\u003e\n\nClips an image to a Geometry or Feature.\n\n\u003cbr /\u003e\n\nThe output bands correspond exactly to the input bands, except data not covered by the geometry is masked. The output image retains the metadata of the input image.\n\nUse clipToCollection to clip an image to a FeatureCollection.\n\nReturns the clipped image.\n\n| Usage | Returns |\n|------------------------|---------|\n| Image.clip`(geometry)` | Image |\n\n| Argument | Type | Details |\n|---------------|---------------------------|-------------------------------------|\n| this: `image` | Image | The Image instance. |\n| `geometry` | Feature\\|Geometry\\|Object | The Geometry or Feature to clip to. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A digital elevation model.\nvar dem = ee.Image('NASA/NASADEM_HGT/001');\nvar demVis = {bands: 'elevation', min: 0, max: 1500};\n\n// Clip the DEM by a polygon geometry.\nvar geomPoly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38);\nvar demClip = dem.clip(geomPoly);\nprint('Clipped image retains metadata and band names', demClip);\nMap.setCenter(-121.12, 38.13, 8);\nMap.addLayer(demClip, demVis, 'Polygon clip');\nMap.addLayer(geomPoly, {color: 'green'}, 'Polygon geometry', false);\n\n// Clip the DEM by a line geometry.\nvar geomLine = ee.Geometry.LinearRing(\n [[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]);\nMap.addLayer(dem.clip(geomLine), demVis, 'Line clip');\nMap.addLayer(geomLine, {color: 'orange'}, 'Line geometry', false);\n\n// Images have geometry; clip the dem image by the geometry of an S2 image.\nvar s2Img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');\nvar geomS2Img = s2Img.geometry();\nMap.addLayer(dem.clip(geomS2Img), demVis, 'Image geometry clip');\nMap.addLayer(geomS2Img, {color: 'blue'}, 'Image geometry', false);\n\n// Don't use ee.Image.clip prior to ee.Image.regionReduction, the \"geometry\"\n// parameter handles it more efficiently.\nvar zonalMax = dem.select('elevation').reduceRegion({\n reducer: ee.Reducer.max(),\n geometry: geomPoly\n});\nprint('Max elevation (m)', zonalMax.get('elevation'));\n\n// Don't use ee.Image.clip to clip an image by a FeatureCollection, use\n// ee.Image.clipToCollection(collection).\nvar watersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10')\n .filterBounds(ee.Geometry.Point(-122.754, 38.606).buffer(2e4));\nMap.addLayer(dem.clipToCollection(watersheds), demVis, 'Watersheds clip');\nMap.addLayer(watersheds, {color: 'red'}, 'Watersheds', false);\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# A digital elevation model.\ndem = ee.Image('NASA/NASADEM_HGT/001')\ndem_vis = {'bands': 'elevation', 'min': 0, 'max': 1500}\n\n# Clip the DEM by a polygon geometry.\ngeom_poly = ee.Geometry.BBox(-121.55, 39.01, -120.57, 39.38)\ndem_clip = dem.clip(geom_poly)\ndisplay('Clipped image retains metadata and band names', dem_clip)\nm = geemap.Map()\nm.set_center(-121.12, 38.13, 8)\nm.add_layer(dem_clip, dem_vis, 'Polygon clip')\nm.add_layer(geom_poly, {'color': 'green'}, 'Polygon geometry', False)\n\n# Clip the DEM by a line geometry.\ngeom_line = ee.Geometry.LinearRing(\n [[-121.19, 38.10], [-120.53, 38.54], [-120.22, 37.83], [-121.19, 38.10]]\n)\nm.add_layer(dem.clip(geom_line), dem_vis, 'Line clip')\nm.add_layer(geom_line, {'color': 'orange'}, 'Line geometry', False)\n\n# Images have geometry clip the dem image by the geometry of an S2 image.\ns_2_img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG')\ngeom_s_2_img = s_2_img.geometry()\nm.add_layer(dem.clip(geom_s_2_img), dem_vis, 'Image geometry clip')\nm.add_layer(geom_s_2_img, {'color': 'blue'}, 'Image geometry', False)\n\n# Don't use ee.Image.clip prior to ee.Image.regionReduction, the \"geometry\"\n# parameter handles it more efficiently.\nzonal_max = dem.select('elevation').reduceRegion(\n reducer=ee.Reducer.max(), geometry=geom_poly\n)\ndisplay('Max elevation (m)', zonal_max.get('elevation'))\n\n# Don't use ee.Image.clip to clip an image by a FeatureCollection, use\n# ee.Image.clipToCollection(collection).\nwatersheds = ee.FeatureCollection('USGS/WBD/2017/HUC10').filterBounds(\n ee.Geometry.Point(-122.754, 38.606).buffer(2e4)\n)\nm.add_layer(dem.clipToCollection(watersheds), dem_vis, 'Watersheds clip')\nm.add_layer(watersheds, {'color': 'red'}, 'Watersheds', False)\nm\n```"]]