公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.arrayReshape
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
将具有相同形状(可能是多维)像素的图像的数组波段转换为具有新形状的数组图像。
用法 | 返回 |
---|
Image.arrayReshape(lengths, dimensions) | 图片 |
参数 | 类型 | 详细信息 |
---|
此:image | 图片 | 要重塑的数组的图片。 |
lengths | 图片 | 一个单波段图像,用于指定输入图像每个轴的新长度(以每个像素一个一维数组的形式表示)。每个形状数组中都应包含“维度”长度值。如果其中一个长度为 -1,则系统会计算相应轴的长度,以使总大小保持不变。具体而言,[-1] 形状会展平为 1 维。最多只能有一个形状分量为 -1。 |
dimensions | 整数 | 所有输出数组像素共有的维度数量。 |
示例
代码编辑器 (JavaScript)
// A function to print arrays 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 with length 6.
var arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 1, 2, 3, 4, 5]
// Reshape the 1D 6-element array to a 2D 2 (row) x 3 (column) array. Notice
// that elements are filled row by row in the reshaped result.
var reshape2x3 = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);
print('2D 2x3 array image (pixel)', sampArrImg(reshape2x3));
// [[0, 1, 2],
// [3, 4, 5]]
// Use -1 to auto-determine a dimension length. For example, here we set
// 3 rows and let Earth Engine determine the number of columns needed.
var reshape3x_ = arrayImg1D.arrayReshape(ee.Image([3, -1]).toArray(), 2);
print('2D 3x? array image (pixel)', sampArrImg(reshape3x_));
// [[0, 1],
// [2, 3],
// [4, 5]]
// Flatten a 2D 2x3 array to 1D 6-element array.
var flattened = reshape2x3.arrayReshape(ee.Image([-1]).toArray(), 1);
print('2D array flattened to 1D', sampArrImg(flattened));
// [0, 1, 2, 3, 4, 5]
Python 设置
如需了解 Python API 和如何使用 geemap
进行交互式开发,请参阅
Python 环境页面。
import ee
import geemap.core as geemap
Colab (Python)
# A function to print arrays 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 with length 6.
array_img_1d = ee.Image([0, 1, 2, 3, 4, 5]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 1, 2, 3, 4, 5]
# Reshape the 1D 6-element array to a 2D 2 (row) x 3 (column) array. Notice
# that elements are filled row by row in the reshaped result.
reshape2x3 = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)
print('2D 2x3 array image (pixel):', samp_arr_img(reshape2x3).getInfo())
# [[0, 1, 2],
# [3, 4, 5]]
# Use -1 to auto-determine a dimension length. For example, here we set
# 3 rows and let Earth Engine determine the number of columns needed.
reshape3x_ = array_img_1d.arrayReshape(ee.Image([3, -1]).toArray(), 2)
print('2D 3x? array image (pixel):', samp_arr_img(reshape3x_).getInfo())
# [[0, 1],
# [2, 3],
# [4, 5]]
# Flatten a 2D 2x3 array to 1D 6-element array.
flattened = reshape2x3.arrayReshape(ee.Image([-1]).toArray(), 1)
print('2D array flattened to 1D:', samp_arr_img(flattened).getInfo())
# [0, 1, 2, 3, 4, 5]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eImage.arrayReshape()\u003c/code\u003e transforms images with array-shaped pixels into images with a new array shape, potentially changing the dimensionality of the pixels.\u003c/p\u003e\n"],["\u003cp\u003eIt takes the original image, desired lengths for each axis of the new shape, and the total number of dimensions as input.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify \u003ccode\u003e-1\u003c/code\u003e for a dimension length to have Earth Engine automatically calculate it, ensuring the total number of elements remains constant.\u003c/p\u003e\n"],["\u003cp\u003eThis function is useful for tasks like converting between 1D, 2D, or higher-dimensional array representations of data within an image.\u003c/p\u003e\n"],["\u003cp\u003eExamples demonstrate reshaping a 1D array into a 2D array, using \u003ccode\u003e-1\u003c/code\u003e for automatic dimension calculation, and flattening a 2D array back to 1D.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arrayReshape\n\nConverts array bands of an image with equally-shaped, possibly multidimensional pixels to an image of arrays with a new shape.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-------------------------------------------|---------|\n| Image.arrayReshape`(lengths, dimensions)` | Image |\n\n| Argument | Type | Details |\n|---------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | The image of arrays to reshape. |\n| `lengths` | Image | A 1-band image specifying the new lengths of each axis of the input image specified as a 1-D array per pixel. There should be 'dimensions' lengths values in each shape' array. If one of the lengths is -1, then the corresponding length for that axis will be computed such that the total size remains constant. In particular, a shape of \\[-1\\] flattens into 1-D. At most one component of shape can be -1. |\n| `dimensions` | Integer | The number of dimensions shared by all output array pixels. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A function to print arrays 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 with length 6.\nvar arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2, 3, 4, 5]\n\n// Reshape the 1D 6-element array to a 2D 2 (row) x 3 (column) array. Notice\n// that elements are filled row by row in the reshaped result.\nvar reshape2x3 = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);\nprint('2D 2x3 array image (pixel)', sampArrImg(reshape2x3));\n// [[0, 1, 2],\n// [3, 4, 5]]\n\n// Use -1 to auto-determine a dimension length. For example, here we set\n// 3 rows and let Earth Engine determine the number of columns needed.\nvar reshape3x_ = arrayImg1D.arrayReshape(ee.Image([3, -1]).toArray(), 2);\nprint('2D 3x? array image (pixel)', sampArrImg(reshape3x_));\n// [[0, 1],\n// [2, 3],\n// [4, 5]]\n\n// Flatten a 2D 2x3 array to 1D 6-element array.\nvar flattened = reshape2x3.arrayReshape(ee.Image([-1]).toArray(), 1);\nprint('2D array flattened to 1D', sampArrImg(flattened));\n// [0, 1, 2, 3, 4, 5]\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 arrays 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 with length 6.\narray_img_1d = ee.Image([0, 1, 2, 3, 4, 5]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2, 3, 4, 5]\n\n# Reshape the 1D 6-element array to a 2D 2 (row) x 3 (column) array. Notice\n# that elements are filled row by row in the reshaped result.\nreshape2x3 = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)\nprint('2D 2x3 array image (pixel):', samp_arr_img(reshape2x3).getInfo())\n# [[0, 1, 2],\n# [3, 4, 5]]\n\n# Use -1 to auto-determine a dimension length. For example, here we set\n# 3 rows and let Earth Engine determine the number of columns needed.\nreshape3x_ = array_img_1d.arrayReshape(ee.Image([3, -1]).toArray(), 2)\nprint('2D 3x? array image (pixel):', samp_arr_img(reshape3x_).getInfo())\n# [[0, 1],\n# [2, 3],\n# [4, 5]]\n\n# Flatten a 2D 2x3 array to 1D 6-element array.\nflattened = reshape2x3.arrayReshape(ee.Image([-1]).toArray(), 1)\nprint('2D array flattened to 1D:', samp_arr_img(flattened).getInfo())\n# [0, 1, 2, 3, 4, 5]\n```"]]