อาร์เรย์ใน Earth Engine สร้างขึ้นจากรายการตัวเลขและรายการของรายการ ระดับการฝังจะกําหนดจํานวนมิติข้อมูล หากต้องการเริ่มต้นด้วยตัวอย่างที่เข้าใจง่ายและน่าสนใจ ให้ดูตัวอย่าง Array
ต่อไปนี้ซึ่งสร้างจากค่าสัมประสิทธิ์หมวกทรงกรวย (TC) ของ Landsat 8 (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], ]);
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], ])
ยืนยันว่านี่คืออาร์เรย์ 2 มิติขนาด 6x6 โดยใช้ length()
ซึ่งจะแสดงผลความยาวของแต่ละแกน
เครื่องมือแก้ไขโค้ด (JavaScript)
// Print the dimensions. print(coefficients.length()); // [6,6]
import ee import geemap.core as geemap
Colab (Python)
# Print the dimensions. display(coefficients.length()) # [6,6]
ตารางต่อไปนี้แสดงการจัดเรียงรายการของเมทริกซ์ตามแกน 0 และแกนที่ 1
1 แกน -> | |||||||
---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | ||
0 | 0.3029 | 0.2786 | 0.4733 | 0.5599 | 0.508 | 0.1872 | |
1 | -0.2941 | -0.243 | -0.5424 | 0.7276 | 0.0713 | -0.1608 | |
แกน 0 | 2 | 0.1511 | 0.1973 | 0.3283 | 0.3407 | -0.7117 | -0.4559 |
3 | -0.8239 | 0.0849 | 0.4396 | -0.058 | 0.2013 | -0.2773 | |
4 | -0.3294 | 0.0557 | 0.1056 | 0.1855 | -0.4349 | 0.8085 | |
5 | 0.1079 | -0.9023 | 0.4119 | 0.0575 | -0.0259 | 0.0252 |
ดัชนีทางด้านซ้ายของตารางระบุตำแหน่งตามแนวแกน 0 เอลิเมนต์ที่ n ในแต่ละลิสต์บนแกน 0 จะอยู่ตําแหน่ง n ตามแกน 1 ตัวอย่างเช่น รายการที่พิกัด [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);
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]]
โปรดทราบว่าพารามิเตอร์ start
และ end
ของ slice()
สอดคล้องกับดัชนีแกน 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);
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 มิติ ซึ่งรายการต่างๆ สอดคล้องกับค่า 6 ค่าในตำแหน่งที่สอดคล้องกันในย่านของ image
รูปภาพอาร์เรย์ของเวกเตอร์ 1 มิติที่สร้างในลักษณะนี้ไม่มีแนวคิดเกี่ยวกับรูปร่าง 2 มิติ หากต้องการดำเนินการแบบ 2 มิติเท่านั้น เช่น การคูณเมทริกซ์ ให้แปลงเป็นอาร์เรย์ 2 มิติต่อรูปภาพแต่ละพิกเซลด้วย toArray(1)
ในพิกเซลแต่ละพิกเซลของรูปภาพอาร์เรย์ 2 มิติจะมีเมทริกซ์ 6x1 ของค่าย่านความถี่ ลองดูตัวอย่างสมมติต่อไปนี้
เครื่องมือแก้ไขโค้ด (JavaScript)
var array1D = ee.Array([1, 2, 3]); // [1,2,3] var array2D = ee.Array.cat([array1D], 1); // [[1],[2],[3]]
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);
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()
เพื่อวัตถุประสงค์ในการแสดงผล
เครื่องมือแก้ไขโค้ด (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');
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');
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()
เอาต์พุตควรมีลักษณะดังนี้
