公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.arraySlice
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过以“step”为增量,从“start”(含)到“end”(不含)沿给定轴切出每个位置,从而创建一个子数组。结果的维度数将与输入的维度数相同,并且在除切片轴之外的所有方向上的长度都相同,而切片轴上的长度将是沿“轴”方向从“开始”到“结束”以“步长”为间隔且在输入数组长度范围内的位置数。这意味着,如果 start=end,或者开始值或结束值完全超出范围,则结果沿指定轴的长度可能为 0。
用法 | 返回 |
---|
Image.arraySlice(axis, start, end, step) | 图片 |
参数 | 类型 | 详细信息 |
---|
此:input | 图片 | 输入数组图片。 |
axis | 整数,默认值:0 | 要进行子集化的轴。 |
start | 图片,默认值:null | 沿“轴”的第一个切片的坐标(含)。负数用于相对于数组末尾定位切片的起始位置,其中 -1 从轴上的最后一个位置开始,-2 从倒数第二个位置开始,依此类推。必须有一个起始索引频段,或者每个“输入”频段有一个频段。如果未设置此实参或在某个像素处对其进行了遮盖,则相应像素处的切片将从索引 0 开始。 |
end | 图片,默认值:null | 停止切片的坐标(不含)。默认情况下,这将是指定轴的长度。负数用于指定切片的结束位置相对于数组末尾的位置,其中 -1 表示排除最后一个位置,-2 表示排除最后两个位置,依此类推。结束指数必须有一个频段,或者每个“输入”频段有一个频段。如果未设置此实参或在某个像素处对其进行了遮盖,则相应像素处的切片将在最后一个索引之后结束。 |
step | 整数,默认值:1 | 沿“轴”的切片分隔;系统将从“开始”(含)到“结束”(不含)的每个“步长”整数倍处获取一个切片。必须为正值。 |
示例
代码编辑器 (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 12.
var arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
// Get the first 3 elements.
print('1D array image first 3 elements (pixel)',
sampArrImg(arrayImg1D.arraySlice(0, 0, 3)));
// [0, 1, 2]
// Get the last 3 elements.
print('1D array image last 3 elements (pixel)',
sampArrImg(arrayImg1D.arraySlice(0, -3)));
// [9, 10, 11]
// Get elements at index positions 3 through 5 (0-based).
print('1D array image elements at index positions 3 through 5 (pixel)',
sampArrImg(arrayImg1D.arraySlice(0, 3, 6)));
// [3, 4, 5]
// Get elements at index positions 4 through end (0-based).
print('1D array image elements at index positions 4 through end (pixel)',
sampArrImg(arrayImg1D.arraySlice(0, 4)));
// [4, 5, 6, 7, 8, 9, 10, 11]
// Get elements using a step of 3.
print('1D array image elements at a step of 3 (pixel)',
sampArrImg(arrayImg1D.arraySlice(0, 0, null, 3)));
// [0, 3, 6, 9]
// Create a 2D array image with 3 rows and 4 columns.
var arrayImg2D = arrayImg1D.arrayReshape(ee.Image([3, 4]).toArray(), 2);
print('2D array image (pixel)', sampArrImg(arrayImg2D));
// [[0, 1, 2, 3],
// [4, 5, 6, 7],
// [8, 9, 10, 11]]
// Get the second row.
print('2D array image second row (pixel)',
sampArrImg(arrayImg2D.arraySlice(0, 1, 2)));
// [[4, 5, 6, 7]
// Get the second column.
print('2D array image second column (pixel)',
sampArrImg(arrayImg2D.arraySlice(1, 1, 2)));
// [[1],
// [5],
// [9]]
// Get all columns except the last.
print('2D array image all columns except last (pixel)',
sampArrImg(arrayImg2D.arraySlice(1, 0, -1)));
// [[0, 1, 2],
// [4, 5, 6],
// [8, 9, 10]]
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 12.
array_img_1d = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# Get the first 3 elements.
print('1D array image first 3 elements (pixel):',
samp_arr_img(array_img_1d.arraySlice(0, 0, 3)).getInfo())
# [0, 1, 2]
# Get the last 3 elements.
print('1D array image last 3 elements (pixel):',
samp_arr_img(array_img_1d.arraySlice(0, -3)).getInfo())
# [9, 10, 11]
# Get elements at index positions 3 through 5 (0-based).
print('1D array image elements at index positions 3 through 5 (pixel):',
samp_arr_img(array_img_1d.arraySlice(0, 3, 6)).getInfo())
# [3, 4, 5]
# Get elements at index positions 4 through end (0-based).
print('1D array image elements at index positions 4 through end (pixel)',
samp_arr_img(array_img_1d.arraySlice(0, 4)).getInfo())
# [4, 5, 6, 7, 8, 9, 10, 11]
# Get elements using a step of 3.
print('1D array image elements at a step of 3 (pixel)',
samp_arr_img(array_img_1d.arraySlice(0, 0, None, 3)).getInfo())
# [0, 3, 6, 9]
# Create a 2D array image with 3 rows and 4 columns.
array_img_2d = array_img_1d.arrayReshape(ee.Image([3, 4]).toArray(), 2)
print('2D array image (pixel)', samp_arr_img(array_img_2d).getInfo())
# [[0, 1, 2, 3],
# [4, 5, 6, 7],
# [8, 9, 10, 11]]
# Get the second row.
print('2D array image second row (pixel):',
samp_arr_img(array_img_2d.arraySlice(0, 1, 2)).getInfo())
# [[4, 5, 6, 7]
# Get the second column.
print('2D array image second column (pixel):',
samp_arr_img(array_img_2d.arraySlice(1, 1, 2)).getInfo())
# [[1],
# [5],
# [9]]
# Get all columns except the last.
print('2D array image all columns except last (pixel):',
samp_arr_img(array_img_2d.arraySlice(1, 0, -1)).getInfo())
# [[0, 1, 2],
# [4, 5, 6],
# [8, 9, 10]]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eImage.arraySlice()\u003c/code\u003e extracts a subarray from an image by specifying the axis, start, end, and step.\u003c/p\u003e\n"],["\u003cp\u003eIt allows you to select portions of an array image along a given axis, similar to slicing in Python.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003estart\u003c/code\u003e and \u003ccode\u003eend\u003c/code\u003e parameters can be negative to index from the end of the array.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003estep\u003c/code\u003e parameter controls the interval between selected elements.\u003c/p\u003e\n"],["\u003cp\u003eThe resulting image has the same dimensions as the input except along the sliced axis, where the length depends on the slicing parameters.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arraySlice\n\nCreates a subarray by slicing out each position along the given axis from the 'start' (inclusive) to 'end' (exclusive) by increments of 'step'. The result will have as many dimensions as the input, and the same length in all directions except the slicing axis, where the length will be the number of positions from 'start' to 'end' by 'step' that are in range of the input array's length along 'axis'. This means the result can be length 0 along the given axis if start=end, or if the start or end values are entirely out of range.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|---------------------------------------------------------------|---------|\n| Image.arraySlice`(`*axis* `, `*start* `, `*end* `, `*step*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `input` | Image | Input array image. |\n| `axis` | Integer, default: 0 | Axis to subset. |\n| `start` | Image, default: null | The coordinate of the first slice (inclusive) along 'axis'. Negative numbers are used to position the start of slicing relative to the end of the array, where -1 starts at the last position on the axis, -2 starts at the next to last position, etc. There must one band for start indices, or one band per 'input' band. If this argument is not set or masked at some pixel, then the slice at that pixel will start at index 0. |\n| `end` | Image, default: null | The coordinate (exclusive) at which to stop taking slices. By default this will be the length of the given axis. Negative numbers are used to position the end of slicing relative to the end of the array, where -1 will exclude the last position, -2 will exclude the last two positions, etc. There must be one band for end indices, or one band per 'input' band. If this argument is not set or masked at some pixel, then the slice at that pixel will end just after the last index. |\n| `step` | Integer, default: 1 | The separation between slices along 'axis'; a slice will be taken at each whole multiple of 'step' from 'start' (inclusive) to 'end' (exclusive). Must be positive. |\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 12.\nvar arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n// Get the first 3 elements.\nprint('1D array image first 3 elements (pixel)',\n sampArrImg(arrayImg1D.arraySlice(0, 0, 3)));\n// [0, 1, 2]\n\n// Get the last 3 elements.\nprint('1D array image last 3 elements (pixel)',\n sampArrImg(arrayImg1D.arraySlice(0, -3)));\n// [9, 10, 11]\n\n// Get elements at index positions 3 through 5 (0-based).\nprint('1D array image elements at index positions 3 through 5 (pixel)',\n sampArrImg(arrayImg1D.arraySlice(0, 3, 6)));\n// [3, 4, 5]\n\n// Get elements at index positions 4 through end (0-based).\nprint('1D array image elements at index positions 4 through end (pixel)',\n sampArrImg(arrayImg1D.arraySlice(0, 4)));\n// [4, 5, 6, 7, 8, 9, 10, 11]\n\n// Get elements using a step of 3.\nprint('1D array image elements at a step of 3 (pixel)',\n sampArrImg(arrayImg1D.arraySlice(0, 0, null, 3)));\n// [0, 3, 6, 9]\n\n// Create a 2D array image with 3 rows and 4 columns.\nvar arrayImg2D = arrayImg1D.arrayReshape(ee.Image([3, 4]).toArray(), 2);\nprint('2D array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 1, 2, 3],\n// [4, 5, 6, 7],\n// [8, 9, 10, 11]]\n\n// Get the second row.\nprint('2D array image second row (pixel)',\n sampArrImg(arrayImg2D.arraySlice(0, 1, 2)));\n// [[4, 5, 6, 7]\n\n// Get the second column.\nprint('2D array image second column (pixel)',\n sampArrImg(arrayImg2D.arraySlice(1, 1, 2)));\n// [[1],\n// [5],\n// [9]]\n\n// Get all columns except the last.\nprint('2D array image all columns except last (pixel)',\n sampArrImg(arrayImg2D.arraySlice(1, 0, -1)));\n// [[0, 1, 2],\n// [4, 5, 6],\n// [8, 9, 10]]\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 12.\narray_img_1d = ee.Image([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n# Get the first 3 elements.\nprint('1D array image first 3 elements (pixel):',\n samp_arr_img(array_img_1d.arraySlice(0, 0, 3)).getInfo())\n# [0, 1, 2]\n\n# Get the last 3 elements.\nprint('1D array image last 3 elements (pixel):',\n samp_arr_img(array_img_1d.arraySlice(0, -3)).getInfo())\n# [9, 10, 11]\n\n# Get elements at index positions 3 through 5 (0-based).\nprint('1D array image elements at index positions 3 through 5 (pixel):',\n samp_arr_img(array_img_1d.arraySlice(0, 3, 6)).getInfo())\n# [3, 4, 5]\n\n# Get elements at index positions 4 through end (0-based).\nprint('1D array image elements at index positions 4 through end (pixel)',\n samp_arr_img(array_img_1d.arraySlice(0, 4)).getInfo())\n# [4, 5, 6, 7, 8, 9, 10, 11]\n\n# Get elements using a step of 3.\nprint('1D array image elements at a step of 3 (pixel)',\n samp_arr_img(array_img_1d.arraySlice(0, 0, None, 3)).getInfo())\n# [0, 3, 6, 9]\n\n# Create a 2D array image with 3 rows and 4 columns.\narray_img_2d = array_img_1d.arrayReshape(ee.Image([3, 4]).toArray(), 2)\nprint('2D array image (pixel)', samp_arr_img(array_img_2d).getInfo())\n# [[0, 1, 2, 3],\n# [4, 5, 6, 7],\n# [8, 9, 10, 11]]\n\n# Get the second row.\nprint('2D array image second row (pixel):',\n samp_arr_img(array_img_2d.arraySlice(0, 1, 2)).getInfo())\n# [[4, 5, 6, 7]\n\n# Get the second column.\nprint('2D array image second column (pixel):',\n samp_arr_img(array_img_2d.arraySlice(1, 1, 2)).getInfo())\n# [[1],\n# [5],\n# [9]]\n\n# Get all columns except the last.\nprint('2D array image all columns except last (pixel):',\n samp_arr_img(array_img_2d.arraySlice(1, 0, -1)).getInfo())\n# [[0, 1, 2],\n# [4, 5, 6],\n# [8, 9, 10]]\n```"]]