结果将具有与输入相同的维度,并且在除切片轴之外的所有方向上具有相同的长度,其中长度将是沿“axis”在输入数组长度范围内从“start”到“end”以“step”为增量的位置数。这意味着,如果 start=end,或者开始值或结束值完全超出范围,则结果沿给定轴的长度可能为 0。
| 用法 | 返回 |
|---|---|
Image.arraySlice(axis, start, end, step) | 图片 |
| 参数 | 类型 | 详细信息 |
|---|---|---|
this:input | 图片 | 输入数组图片。 |
axis | 整数,默认值:0 | 要创建子集的轴。 |
start | 图片,默认值:null | 沿“axis”的第一个切片(含边界值)的坐标。负数用于相对于数组末尾定位切片的开头,其中 -1 从轴上的最后一个位置开始,-2 从倒数第二个位置开始,依此类推。开始索引必须有一个波段,或者每个“input”波段有一个波段。如果未设置此实参或在某些像素处对其进行了遮盖,则该像素处的切片将从索引 0 开始。 |
end | 图片,默认值:null | 停止切片的坐标(不含边界值)。默认情况下,这将是给定轴的长度。负数用于相对于数组末尾定位切片的结尾,其中 -1 将排除最后一个位置,-2 将排除最后两个位置,依此类推。结束索引必须有一个波段,或者每个“input”波段有一个波段。如果未设置此实参或在某些像素处对其进行了遮盖,则该像素处的切片将在最后一个索引之后结束。 |
step | 整数,默认值:1 | 沿“axis”的切片之间的间隔;将从“start”(含边界值)到“end”(不含边界值)以“step”的每个整数倍数进行切片。必须为正值。 |
示例
代码编辑器 (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]]
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() display('1D array image (pixel):', samp_arr_img(array_img_1d)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] # Get the first 3 elements. display('1D array image first 3 elements (pixel):', samp_arr_img(array_img_1d.arraySlice(0, 0, 3))) # [0, 1, 2] # Get the last 3 elements. display('1D array image last 3 elements (pixel):', samp_arr_img(array_img_1d.arraySlice(0, -3))) # [9, 10, 11] # Get elements at index positions 3 through 5 (0-based). display('1D array image elements at index positions 3 through 5 (pixel):', samp_arr_img(array_img_1d.arraySlice(0, 3, 6))) # [3, 4, 5] # Get elements at index positions 4 through end (0-based). display('1D array image elements at index positions 4 through end (pixel)', samp_arr_img(array_img_1d.arraySlice(0, 4))) # [4, 5, 6, 7, 8, 9, 10, 11] # Get elements using a step of 3. display('1D array image elements at a step of 3 (pixel)', samp_arr_img(array_img_1d.arraySlice(0, 0, None, 3))) # [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) display('2D array image (pixel)', samp_arr_img(array_img_2d)) # [[0, 1, 2, 3], # [4, 5, 6, 7], # [8, 9, 10, 11]] # Get the second row. display('2D array image second row (pixel):', samp_arr_img(array_img_2d.arraySlice(0, 1, 2))) # [[4, 5, 6, 7] # Get the second column. display('2D array image second column (pixel):', samp_arr_img(array_img_2d.arraySlice(1, 1, 2))) # [[1], # [5], # [9]] # Get all columns except the last. display('2D array image all columns except last (pixel):', samp_arr_img(array_img_2d.arraySlice(1, 0, -1))) # [[0, 1, 2], # [4, 5, 6], # [8, 9, 10]]