배열 및 배열 이미지

Earth Engine의 배열은 숫자 목록과 목록 목록으로 구성됩니다. 중첩 정도에 따라 측정기준 수가 결정됩니다. 동기를 부여하는 간단한 예를 시작하려면 Landsat 8 테이프 모자 (TC) 계수(Baig et al.,Array 2014):

코드 편집기 (JavaScript)

// Create an Array of Tasseled Cap coefficients.
var coefficients = ee.Array([
  [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872],
  [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608],
  [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559],
  [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773],
  [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085],
  [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252],
]);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Create an Array of Tasseled Cap coefficients.
coefficients = ee.Array([
    [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872],
    [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608],
    [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559],
    [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773],
    [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085],
    [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252],
])

각 축의 길이를 반환하는 length()를 사용하여 6x6 2D 배열인지 확인합니다.

코드 편집기 (JavaScript)

// Print the dimensions.
print(coefficients.length()); //    [6,6]

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Print the dimensions.
display(coefficients.length())  #    [6,6]

다음 표는 0축과 1축을 따라 매트릭스 항목이 배치되는 방식을 보여줍니다.

1축 ->
012345
00.30290.27860.47330.55990.5080.1872
1-0.2941-0.243-0.54240.72760.0713-0.1608
0축20.15110.19730.32830.3407-0.7117-0.4559
3-0.82390.08490.4396-0.0580.2013-0.2773
4-0.32940.05570.10560.1855-0.43490.8085
50.1079-0.90230.41190.0575-0.02590.0252

표 왼쪽의 색인은 0축의 위치를 나타냅니다. 0축의 각 목록 내 n번째 요소는 1축을 따라 n번째 위치에 있습니다. 예를 들어 배열의 좌표 [3,1] 에 있는 항목은 0.0849입니다. '녹색'이 관심의 TC 구성요소라고 가정해 보겠습니다. slice()를 사용하여 녹색 하위 행렬을 가져올 수 있습니다.

코드 편집기 (JavaScript)

// Get the 1x6 greenness slice, display it.
var greenness = coefficients.slice({axis: 0, start: 1, end: 2, step: 1});
print(greenness);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Get the 1x6 greenness slice, display it.
greenness = coefficients.slice(axis=0, start=1, end=2, step=1)
display(greenness)

2D 녹색 매트릭스는 다음과 같이 표시됩니다.

[[-0.2941,-0.243,-0.5424,0.7276,0.0713,-0.1608]]
    

slice()startend 매개변수는 표에 표시된 0축 색인과 일치합니다 (start는 양 끝값을 포함하고 end는 양 끝값을 제외함).

배열 이미지

녹색 이미지를 얻으려면 Landsat 8 이미지의 밴드에 녹색 매트릭스를 곱합니다. 이렇게 하려면 먼저 멀티밴드 Landsat 이미지를 '배열 이미지'로 변환합니다. 여기서 각 픽셀은 밴드 값의 Array입니다. 예를 들면 다음과 같습니다.

코드 편집기 (JavaScript)

// Load a Landsat 8 image, select the bands of interest.
var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')
  .select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7']);

// Make an Array Image, with a 1-D Array per pixel.
var arrayImage1D = image.toArray();

// Make an Array Image with a 2-D Array per pixel, 6x1.
var arrayImage2D = arrayImage1D.toArray(1);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 image, select the bands of interest.
image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select(
    ['B2', 'B3', 'B4', 'B5', 'B6', 'B7']
)

# Make an Array Image, with a 1-D Array per pixel.
array_image_1d = image.toArray()

# Make an Array Image with a 2-D Array per pixel, 6x1.
array_image_2d = array_image_1d.toArray(1)

이 예에서 toArray()image를 각 픽셀이 1차원 벡터인 배열 이미지로 변환하며, 이 배열 이미지의 항목은 image의 밴드에서 해당 위치의 6개 값에 해당합니다. 이 방식으로 생성된 1차원 벡터의 배열 이미지에는 2차원 도형 개념이 없습니다. 행렬 곱셈과 같은 2D 전용 연산을 실행하려면 toArray(1)를 사용하여 픽셀당 2D 배열 이미지로 변환합니다. 2D 배열 이미지의 각 픽셀에는 밴드 값의 6x1 행렬이 있습니다. 이를 확인하려면 다음과 같은 간단한 예시를 살펴보세요.

코드 편집기 (JavaScript)

var array1D = ee.Array([1, 2, 3]);              // [1,2,3]
var array2D = ee.Array.cat([array1D], 1);     // [[1],[2],[3]]

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

array_1d = ee.Array([1, 2, 3])  # [1,2,3]
array_2d = ee.Array.cat([array_1d], 1)  # [[1],[2],[3]]

array1D 벡터가 0축을 따라 변하는 것을 확인합니다. array2D 매트릭스도 마찬가지이지만 추가 측정기준이 있습니다. 배열 이미지에서 toArray(1)를 호출하는 것은 모든 픽셀에서 cat(bandVector, 1)를 호출하는 것과 같습니다. 2D 배열 이미지를 사용하여 각 픽셀에 녹색 계수의 2D 매트릭스가 포함된 이미지로 왼쪽 곱합니다.

