ImageCollection
의 모든 Image
에 함수를 적용하려면 imageCollection.map()
를 사용하세요. map()
의 유일한 인수는 ee.Image
라는 하나의 매개변수를 사용하는 함수입니다. 예를 들어 다음 코드는 컬렉션의 모든 이미지에 타임스탬프 밴드를 추가합니다.
코드 편집기 (JavaScript)
// Load a Landsat 8 collection for a single path-row, 2021 images only. var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)); // This function adds a band representing the image timestamp. var addTime = function(image) { return image.addBands(image.getNumber('system:time_start')); }; // Map the function over the collection and display the result. print(collection.map(addTime));
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 collection for a single path-row, 2021 images only. collection = ( ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)) ) # This function adds a band representing the image timestamp. def add_time(image): return image.addBands(image.getNumber('system:time_start')) # Map the function over the collection and display the result. display(collection.map(add_time))
사전 정의된 함수에서 getNumber()
메서드는 속성의 숫자 값에서 새 Image
를 만드는 데 사용됩니다. 감소 및 합성 섹션에서 설명한 대로 시간 밴드를 사용하면 변화를 선형적으로 모델링하고 합성물을 만드는 데 유용합니다.
매핑된 함수는 실행할 수 있는 작업이 제한됩니다. 특히 함수 외부의 변수를 수정할 수 없으며, 아무것도 출력할 수 없으며, JavaScript 및 Python 'if' 또는 'for' 문을 사용할 수 없습니다. 하지만 ee.Algorithms.If()
를 사용하여 매핑된 함수에서 조건부 연산을 실행할 수 있습니다. 예를 들면 다음과 같습니다.
코드 편집기 (JavaScript)
// Load a Landsat 8 collection for a single path-row, 2021 images only. var collection = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)); // This function uses a conditional statement to return the image if // the solar elevation > 40 degrees. Otherwise it returns a "zero image". var conditional = function(image) { return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0)); }; // Map the function over the collection and print the result. Expand the // collection and note that 7 of the 22 images are now "zero images'. print('Expand this to see the result', collection.map(conditional));
import ee import geemap.core as geemap
Colab (Python)
# Load a Landsat 8 collection for a single path-row, 2021 images only. collection = ( ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA') .filterDate('2021', '2022') .filter(ee.Filter.eq('WRS_PATH', 44)) .filter(ee.Filter.eq('WRS_ROW', 34)) ) # This function uses a conditional statement to return the image if # the solar elevation > 40 degrees. Otherwise it returns a "zero image". def conditional(image): return ee.Algorithms.If( ee.Number(image.get('SUN_ELEVATION')).gt(40), image, ee.Image(0) ) # Map the function over the collection and print the result. Expand the # collection and note that 7 of the 22 images are now "zero images'. display('Expand this to see the result', collection.map(conditional))
출력 ImageCollection의 이미지 목록을 검사하고 If()
알고리즘에서 평가한 조건이 true이면 출력에 상수 이미지가 포함된다는 점에 유의합니다. 서버 측 조건부 함수를 보여주지만(Earth Engine의 클라이언트와 서버에 대해 자세히 알아보기) 일반적으로 If()
를 사용하지 말고 대신 필터를 사용하세요.