公告:所有在
2025 年 4 月 15 日之前注册使用 Earth Engine 的非商业项目都必须
验证是否符合非商业性质的资格条件,才能继续使用 Earth Engine。
ee.Image.arraySort
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
沿一个轴对每个数组像素的元素进行排序。
用法 | 返回 |
---|
Image.arraySort(keys) | 图片 |
参数 | 类型 | 详细信息 |
---|
此:image | 图片 | 要排序的数组映像。 |
keys | 图片,默认值:null | 用于排序的可选键。如果未提供,则这些值将用作键。键只能在一个轴上具有多个元素,这决定了排序方向。 |
示例
代码编辑器 (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, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray();
print('1D array image (pixel)', sampArrImg(arrayImg1D));
// [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]
// Sort the 1D array in ascending order.
var arrayImg1DSorted = arrayImg1D.arraySort();
print('1D array image sorted (pixel)', sampArrImg(arrayImg1DSorted));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
// You can use another array image to control sorting. The order of values
// in the optional keys array translates to relative position in the input
// array. For example, here is a keys array image with non-sequential values in
// descending order, it will reverse the order of the input array; make the
// last position first and the first position last.
var keys1D = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray();
print('1D array image keys (pixel)', sampArrImg(keys1D));
// [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]
var arrayImg1DSortedKeys = arrayImg1D.arraySort(keys1D);
print('1D array image sorted by keys (pixel)', sampArrImg(arrayImg1DSortedKeys));
// [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]
// To sort a 2D array, the keys array image is required.
// 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, 10, 6, 5],
// [4, 7, 11, 1],
// [2, 9, 8, 3]]
// Only a single axis can be sorted at a time. Here, we reverse sort along rows.
// There are 4 elements in each row, so we construct an 1x4 array image.
var keys2Drows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose();
print('2D array image row keys (pixel)', sampArrImg(keys2Drows));
// [[3, 2, 1, 0]]
var arrayImg2DSortedRows = arrayImg2D.arraySort(keys2Drows);
print('2D array image sorted rows (pixel)', sampArrImg(arrayImg2DSortedRows));
// [[5, 6, 10, 0],
// [1, 11, 7, 4],
// [3, 8, 9, 2]]
// Reverse sort along columns, create a 3x1 keys array image with values in
// descending order.
var keys2Dcols = ee.Image([2, 1, 0]).toArray().toArray(1);
print('2D array image column keys (pixel)', sampArrImg(keys2Dcols));
// [[2],
// [1],
// [0]]
var arrayImg2DSortedCols = arrayImg2D.arraySort(keys2Dcols);
print('2D array image sorted cols (pixel)', sampArrImg(arrayImg2DSortedCols));
// [[2, 9, 8, 3],
// [4, 7, 11, 1],
// [0, 10, 6, 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 12.
array_img_1d = ee.Image([0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray()
print('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())
# [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]
# Sort the 1D array in ascending order.
array_img_1d_sorted = array_img_1d.arraySort()
print('1D array image sorted (pixel):',
samp_arr_img(array_img_1d_sorted).getInfo())
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
# You can use another array image to control sorting. The order of values
# in the optional keys array translates to relative position in the input
# array. For example, here is a keys array image with non-sequential values in
# descending order, it will reverse the order of the input array; make the
# last position first and the first position last.
keys_1d = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray()
print('1D array image keys (pixel):', samp_arr_img(keys_1d).getInfo())
# [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]
array_img_1d_sorted_keys = array_img_1d.arraySort(keys_1d)
print('1D array image sorted by keys (pixel):',
samp_arr_img(array_img_1d_sorted_keys).getInfo())
# [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]
# To sort a 2D array, the keys array image is required.
# 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, 10, 6, 5],
# [4, 7, 11, 1],
# [2, 9, 8, 3]]
# Only a single axis can be sorted at a time. Here, we reverse sort along rows.
# There are 4 elements in each row, so we construct an 1x4 array image.
keys_2d_rows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose()
print('2D array image row keys (pixel):', samp_arr_img(keys_2d_rows).getInfo())
# [[3, 2, 1, 0]]
array_img_2d_sorted_rows = array_img_2d.arraySort(keys_2d_rows)
print('2D array image sorted rows (pixel):',
samp_arr_img(array_img_2d_sorted_rows).getInfo())
# [[5, 6, 10, 0],
# [1, 11, 7, 4],
# [3, 8, 9, 2]]
# Reverse sort along columns, create a 3x1 keys array image with values in
# descending order.
keys_2d_cols = ee.Image([2, 1, 0]).toArray().toArray(1)
print('2D array image column keys (pixel):',
samp_arr_img(keys_2d_cols).getInfo())
# [[2],
# [1],
# [0]]
array_img_2d_sorted_cols = array_img_2d.arraySort(keys_2d_cols)
print('2D array image sorted cols (pixel):',
samp_arr_img(array_img_2d_sorted_cols).getInfo())
# [[2, 9, 8, 3],
# [4, 7, 11, 1],
# [0, 10, 6, 5]]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003e\u003ccode\u003eImage.arraySort()\u003c/code\u003e sorts elements within each pixel's array along a single axis, either ascending or descending.\u003c/p\u003e\n"],["\u003cp\u003eBy default, it sorts array elements in ascending order based on their values; an optional \u003ccode\u003ekeys\u003c/code\u003e argument allows for custom sorting based on another array image.\u003c/p\u003e\n"],["\u003cp\u003eWhen sorting 2D arrays, the \u003ccode\u003ekeys\u003c/code\u003e array image defines the sorting direction and order, allowing you to sort by rows or columns.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003ekeys\u003c/code\u003e array's dimensions determine the sorting axis: a 1xN array sorts along rows, and an Nx1 array sorts along columns of the input array image.\u003c/p\u003e\n"],["\u003cp\u003eThe values in the \u003ccode\u003ekeys\u003c/code\u003e array determine the relative positions of elements in the sorted output array.\u003c/p\u003e\n"]]],[],null,["# ee.Image.arraySort\n\nSorts elements of each array pixel along one axis.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|-----------------------------|---------|\n| Image.arraySort`(`*keys*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| this: `image` | Image | Array image to sort. |\n| `keys` | Image, default: null | Optional keys to sort by. If not provided, the values are used as the keys. The keys can only have multiple elements along one axis, which determines the direction to sort in. |\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, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]\n\n// Sort the 1D array in ascending order.\nvar arrayImg1DSorted = arrayImg1D.arraySort();\nprint('1D array image sorted (pixel)', sampArrImg(arrayImg1DSorted));\n// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n// You can use another array image to control sorting. The order of values\n// in the optional keys array translates to relative position in the input\n// array. For example, here is a keys array image with non-sequential values in\n// descending order, it will reverse the order of the input array; make the\n// last position first and the first position last.\nvar keys1D = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray();\nprint('1D array image keys (pixel)', sampArrImg(keys1D));\n// [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]\nvar arrayImg1DSortedKeys = arrayImg1D.arraySort(keys1D);\nprint('1D array image sorted by keys (pixel)', sampArrImg(arrayImg1DSortedKeys));\n// [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]\n\n// To sort a 2D array, the keys array image is required.\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, 10, 6, 5],\n// [4, 7, 11, 1],\n// [2, 9, 8, 3]]\n\n// Only a single axis can be sorted at a time. Here, we reverse sort along rows.\n// There are 4 elements in each row, so we construct an 1x4 array image.\nvar keys2Drows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose();\nprint('2D array image row keys (pixel)', sampArrImg(keys2Drows));\n// [[3, 2, 1, 0]]\nvar arrayImg2DSortedRows = arrayImg2D.arraySort(keys2Drows);\nprint('2D array image sorted rows (pixel)', sampArrImg(arrayImg2DSortedRows));\n// [[5, 6, 10, 0],\n// [1, 11, 7, 4],\n// [3, 8, 9, 2]]\n\n// Reverse sort along columns, create a 3x1 keys array image with values in\n// descending order.\nvar keys2Dcols = ee.Image([2, 1, 0]).toArray().toArray(1);\nprint('2D array image column keys (pixel)', sampArrImg(keys2Dcols));\n// [[2],\n// [1],\n// [0]]\nvar arrayImg2DSortedCols = arrayImg2D.arraySort(keys2Dcols);\nprint('2D array image sorted cols (pixel)', sampArrImg(arrayImg2DSortedCols));\n// [[2, 9, 8, 3],\n// [4, 7, 11, 1],\n// [0, 10, 6, 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 12.\narray_img_1d = ee.Image([0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 10, 6, 5, 4, 7, 11, 1, 2, 9, 8, 3]\n\n# Sort the 1D array in ascending order.\narray_img_1d_sorted = array_img_1d.arraySort()\nprint('1D array image sorted (pixel):',\n samp_arr_img(array_img_1d_sorted).getInfo())\n# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n\n# You can use another array image to control sorting. The order of values\n# in the optional keys array translates to relative position in the input\n# array. For example, here is a keys array image with non-sequential values in\n# descending order, it will reverse the order of the input array; make the\n# last position first and the first position last.\nkeys_1d = ee.Image([99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]).toArray()\nprint('1D array image keys (pixel):', samp_arr_img(keys_1d).getInfo())\n# [99, 98, 50, 45, 23, 18, 9, 8, 7, 5, 2, 0]\narray_img_1d_sorted_keys = array_img_1d.arraySort(keys_1d)\nprint('1D array image sorted by keys (pixel):',\n samp_arr_img(array_img_1d_sorted_keys).getInfo())\n# [3, 8, 9, 2, 1, 11, 7, 4, 5, 6, 10, 0]\n\n# To sort a 2D array, the keys array image is required.\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, 10, 6, 5],\n# [4, 7, 11, 1],\n# [2, 9, 8, 3]]\n\n# Only a single axis can be sorted at a time. Here, we reverse sort along rows.\n# There are 4 elements in each row, so we construct an 1x4 array image.\nkeys_2d_rows = ee.Image([3, 2, 1, 0]).toArray().toArray(1).arrayTranspose()\nprint('2D array image row keys (pixel):', samp_arr_img(keys_2d_rows).getInfo())\n# [[3, 2, 1, 0]]\narray_img_2d_sorted_rows = array_img_2d.arraySort(keys_2d_rows)\nprint('2D array image sorted rows (pixel):',\n samp_arr_img(array_img_2d_sorted_rows).getInfo())\n# [[5, 6, 10, 0],\n# [1, 11, 7, 4],\n# [3, 8, 9, 2]]\n\n# Reverse sort along columns, create a 3x1 keys array image with values in\n# descending order.\nkeys_2d_cols = ee.Image([2, 1, 0]).toArray().toArray(1)\nprint('2D array image column keys (pixel):',\n samp_arr_img(keys_2d_cols).getInfo())\n# [[2],\n# [1],\n# [0]]\narray_img_2d_sorted_cols = array_img_2d.arraySort(keys_2d_cols)\nprint('2D array image sorted cols (pixel):',\n samp_arr_img(array_img_2d_sorted_cols).getInfo())\n# [[2, 9, 8, 3],\n# [4, 7, 11, 1],\n# [0, 10, 6, 5]]\n```"]]