코드 편집기 (JavaScript)

// Do a matrix multiplication: 1x6 times 6x1.
// Cast the greenness Array to an Image prior to multiplication.
var greennessArrayImage = ee.Image(greenness).matrixMultiply(arrayImage2D);

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Do a matrix multiplication: 1x6 times 6x1.
# Cast the greenness Array to an Image prior to multiplication.
greenness_array_image = ee.Image(greenness).matrixMultiply(array_image_2d)

결과는 모든 픽셀이 1x6 녹색 행렬 (왼쪽)과 6x1 밴드 행렬 (오른쪽)을 곱한 결과인 1x1 행렬인 새 배열 이미지입니다. 디스플레이 목적으로는 arrayGet()를 사용하여 일반 1밴드 이미지로 변환합니다.

코드 편집기 (JavaScript)

// Get the result from the 1x1 array in each pixel of the 2-D array image.
var greennessImage = greennessArrayImage.arrayGet([0, 0]);

// Display the input imagery with the greenness result.
Map.setCenter(-122.3, 37.562, 10);
Map.addLayer(image, {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5}, 'image');
Map.addLayer(greennessImage, {min: -0.1, max: 0.13}, 'greenness');

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Get the result from the 1x1 array in each pixel of the 2-D array image.
greenness_image = greenness_array_image.arrayGet([0, 0])

# Display the input imagery with the greenness result.
m = geemap.Map()
m.set_center(-122.3, 37.562, 10)
m.add_layer(image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 'image')
m.add_layer(greenness_image, {'min': -0.1, 'max': 0.13}, 'greenness')
m

다음은 전체 계수 배열을 사용하여 여러 개의 술 달린 모자 구성요소를 한 번에 계산하고 결과를 표시하는 전체 예입니다.

코드 편집기 (JavaScript)

// Define an Array of Tasseled Cap coefficients.
var coefficients = ee.Array([
  [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872],
  [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608],
  [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559],
  [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773],
  [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085],
  [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252],
]);

// Load a Landsat 8 image, select the bands of interest.
var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')
  .select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7']);

// Make an Array Image, with a 1-D Array per pixel.
var arrayImage1D = image.toArray();

// Make an Array Image with a 2-D Array per pixel, 6x1.
var arrayImage2D = arrayImage1D.toArray(1);

// Do a matrix multiplication: 6x6 times 6x1.
var componentsImage = ee.Image(coefficients)
  .matrixMultiply(arrayImage2D)
  // Get rid of the extra dimensions.
  .arrayProject([0])
  .arrayFlatten(
    [['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']]);

// Display the first three bands of the result and the input imagery.
var vizParams = {
  bands: ['brightness', 'greenness', 'wetness'],
  min: -0.1, max: [0.5, 0.1, 0.1]
};
Map.setCenter(-122.3, 37.562, 10);
Map.addLayer(image, {bands: ['B5', 'B4', 'B3'], min: 0, max: 0.5}, 'image');
Map.addLayer(componentsImage, vizParams, 'components');

Python 설정

Python API 및 대화형 개발을 위한 geemap 사용에 관한 자세한 내용은 Python 환경 페이지를 참고하세요.

import ee
import geemap.core as geemap

Colab (Python)

# Define an Array of Tasseled Cap coefficients.
coefficients = ee.Array([
    [0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872],
    [-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608],
    [0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559],
    [-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773],
    [-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085],
    [0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252],
])

# Load a Landsat 8 image, select the bands of interest.
image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select(
    ['B2', 'B3', 'B4', 'B5', 'B6', 'B7']
)

# Make an Array Image, with a 1-D Array per pixel.
array_image_1d = image.toArray()

# Make an Array Image with a 2-D Array per pixel, 6x1.
array_image_2d = array_image_1d.toArray(1)

# Do a matrix multiplication: 6x6 times 6x1.
components_image = (
    ee.Image(coefficients)
    .matrixMultiply(array_image_2d)
    # Get rid of the extra dimensions.
    .arrayProject([0])
    .arrayFlatten(
        [['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']]
    )
)

# Display the first three bands of the result and the input imagery.
viz_params = {
    'bands': ['brightness', 'greenness', 'wetness'],
    'min': -0.1,
    'max': [0.5, 0.1, 0.1],
}
m = geemap.Map()
m.set_center(-122.3, 37.562, 10)
m.add_layer(image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 'image')
m.add_layer(components_image, viz_params, 'components')
m

배열 이미지에서 밴드를 가져올 때는 먼저 project()를 사용하여 추가 크기를 삭제한 다음 arrayFlatten()를 사용하여 일반 이미지로 다시 변환합니다. 출력은 다음과 같이 표시됩니다.

술 달린 모자 이미지
그림 1. 술 달린 모자 구성요소인 '밝기'(빨간색), '녹색'(녹색), '습도'(파란색)