公告:凡是在
2025 年 4 月 15 日前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格,才能繼續存取 Earth Engine。
ee.Image.arrayPad
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
將每個像素的陣列值填入固定長度。填充值會附加至每個陣列,沿著每個軸延伸至指定長度。圖片的所有頻帶都必須是陣列值,且具有相同的維度。
用量 | 傳回 |
---|
Image.arrayPad(lengths, pad) | 圖片 |
引數 | 類型 | 詳細資料 |
---|
this:image | 圖片 | 將陣列圖片放入填充區。 |
lengths | 清單 | 輸出陣列中每個軸的所需長度清單。陣列的大小已達或超過指定長度,因此不會沿著該軸線變更。 |
pad | 數字,預設值:0 | 要填充的值。 |
範例
程式碼編輯器 (JavaScript)
// A function to print the array for a selected pixel in the following examples.
function sampArrImg(arrImg) {
var point = ee.Geometry.Point([-121, 42]);
return arrImg.sample(point, 500).first().get('array');
}
// Create a 1D array image.
var arrayImg1D = ee.Image([0, 1, 2]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 1, 2]
// Pad 1D array to length of 5 with value 9.
var arrayImg1Dpad = arrayImg1D.arrayPad([5], 9);
print('1D array image padded', sampArrImg(arrayImg1Dpad));
// [0, 1, 2, 9, 9]
// Create a 2D array image.
var arrayImg2D = ee.Image([0, 1, 2, 3, 4, 5]).toArray()
.arrayReshape(ee.Image([2, 3]).toArray(), 2);
print('2D 2x3 array image (pixel)', sampArrImg(arrayImg2D));
// [[0, 1, 2],
// [3, 4, 5]]
// Pad 2D array to 0-axis length 3 and 1-axis length 5 with value 9.
var arrayImg2Dpad = arrayImg2D.arrayPad([3, 5], 9);
print('2D array image padded', sampArrImg(arrayImg2Dpad));
// [[0, 1, 2, 9, 9],
// [3, 4, 5, 9, 9],
// [9, 9, 9, 9, 9]]
Python 設定
請參閱「
Python 環境」頁面,瞭解 Python API 和如何使用 geemap
進行互動式開發。
import ee
import geemap.core as geemap
Colab (Python)
# A function to print the array for a selected pixel in the following examples.
def samp_arr_img(arr_img):
point = ee.Geometry.Point([-121, 42])
return arr_img.sample(point, 500).first().get('array')
# Create a 1D array image.
array_img_1d = ee.Image([0, 1, 2]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 1, 2]
# Pad 1D array to length of 5 with value 9.
array_img_1d_pad = array_img_1d.arrayPad([5], 9)
print('1D array image padded:', samp_arr_img(array_img_1d_pad).getInfo())
# [0, 1, 2, 9, 9]
# Create a 2D array image.
array_img_2d = ee.Image([0, 1, 2, 3, 4, 5]).toArray().arrayReshape(
ee.Image([2, 3]).toArray(),
2
)
print('2D 2x3 array image (pixel):', samp_arr_img(array_img_2d).getInfo())
# [[0, 1, 2],
# [3, 4, 5]]
# Pad 2D array to 0-axis length 3 and 1-axis length 5 with value 9.
array_img_2d_pad = array_img_2d.arrayPad([3, 5], 9)
print('2D array image padded:', samp_arr_img(array_img_2d_pad).getInfo())
# [[0, 1, 2, 9, 9],
# [3, 4, 5, 9, 9],
# [9, 9, 9, 9, 9]]
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003earrayPad\u003c/code\u003e extends the array values in each image pixel to a specified length using a provided pad value.\u003c/p\u003e\n"],["\u003cp\u003eIt accepts an array image, a list of desired lengths for each axis, and an optional pad value (defaulting to 0).\u003c/p\u003e\n"],["\u003cp\u003eIf an array is already at or beyond the desired length, it remains unchanged along that axis.\u003c/p\u003e\n"],["\u003cp\u003eAll bands in the image must be array-valued and share the same dimensions for the function to work correctly.\u003c/p\u003e\n"],["\u003cp\u003eThis function is useful for standardizing array lengths in images for further processing.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arrayPad\n\nPads the array values in each pixel to be a fixed length. The pad value will be appended to each array to extend it to given length along each axis. All bands of the image must be array-valued and have the same dimensions.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|------------------------------------|---------|\n| Image.arrayPad`(lengths, `*pad*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|--------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | Array image to pad. |\n| `lengths` | List | A list of desired lengths for each axis in the output arrays. Arrays are already as large or larger than the given length will be unchanged along that axis. |\n| `pad` | Number, default: 0 | The value to pad with. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A function to print the array for a selected pixel in the following examples.\nfunction sampArrImg(arrImg) {\n var point = ee.Geometry.Point([-121, 42]);\n return arrImg.sample(point, 500).first().get('array');\n}\n\n// Create a 1D array image.\nvar arrayImg1D = ee.Image([0, 1, 2]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2]\n\n// Pad 1D array to length of 5 with value 9.\nvar arrayImg1Dpad = arrayImg1D.arrayPad([5], 9);\nprint('1D array image padded', sampArrImg(arrayImg1Dpad));\n// [0, 1, 2, 9, 9]\n\n// Create a 2D array image.\nvar arrayImg2D = ee.Image([0, 1, 2, 3, 4, 5]).toArray()\n .arrayReshape(ee.Image([2, 3]).toArray(), 2);\nprint('2D 2x3 array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 1, 2],\n// [3, 4, 5]]\n\n// Pad 2D array to 0-axis length 3 and 1-axis length 5 with value 9.\nvar arrayImg2Dpad = arrayImg2D.arrayPad([3, 5], 9);\nprint('2D array image padded', sampArrImg(arrayImg2Dpad));\n// [[0, 1, 2, 9, 9],\n// [3, 4, 5, 9, 9],\n// [9, 9, 9, 9, 9]]\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 function to print the array for a selected pixel in the following examples.\ndef samp_arr_img(arr_img):\n point = ee.Geometry.Point([-121, 42])\n return arr_img.sample(point, 500).first().get('array')\n\n# Create a 1D array image.\narray_img_1d = ee.Image([0, 1, 2]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2]\n\n# Pad 1D array to length of 5 with value 9.\narray_img_1d_pad = array_img_1d.arrayPad([5], 9)\nprint('1D array image padded:', samp_arr_img(array_img_1d_pad).getInfo())\n# [0, 1, 2, 9, 9]\n\n# Create a 2D array image.\narray_img_2d = ee.Image([0, 1, 2, 3, 4, 5]).toArray().arrayReshape(\n ee.Image([2, 3]).toArray(),\n 2\n)\nprint('2D 2x3 array image (pixel):', samp_arr_img(array_img_2d).getInfo())\n# [[0, 1, 2],\n# [3, 4, 5]]\n\n# Pad 2D array to 0-axis length 3 and 1-axis length 5 with value 9.\narray_img_2d_pad = array_img_2d.arrayPad([3, 5], 9)\nprint('2D array image padded:', samp_arr_img(array_img_2d_pad).getInfo())\n# [[0, 1, 2, 9, 9],\n# [3, 4, 5, 9, 9],\n# [9, 9, 9, 9, 9]]\n```"]]