ee.Image.arraySort
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Sắp xếp các phần tử của từng pixel mảng dọc theo một trục.
Cách sử dụng | Giá trị trả về |
---|
Image.arraySort(keys) | Hình ảnh |
Đối số | Loại | Thông tin chi tiết |
---|
this: image | Hình ảnh | Sắp xếp hình ảnh mảng. |
keys | Hình ảnh, mặc định: rỗng | Khoá không bắt buộc để sắp xếp. Nếu không được cung cấp, các giá trị này sẽ được dùng làm khoá. Các khoá chỉ có thể có nhiều phần tử dọc theo một trục, xác định hướng sắp xếp. |
Ví dụ
Trình soạn thảo mã (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]]
Thiết lập Python
Hãy xem trang
Môi trường Python để biết thông tin về API Python và cách sử dụng geemap
cho quá trình phát triển tương tác.
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]]
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\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```"]]