配列と配列イメージ

Earth Engine の配列は、数値のリストとリストのリストから構成されます。ネストの深さによってディメンションの数が変わります。簡単な例を挙げて説明しましょう。Landsat 8 のタスセルド キャップ(TC)係数から作成された Array の例を以下に示します(Baig et al., 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 の 2 次元配列であることを確認します。この関数は、各軸の長さを返します。

コードエディタ(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)

2 次元の緑色マトリックスは次のようになります。

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

slice()start パラメータと end パラメータは、表に表示される 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 配列に変換します。2 次元配列画像の各ピクセルには、バンド値の 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) を呼び出すようなものです。2 次元配列画像を使用して、各ピクセルに緑色係数の 2 次元マトリックスが含まれる画像に左から乗算します。

コードエディタ(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)

結果として、すべてのピクセルが 1x1 行列である新しい配列画像が生成されます。この行列は、1x6 の緑色度行列(左)と 6x1 のバンド行列(右)の行列乗算によって得られます。表示目的の場合は、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. タッセル キャップのコンポーネント「明るさ」(赤)、「緑色」(緑)、「湿り気」(青)。