관계, 조건부, 불리언 연산

ee.Image 객체에는 의사결정 표현식을 구성하기 위한 일련의 관계형, 조건부, 불리언 메서드가 있습니다. 이러한 방법의 결과는 마스킹, 분류된 지도 개발, 값 재할당을 통해 분석을 특정 픽셀 또는 영역으로 제한하는 데 유용합니다.

관계 및 불리언 연산자

관계형 메서드에는 다음이 포함됩니다.

eq(), gt(), gte(), lt(), lte()

불리언 메서드에는 다음이 포함됩니다.

코드 편집기 (JavaScript)

and(),or(), not()

Colab (Python)

And(),Or(), Not()

이미지 간의 픽셀별 비교를 실행하려면 관계 연산자를 사용하세요. 이미지에서 도시화된 지역을 추출하기 위해 이 예에서는 관계 연산자를 사용하여 스펙트럼 지수를 임곗값으로 설정하고 임곗값을 and 연산자와 결합합니다.

코드 편집기 (JavaScript)

// Load a Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318');

// Create NDVI and NDWI spectral indices.
var ndvi = image.normalizedDifference(['B5', 'B4']);
var ndwi = image.normalizedDifference(['B3', 'B5']);

// Create a binary layer using logical operations.
var bare = ndvi.lt(0.2).and(ndwi.lt(0));

// Mask and display the binary layer.
Map.setCenter(-122.3578, 37.7726, 12);
Map.setOptions('satellite');
Map.addLayer(bare.selfMask(), {}, 'bare');

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Load a Landsat 8 image.
image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318')

# Create NDVI and NDWI spectral indices.
ndvi = image.normalizedDifference(['B5', 'B4'])
ndwi = image.normalizedDifference(['B3', 'B5'])

# Create a binary layer using logical operations.
bare = ndvi.lt(0.2).And(ndwi.lt(0))

# Define a map centered on San Francisco Bay.
map_bare = geemap.Map(center=[37.7726, -122.3578], zoom=12)

# Add the masked image layer to the map and display it.
map_bare.add_layer(bare.selfMask(), None, 'bare')
display(map_bare)

이 예에서 볼 수 있듯이 관계 및 불리언 연산자의 출력은 true (1) 또는 false (0)입니다. 0을 마스크하려면 selfMask()를 사용하여 결과 바이너리 이미지를 자체로 마스크하면 됩니다.

relational_sf
미국 캘리포니아 샌프란시스코의 Landsat 8에서 측정한 NDVI가 낮고 NDWI가 낮은 지역 (흰색).

관계 및 불리언 연산자에 의해 반환되는 바이너리 이미지는 수학 연산자와 함께 사용할 수 있습니다. 이 예에서는 관계 연산자와 add()를 사용하여 야간 조명 이미지에서 도시화 영역을 만듭니다.

코드 편집기 (JavaScript)

// Load a 2012 nightlights image.
var nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012');
var lights = nl2012.select('stable_lights');

// Define arbitrary thresholds on the 6-bit stable lights band.
var zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62));

// Display the thresholded image as three distinct zones near Paris.
var palette = ['000000', '0000FF', '00FF00', 'FF0000'];
Map.setCenter(2.373, 48.8683, 8);
Map.addLayer(zones, {min: 0, max: 3, palette: palette}, 'development zones');

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Load a 2012 nightlights image.
nl_2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012')
lights = nl_2012.select('stable_lights')

# Define arbitrary thresholds on the 6-bit stable lights band.
zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62))

# Define a map centered on Paris, France.
map_zones = geemap.Map(center=[48.8683, 2.373], zoom=8)

# Display the thresholded image as three distinct zones near Paris.
palette = ['000000', '0000FF', '00FF00', 'FF0000']
map_zones.add_layer(
    zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones'
)
display(map_zones)

조건부 연산자

이전 예의 코드는 expression()로 구현된 삼항 연산자를 사용하는 것과 같습니다.

코드 편집기 (JavaScript)

// Create zones using an expression, display.
var zonesExp = nl2012.expression(
    "(b('stable_lights') > 62) ? 3" +
      ": (b('stable_lights') > 55) ? 2" +
        ": (b('stable_lights') > 30) ? 1" +
          ": 0"
);
Map.addLayer(zonesExp,
             {min: 0, max: 3, palette: palette},
             'development zones (ternary)');

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Create zones using an expression, display.
zones_exp = nl_2012.expression(
    "(b('stable_lights') > 62) ? 3 "
    ": (b('stable_lights') > 55) ? 2 "
    ": (b('stable_lights') > 30) ? 1 "
    ': 0'
)

# Define a map centered on Paris, France.
map_zones_exp = geemap.Map(center=[48.8683, 2.373], zoom=8)

# Add the image layer to the map and display it.
map_zones_exp.add_layer(
    zones_exp, {'min': 0, 'max': 3, 'palette': palette}, 'zones exp'
)
display(map_zones_exp)

이전 표현식 예에서 관심 있는 밴드는 변수 이름 사전이 아닌 b() 함수를 사용하여 참조됩니다. 이 페이지에서 이미지 표현식에 대해 자세히 알아보세요. 수학 연산자 또는 표현식을 사용하면 동일한 결과가 나옵니다.

conditional_paris
프랑스 파리의 2012년 야간 조명 이미지 중 임의의 영역입니다.

이미지에 조건부 작업을 구현하는 또 다른 방법은 where() 연산자를 사용하는 것입니다. 마스크 처리된 픽셀을 다른 데이터로 대체해야 하는지 고려합니다. 다음 예에서는 where()를 사용하여 구름이 있는 픽셀을 구름이 없는 이미지의 픽셀로 바꿉니다.

코드 편집기 (JavaScript)

// Load a cloudy Sentinel-2 image.
var image = ee.Image(
  'COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG');
Map.addLayer(image,
             {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000},
             'original image');

// Load another image to replace the cloudy pixels.
var replacement = ee.Image(
  'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');

// Set cloudy pixels (greater than 5% probability) to the other image.
var replaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement);

// Display the result.
Map.setCenter(-122.3769, 37.7349, 11);
Map.addLayer(replaced,
             {bands: ['B4', 'B3', 'B2'], min: 0, max: 2000},
             'clouds replaced');

Python 설정

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

import ee
import geemap.core as geemap

Colab (Python)

# Load a cloudy Sentinel-2 image.
image = ee.Image('COPERNICUS/S2_SR/20210114T185729_20210114T185730_T10SEG')

# Load another image to replace the cloudy pixels.
replacement = ee.Image(
    'COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG'
)

# Set cloudy pixels (greater than 5% probability) to the other image.
replaced = image.where(image.select('MSK_CLDPRB').gt(5), replacement)

# Define a map centered on San Francisco Bay.
map_replaced = geemap.Map(center=[37.7349, -122.3769], zoom=11)

# Display the images on a map.
vis_params = {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 2000}
map_replaced.add_layer(image, vis_params, 'original image')
map_replaced.add_layer(replaced, vis_params, 'clouds replaced')
display(map_replaced)