Image workflows#

Packages#

import ee
import geemap

try:
    ee.Initialize(project="ee-pflugmacher")
except Exception as e:
    ee.Authenticate()
    ee.Initialize(project="ee-pflugmacher")

Set up map window#

Map = geemap.Map(center=[53, 12], zoom=8)

Load an image#

Let’s load a Landsat-8 image from the GEE data catalog.

l8_img = ee.Image('LANDSAT/LC08/C02/T1_L2/LC08_193024_20180520')

We can explore the content of an ee.Image by printing it. In a python console you would use print() or getInfo(). Thanks to the eerepr package implemented in geemap, we can simply evaluate the objective in Jupyter Notebook and see the content in a nice collapsible tree.

l8_img
  • Image LANDSAT/LC08/C02/T1_L2/LC08_193024_20180520 (19 bands)
    • type:Image
    • id:LANDSAT/LC08/C02/T1_L2/LC08_193024_20180520
    • version:1629940279037627
    • bands: List (19 elements)
      • 0: "SR_B1", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B1
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 1: "SR_B2", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B2
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 2: "SR_B3", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B3
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 3: "SR_B4", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B4
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 4: "SR_B5", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B5
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 5: "SR_B6", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B6
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 6: "SR_B7", unsigned int16, EPSG:32633, 8051x8141 px
        • id:SR_B7
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 7: "SR_QA_AEROSOL", unsigned int8, EPSG:32633, 8051x8141 px
        • id:SR_QA_AEROSOL
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int8
          • type:PixelType
          • max:255
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 8: "ST_B10", unsigned int16, EPSG:32633, 8051x8141 px
        • id:ST_B10
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 9: "ST_ATRAN", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_ATRAN
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 10: "ST_CDIST", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_CDIST
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 11: "ST_DRAD", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_DRAD
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 12: "ST_EMIS", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_EMIS
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 13: "ST_EMSD", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_EMSD
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 14: "ST_QA", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_QA
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 15: "ST_TRAD", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_TRAD
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 16: "ST_URAD", signed int16, EPSG:32633, 8051x8141 px
        • id:ST_URAD
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: signed int16
          • type:PixelType
          • max:32767
          • min:-32768
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 17: "QA_PIXEL", unsigned int16, EPSG:32633, 8051x8141 px
        • id:QA_PIXEL
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
      • 18: "QA_RADSAT", unsigned int16, EPSG:32633, 8051x8141 px
        • id:QA_RADSAT
        • crs:EPSG:32633
        • crs_transform: [30, 0, 225585, 0, -30, 5850915]
          • 0:30
          • 1:0
          • 2:225585
          • 3:0
          • 4:-30
          • 5:5850915
        • data_type: unsigned int16
          • type:PixelType
          • max:65535
          • min:0
          • precision:int
        • dimensions: [8051, 8141]
          • 0:8051
          • 1:8141
    • properties: Object (94 properties)
      • ALGORITHM_SOURCE_SURFACE_REFLECTANCE:LaSRC_1.5.0
      • ALGORITHM_SOURCE_SURFACE_TEMPERATURE:st_1.3.0
      • CLOUD_COVER:0.04
      • CLOUD_COVER_LAND:0.04
      • COLLECTION_CATEGORY:T1
      • COLLECTION_NUMBER:2
      • DATA_SOURCE_AIR_TEMPERATURE:MODIS
      • DATA_SOURCE_ELEVATION:GLS2000
      • DATA_SOURCE_OZONE:MODIS
      • DATA_SOURCE_PRESSURE:Calculated
      • DATA_SOURCE_REANALYSIS:GEOS-5 FP-IT
      • DATA_SOURCE_TIRS_STRAY_LIGHT_CORRECTION:TIRS
      • DATA_SOURCE_WATER_VAPOR:MODIS
      • DATE_ACQUIRED:2018-05-20
      • DATE_PRODUCT_GENERATED:1598926706000
      • DATUM:WGS84
      • EARTH_SUN_DISTANCE:1.0119074
      • ELLIPSOID:WGS84
      • GEOMETRIC_RMSE_MODEL:6.462
      • GEOMETRIC_RMSE_MODEL_X:3.948
      • GEOMETRIC_RMSE_MODEL_Y:5.115
      • GEOMETRIC_RMSE_VERIFY:5.216
      • GRID_CELL_SIZE_REFLECTIVE:30
      • GRID_CELL_SIZE_THERMAL:30
      • GROUND_CONTROL_POINTS_MODEL:714
      • GROUND_CONTROL_POINTS_VERIFY:169
      • GROUND_CONTROL_POINTS_VERSION:5
      • IMAGE_QUALITY_OLI:9
      • IMAGE_QUALITY_TIRS:9
      • L1_DATE_PRODUCT_GENERATED:2020-09-01T02:07:45Z
      • L1_LANDSAT_PRODUCT_ID:LC08_L1TP_193024_20180520_20200901_02_T1
      • L1_PROCESSING_LEVEL:L1TP
      • L1_PROCESSING_SOFTWARE_VERSION:LPGS_15.3.1c
      • L1_REQUEST_ID:L2
      • LANDSAT_PRODUCT_ID:LC08_L2SP_193024_20180520_20200901_02_T1
      • LANDSAT_SCENE_ID:LC81930242018140LGN00
      • MAP_PROJECTION:UTM
      • NADIR_OFFNADIR:NADIR
      • ORIENTATION:NORTH_UP
      • PROCESSING_LEVEL:L2SP
      • PROCESSING_SOFTWARE_VERSION:LPGS_15.3.1c
      • REFLECTANCE_ADD_BAND_1:-0.2
      • REFLECTANCE_ADD_BAND_2:-0.2
      • REFLECTANCE_ADD_BAND_3:-0.2
      • REFLECTANCE_ADD_BAND_4:-0.2
      • REFLECTANCE_ADD_BAND_5:-0.2
      • REFLECTANCE_ADD_BAND_6:-0.2
      • REFLECTANCE_ADD_BAND_7:-0.2
      • REFLECTANCE_MULT_BAND_1:2.75e-05
      • REFLECTANCE_MULT_BAND_2:2.75e-05
      • REFLECTANCE_MULT_BAND_3:2.75e-05
      • REFLECTANCE_MULT_BAND_4:2.75e-05
      • REFLECTANCE_MULT_BAND_5:2.75e-05
      • REFLECTANCE_MULT_BAND_6:2.75e-05
      • REFLECTANCE_MULT_BAND_7:2.75e-05
      • REFLECTIVE_LINES:8141
      • REFLECTIVE_SAMPLES:8051
      • REQUEST_ID:L2
      • ROLL_ANGLE:-0.001
      • SATURATION_BAND_1:Y
      • SATURATION_BAND_2:Y
      • SATURATION_BAND_3:Y
      • SATURATION_BAND_4:Y
      • SATURATION_BAND_5:Y
      • SATURATION_BAND_6:Y
      • SATURATION_BAND_7:Y
      • SATURATION_BAND_8:N
      • SATURATION_BAND_9:N
      • SCENE_CENTER_TIME:10:01:54.2869600Z
      • SENSOR_ID:OLI_TIRS
      • SPACECRAFT_ID:LANDSAT_8
      • STATION_ID:LGN
      • SUN_AZIMUTH:152.6609467
      • SUN_ELEVATION:55.96719823
      • TARGET_WRS_PATH:193
      • TARGET_WRS_ROW:24
      • TEMPERATURE_ADD_BAND_ST_B10:149
      • TEMPERATURE_MAXIMUM_BAND_ST_B10:372.999941
      • TEMPERATURE_MINIMUM_BAND_ST_B10:149.003418
      • TEMPERATURE_MULT_BAND_ST_B10:0.00341802
      • THERMAL_LINES:8141
      • THERMAL_SAMPLES:8051
      • TIRS_SSM_MODEL:FINAL
      • TIRS_SSM_POSITION_STATUS:ESTIMATED
      • TRUNCATION_OLI:UPPER
      • UTM_ZONE:33
      • WRS_PATH:193
      • WRS_ROW:24
      • WRS_TYPE:2
      • system:asset_size:940822318
      • system:footprint: LinearRing (20 vertices)
        • type:LinearRing
        • coordinates: List (20 elements)
          • 0: [13.844222471195437, 50.99199285981453]
            • 0:13.844222471195437
            • 1:50.99199285981453
          • 1: [13.855826240119812, 51.01670999776455]
            • 0:13.855826240119812
            • 1:51.01670999776455
          • 2: [14.109322117820824, 51.551027757990994]
            • 0:14.109322117820824
            • 1:51.551027757990994
          • 3: [14.199191897246866, 51.73767783389154]
            • 0:14.199191897246866
            • 1:51.73767783389154
          • 4: [14.245648225349884, 51.833592384461866]
            • 0:14.245648225349884
            • 1:51.833592384461866
          • 5: [14.336717804876788, 52.02063747336056]
            • 0:14.336717804876788
            • 1:52.02063747336056
          • 6: [14.430005012502171, 52.21078734236527]
            • 0:14.430005012502171
            • 1:52.21078734236527
          • 7: [14.455496371206742, 52.26249023135963]
            • 0:14.455496371206742
            • 1:52.26249023135963
          • 8: [14.475477969190871, 52.303097598173466]
            • 0:14.475477969190871
            • 1:52.303097598173466
          • 9: [14.475466618839144, 52.30405617898129]
            • 0:14.475466618839144
            • 1:52.30405617898129
          • 10: [14.469332259247972, 52.30527927197727]
            • 0:14.469332259247972
            • 1:52.30527927197727
          • 11: [14.287603742404633, 52.33873884618632]
            • 0:14.287603742404633
            • 1:52.33873884618632
          • 12: [11.820444044010138, 52.76063592550844]
            • 0:11.820444044010138
            • 1:52.76063592550844
          • 13: [11.456841381709319, 51.90934418039607]
            • 0:11.456841381709319
            • 1:51.90934418039607
          • 14: [11.10761220515445, 51.060979227740944]
            • 0:11.10761220515445
            • 1:51.060979227740944
          • 15: [11.688514379484262, 50.96492566433909]
            • 0:11.688514379484262
            • 1:50.96492566433909
          • 16: [13.041056488959287, 50.728343519706264]
            • 0:13.041056488959287
            • 1:50.728343519706264
          • 17: [13.665013148144624, 50.613036090647]
            • 0:13.665013148144624
            • 1:50.613036090647
          • 18: [13.667128095206905, 50.613060323220125]
            • 0:13.667128095206905
            • 1:50.613060323220125
          • 19: [13.844222471195437, 50.99199285981453]
            • 0:13.844222471195437
            • 1:50.99199285981453
      • system:index:LC08_193024_20180520
      • system:time_end:1526810514286
      • system:time_start:1526810514286

Image properties#

Images contain several types of metadata information such as band names, projection information, properties, and other metadata. To understand what methods are available for an ee.Image object, you may consult the API documentation (see Client libraries).

We can retrieve the band names of the ee.Image using the bandNames method.

To understand what methods are available for an ee.Image object, you may consult the API documentation (see Client libraries). For example, the addBands() method adds a band to an image object. You must access the method from your image object not from ee.Image(), i.e., ee.Image.addBands() does not work.

l8_img.bandNames()
  • List (19 elements)
    • 0:SR_B1
    • 1:SR_B2
    • 2:SR_B3
    • 3:SR_B4
    • 4:SR_B5
    • 5:SR_B6
    • 6:SR_B7
    • 7:SR_QA_AEROSOL
    • 8:ST_B10
    • 9:ST_ATRAN
    • 10:ST_CDIST
    • 11:ST_DRAD
    • 12:ST_EMIS
    • 13:ST_EMSD
    • 14:ST_QA
    • 15:ST_TRAD
    • 16:ST_URAD
    • 17:QA_PIXEL
    • 18:QA_RADSAT

Get a list of all metadata properties.

properties = l8_img.propertyNames()
properties
  • List (98 elements)
    • 0:DATA_SOURCE_ELEVATION
    • 1:WRS_TYPE
    • 2:system:id
    • 3:REFLECTANCE_ADD_BAND_1
    • 4:REFLECTANCE_ADD_BAND_2
    • 5:DATUM
    • 6:REFLECTANCE_ADD_BAND_3
    • 7:REFLECTANCE_ADD_BAND_4
    • 8:REFLECTANCE_ADD_BAND_5
    • 9:REFLECTANCE_ADD_BAND_6
    • 10:REFLECTANCE_ADD_BAND_7
    • 11:system:footprint
    • 12:REFLECTIVE_SAMPLES
    • 13:system:version
    • 14:GROUND_CONTROL_POINTS_VERSION
    • 15:SUN_AZIMUTH
    • 16:DATA_SOURCE_TIRS_STRAY_LIGHT_CORRECTION
    • 17:UTM_ZONE
    • 18:DATE_ACQUIRED
    • 19:ELLIPSOID
    • 20:system:time_end
    • 21:DATA_SOURCE_PRESSURE
    • 22:LANDSAT_PRODUCT_ID
    • 23:STATION_ID
    • 24:TEMPERATURE_ADD_BAND_ST_B10
    • 25:DATA_SOURCE_REANALYSIS
    • 26:REFLECTANCE_MULT_BAND_7
    • 27:system:time_start
    • 28:REFLECTANCE_MULT_BAND_6
    • 29:L1_PROCESSING_LEVEL
    • 30:PROCESSING_SOFTWARE_VERSION
    • 31:L1_DATE_PRODUCT_GENERATED
    • 32:ORIENTATION
    • 33:REFLECTANCE_MULT_BAND_1
    • 34:WRS_ROW
    • 35:REFLECTANCE_MULT_BAND_3
    • 36:REFLECTANCE_MULT_BAND_2
    • 37:TARGET_WRS_ROW
    • 38:REFLECTANCE_MULT_BAND_5
    • 39:REFLECTANCE_MULT_BAND_4
    • 40:THERMAL_LINES
    • 41:TIRS_SSM_POSITION_STATUS
    • 42:GRID_CELL_SIZE_THERMAL
    • 43:IMAGE_QUALITY_TIRS
    • 44:TRUNCATION_OLI
    • 45:NADIR_OFFNADIR
    • 46:CLOUD_COVER
    • 47:REQUEST_ID
    • 48:EARTH_SUN_DISTANCE
    • 49:GEOMETRIC_RMSE_VERIFY
    • 50:TIRS_SSM_MODEL
    • 51:COLLECTION_CATEGORY
    • 52:SCENE_CENTER_TIME
    • 53:GRID_CELL_SIZE_REFLECTIVE
    • 54:SUN_ELEVATION
    • 55:ALGORITHM_SOURCE_SURFACE_TEMPERATURE
    • 56:TEMPERATURE_MAXIMUM_BAND_ST_B10
    • 57:CLOUD_COVER_LAND
    • 58:GEOMETRIC_RMSE_MODEL
    • 59:ROLL_ANGLE
    • 60:COLLECTION_NUMBER
    • 61:DATE_PRODUCT_GENERATED
    • 62:L1_REQUEST_ID
    • 63:DATA_SOURCE_OZONE
    • 64:SATURATION_BAND_1
    • 65:DATA_SOURCE_WATER_VAPOR
    • 66:SATURATION_BAND_2
    • 67:SATURATION_BAND_3
    • 68:IMAGE_QUALITY_OLI
    • 69:SATURATION_BAND_4
    • 70:LANDSAT_SCENE_ID
    • 71:SATURATION_BAND_5
    • 72:MAP_PROJECTION
    • 73:SATURATION_BAND_6
    • 74:SENSOR_ID
    • 75:SATURATION_BAND_7
    • 76:SATURATION_BAND_8
    • 77:WRS_PATH
    • 78:SATURATION_BAND_9
    • 79:TARGET_WRS_PATH
    • 80:L1_PROCESSING_SOFTWARE_VERSION
    • 81:TEMPERATURE_MULT_BAND_ST_B10
    • 82:L1_LANDSAT_PRODUCT_ID
    • 83:PROCESSING_LEVEL
    • 84:ALGORITHM_SOURCE_SURFACE_REFLECTANCE
    • 85:GROUND_CONTROL_POINTS_MODEL
    • 86:SPACECRAFT_ID
    • 87:TEMPERATURE_MINIMUM_BAND_ST_B10
    • 88:GEOMETRIC_RMSE_MODEL_Y
    • 89:REFLECTIVE_LINES
    • 90:GEOMETRIC_RMSE_MODEL_X
    • 91:THERMAL_SAMPLES
    • 92:system:asset_size
    • 93:DATA_SOURCE_AIR_TEMPERATURE
    • 94:GROUND_CONTROL_POINTS_VERIFY
    • 95:system:index
    • 96:system:bands
    • 97:system:band_names

Get a specific metadata property:

band_info = l8_img.get('CLOUD_COVER')
band_info
  • 0.04

Image math#

Image-based math operators are accessed via the created image object, e.g., image.add(), image.subtract, image.multiply(), image.divide(). Let’s calculate the NDVI from the Landsat 8 image. But first, we need to rescale the digital numbers to reflectance values using the gains and offset described in the data catalog.

l8_sr = l8_img.multiply(0.0000275).add(-0.2)

We can select individual bands:

nir = l8_sr.select("SR_B5")
red = l8_sr.select("SR_B4")

… and calculate the NDVI:

ndvi = nir.subtract(red) \
          .divide(nir.add(red)) \
          .rename('NDVI')
              
ndviVis = {"min":-1, "max":1, "palette": ['red', 'white', 'green']}
Map.addLayer(ndvi, ndviVis, 'NDVI')
Map

Logical operations#

We can create a binary mask based on the NDVI. For example, we might want to separate areas with low NDVI (bare or sparse vegetation - but not exact) from vegetation areas.

veg_mask = ndvi.gt(0.4)
Map.addLayer(veg_mask, {}, 'High NDVI')

Masking#

We can apply the mask using ee.Image.mask() or ee.Image.updateMask(). For example, we can do the following to mask out bare areas:

veg_image = ndvi.updateMask(veg_mask)
Map.addLayer(veg_image, ndviVis, 'NDVI vegetation')

Filter collections#

Usually, we want to limit our operations to a subset of a collection. We can do that by filtering the collection by date, location, and metadata attributes.

l8sr = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
geometry = ee.Geometry.Point([13.38, 52.52])

l8c = l8sr.filterDate('2016-07-01', '2017-07-08') \
          .filter(ee.Filter.lt('CLOUD_COVER', 70)) \
          .filterBounds(geometry)
l8c.count()
  • Image (19 bands)
    • type:Image
    • bands: List (19 elements)
      • 0: "SR_B1", unsigned int32, EPSG:4326
        • id:SR_B1
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 1: "SR_B2", unsigned int32, EPSG:4326
        • id:SR_B2
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 2: "SR_B3", unsigned int32, EPSG:4326
        • id:SR_B3
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 3: "SR_B4", unsigned int32, EPSG:4326
        • id:SR_B4
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 4: "SR_B5", unsigned int32, EPSG:4326
        • id:SR_B5
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 5: "SR_B6", unsigned int32, EPSG:4326
        • id:SR_B6
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 6: "SR_B7", unsigned int32, EPSG:4326
        • id:SR_B7
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 7: "SR_QA_AEROSOL", unsigned int32, EPSG:4326
        • id:SR_QA_AEROSOL
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 8: "ST_B10", unsigned int32, EPSG:4326
        • id:ST_B10
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 9: "ST_ATRAN", unsigned int32, EPSG:4326
        • id:ST_ATRAN
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 10: "ST_CDIST", unsigned int32, EPSG:4326
        • id:ST_CDIST
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 11: "ST_DRAD", unsigned int32, EPSG:4326
        • id:ST_DRAD
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 12: "ST_EMIS", unsigned int32, EPSG:4326
        • id:ST_EMIS
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 13: "ST_EMSD", unsigned int32, EPSG:4326
        • id:ST_EMSD
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 14: "ST_QA", unsigned int32, EPSG:4326
        • id:ST_QA
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 15: "ST_TRAD", unsigned int32, EPSG:4326
        • id:ST_TRAD
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 16: "ST_URAD", unsigned int32, EPSG:4326
        • id:ST_URAD
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 17: "QA_PIXEL", unsigned int32, EPSG:4326
        • id:QA_PIXEL
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int
      • 18: "QA_RADSAT", unsigned int32, EPSG:4326
        • id:QA_RADSAT
        • crs:EPSG:4326
        • crs_transform: [1, 0, 0, 0, 1, 0]
          • 0:1
          • 1:0
          • 2:0
          • 3:0
          • 4:1
          • 5:0
        • data_type: unsigned int32
          • type:PixelType
          • max:4294967295
          • min:0
          • precision:int

Map through a collection#

To apply a function to every Image in an ImageCollection use the imageCollection.map() method. The only argument to map() is a function which takes one parameter: an ee.Image. For example, the following code calculates the NDVI for each image. The resulting collection holds NDVI images corresponding to the Landsat image collection.

def calcNdvi(image):
  return image.normalizedDifference(["SR_B5", "SR_B4"]).rename("ndvi")

ndviCollection = l8c.map(calcNdvi)

# adds first image from collection to map canvas
Map.addLayer(ndviCollection.first(), ndviVis, "max NDVI")

Reduce a collection#

From the collection of NDVI images, we can now calculate aggregation statistics such as the mean, max, etc. Aggregating a collection to a single image is called reducing. Reducer’s (reducing functions) are available to you in two ways: 1) via the ee.Image object and 2) via the ee.Reducer object (help).

maxNdvi = ndviCollection.reduce(ee.Reducer.max())

medianNdvi = ndviCollection.median()

Map.addLayer(maxNdvi, {"min":0, "max":1, "palette": ['white', 'green']}, "max NDVI")

Map.addLayer(medianNdvi, {"min":0, "max":1, "palette": ['white', 'green']}, "median NDVI")
Map.centerObject(geometry, 10)

Map

Reducing an image#

In Google Earth Engine, reducing an image means applying a reducer to aggregate pixel values over a specified region or along a dimension, producing summary statistics such as mean, sum, minimum, maximum, or standard deviation. This is typically done with functions like reduceRegion() for a single region or reduceRegions() for many features

stats = l8_img.reduceRegion(
    reducer = ee.Reducer.minMax(),
    geometry = l8_img.geometry(),
    scale = 30,
    maxPixels = 1e9
)

stats
  • Object (38 properties)
    • QA_PIXEL_max:56916
    • QA_PIXEL_min:21762
    • QA_RADSAT_max:127
    • QA_RADSAT_min:0
    • SR_B1_max:56418
    • SR_B1_min:9
    • SR_B2_max:57482
    • SR_B2_min:8
    • SR_B3_max:61743
    • SR_B3_min:1650
    • SR_B4_max:61454
    • SR_B4_min:2110
    • SR_B5_max:60098
    • SR_B5_min:6459
    • SR_B6_max:62629
    • SR_B6_min:6646
    • SR_B7_max:64407
    • SR_B7_min:5908
    • SR_QA_AEROSOL_max:228
    • SR_QA_AEROSOL_min:1
    • ST_ATRAN_max:9228
    • ST_ATRAN_min:8400
    • ST_B10_max:54037
    • ST_B10_min:37728
    • ST_CDIST_max:2017
    • ST_CDIST_min:0
    • ST_DRAD_max:571
    • ST_DRAD_min:278
    • ST_EMIS_max:9997
    • ST_EMIS_min:6853
    • ST_EMSD_max:5726
    • ST_EMSD_min:0
    • ST_QA_max:5280
    • ST_QA_min:158
    • ST_TRAD_max:12211
    • ST_TRAD_min:6594
    • ST_URAD_max:1090
    • ST_URAD_min:478

geemap.image_stats is a convenience function (a wrapper around ee.Image.reduceRegion) in the geemap Python package that computes summary statistics (such as minimum, maximum, mean, and standard deviation) for all bands of an Earth Engine image over a specified region.

landsat_stats = geemap.image_stats(l8_img, region=l8_img.geometry(), scale=30)
landsat_stats
  • Object (5 properties)
    • max: Object (19 properties)
      • QA_PIXEL:56916
      • QA_RADSAT:127
      • SR_B1:56418
      • SR_B2:57482
      • SR_B3:61743
      • SR_B4:61454
      • SR_B5:60098
      • SR_B6:62629
      • SR_B7:64407
      • SR_QA_AEROSOL:228
      • ST_ATRAN:9228
      • ST_B10:54037
      • ST_CDIST:2017
      • ST_DRAD:571
      • ST_EMIS:9997
      • ST_EMSD:5726
      • ST_QA:5280
      • ST_TRAD:12211
      • ST_URAD:1090
    • mean: Object (19 properties)
      • QA_PIXEL:21838.41854148345
      • QA_RADSAT:0.00185210621977308
      • SR_B1:8303.776129219845
      • SR_B2:8499.614305915358
      • SR_B3:9449.536585439602
      • SR_B4:9180.228247499383
      • SR_B5:19219.32628780759
      • SR_B6:13346.072673605704
      • SR_B7:10954.44965966118
      • SR_QA_AEROSOL:100.97724766261926
      • ST_ATRAN:8766.365141429198
      • ST_B10:44064.15427761471
      • ST_CDIST:400.3146862631387
      • ST_DRAD:451.6119540392553
      • ST_EMIS:9755.895035060148
      • ST_EMSD:72.2433181819273
      • ST_QA:253.97541235587673
      • ST_TRAD:9025.223147478997
      • ST_URAD:837.1416982040838
    • min: Object (19 properties)
      • QA_PIXEL:21762
      • QA_RADSAT:0
      • SR_B1:9
      • SR_B2:8
      • SR_B3:1650
      • SR_B4:2110
      • SR_B5:6459
      • SR_B6:6646
      • SR_B7:5908
      • SR_QA_AEROSOL:1
      • ST_ATRAN:8400
      • ST_B10:37728
      • ST_CDIST:0
      • ST_DRAD:278
      • ST_EMIS:6853
      • ST_EMSD:0
      • ST_QA:158
      • ST_TRAD:6594
      • ST_URAD:478
    • std: Object (19 properties)
      • QA_PIXEL:599.8176156895673
      • QA_RADSAT:0.4148219447137856
      • SR_B1:726.2628980809488
      • SR_B2:835.853485644724
      • SR_B3:1083.388310374887
      • SR_B4:1493.6105067788237
      • SR_B5:3897.873536803412
      • SR_B6:2592.9476765994727
      • SR_B7:2625.445466137608
      • SR_QA_AEROSOL:26.54305060367694
      • ST_ATRAN:111.67054664476356
      • ST_B10:1241.2719262138194
      • ST_CDIST:308.5079667809496
      • ST_DRAD:36.79267406518335
      • ST_EMIS:99.13774203868262
      • ST_EMSD:60.89096493718093
      • ST_QA:63.44498666694878
      • ST_TRAD:498.16457881092
      • ST_URAD:78.13986222661306
    • sum: Object (19 properties)
      • QA_PIXEL:888837578083.2314
      • QA_RADSAT:75452
      • SR_B1:337964705525.2863
      • SR_B2:345938003682.7921
      • SR_B3:384602172389.7451
      • SR_B4:373641151086.9569
      • SR_B5:782238851114.4393
      • SR_B6:543193679047.6353
      • SR_B7:445853095371.0039
      • SR_QA_AEROSOL:4113660010.0470586
      • ST_ATRAN:356933794904
      • ST_B10:1771581189554.4119
      • ST_CDIST:16293063324.772549
      • ST_DRAD:18387959659.294113
      • ST_EMIS:392231745161.62354
      • ST_EMSD:2904513600.5372553
      • ST_QA:10210976938.403923
      • ST_TRAD:367473530467.1451
      • ST_URAD:34085297428.4902

Image geometry#

For catalog images in Earth Engine (such as Landsat or Sentinel scenes), ee.Image.geometry() usually returns the actual footprint of the scene, i.e., the spatial extent where the data are defined.

l8_img.geometry()
  • Polygon (20 vertices)
    • type:Polygon
    • coordinates: List (1 element)
      • 0: List (20 elements)
        • 0: [13.844222471195437, 50.99199285981453]
          • 0:13.844222471195437
          • 1:50.99199285981453
        • 1: [13.855826240119812, 51.01670999776455]
          • 0:13.855826240119812
          • 1:51.01670999776455
        • 2: [14.109322117820824, 51.551027757990994]
          • 0:14.109322117820824
          • 1:51.551027757990994
        • 3: [14.199191897246866, 51.73767783389154]
          • 0:14.199191897246866
          • 1:51.73767783389154
        • 4: [14.245648225349884, 51.833592384461866]
          • 0:14.245648225349884
          • 1:51.833592384461866
        • 5: [14.336717804876788, 52.02063747336056]
          • 0:14.336717804876788
          • 1:52.02063747336056
        • 6: [14.430005012502171, 52.21078734236527]
          • 0:14.430005012502171
          • 1:52.21078734236527
        • 7: [14.455496371206742, 52.26249023135963]
          • 0:14.455496371206742
          • 1:52.26249023135963
        • 8: [14.475477969190871, 52.303097598173466]
          • 0:14.475477969190871
          • 1:52.303097598173466
        • 9: [14.475466618839144, 52.30405617898129]
          • 0:14.475466618839144
          • 1:52.30405617898129
        • 10: [14.469332259247972, 52.30527927197727]
          • 0:14.469332259247972
          • 1:52.30527927197727
        • 11: [14.287603742404633, 52.33873884618632]
          • 0:14.287603742404633
          • 1:52.33873884618632
        • 12: [11.820444044010138, 52.76063592550844]
          • 0:11.820444044010138
          • 1:52.76063592550844
        • 13: [11.456841381709319, 51.90934418039607]
          • 0:11.456841381709319
          • 1:51.90934418039607
        • 14: [11.10761220515445, 51.060979227740944]
          • 0:11.10761220515445
          • 1:51.060979227740944
        • 15: [11.688514379484262, 50.96492566433909]
          • 0:11.688514379484262
          • 1:50.96492566433909
        • 16: [13.041056488959287, 50.728343519706264]
          • 0:13.041056488959287
          • 1:50.728343519706264
        • 17: [13.665013148144624, 50.613036090647]
          • 0:13.665013148144624
          • 1:50.613036090647
        • 18: [13.667128095206905, 50.613060323220125]
          • 0:13.667128095206905
          • 1:50.613060323220125
        • 19: [13.844222471195437, 50.99199285981453]
          • 0:13.844222471195437
          • 1:50.99199285981453

In contrast, for newly created or derived images (for example, images produced by arithmetic operations, band math, or reducers), the resulting image often has a default, unbounded or global geometry, because Earth Engine no longer tracks a meaningful spatial footprint through those operations. As a result, image.geometry() on such derived images commonly returns a geometry spanning the entire globe, unless the image has been explicitly clipped or its footprint has otherwise been constrained.

maxNdvi.geometry()
  • Polygon (5 vertices)
    • type:Polygon
    • coordinates: List (1 element)
      • 0: List (5 elements)
        • 0: [-180, -90]
          • 0:-180
          • 1:-90
        • 1: [180, -90]
          • 0:180
          • 1:-90
        • 2: [180, 90]
          • 0:180
          • 1:90
        • 3: [-180, 90]
          • 0:-180
          • 1:90
        • 4: [-180, -90]
          • 0:-180
          • 1:-90

Projections and scale#

The Earth Engine Data Catalog contains a vast amount of satellite data and geospatial data that all come in different projections and spatial resolutions. Fortunately, you do not have to worry about harmonizing between different coordinate reference systems and pixel sizes, the Earth Engine takes care of this in the background, but you need to understand some principles. The projection and scale (pixel size) is determined by the final output rather than the input, but the actual projection is performed right in the beginning of the analysis (see here). For example, if the output is the interactive Map display all input data are automatically projected to Web Mercator. The scale of the output in the Map window depends on the zoom factor. If you zoom out to see the entire globe, the scale is going to be very large, i.e., the computations are performed with pixels that might be hundreds of kilometers in size. This way you can quickly generate visual results though at the expense of spatial precision. Conversely, the scale will decrease the further you zoom in the Map display. In batch mode, the projection and scale is set in the export function using the arguments crs and scale or crs and crsTransform. If you do not specify crs and scale the projection will default to the geographic reference system WGS84 (EPSG=4326) and a potentially large scale. For many land cover and land use applications it is recommended to specify an equal area projection. An alternative to specifying scale is the crsTransform parameter which combines information on scale and the origin of the output pixel grid.

This is useful for aligning the pixel grids of multiple outputs. The crsTransform is a list of the following format [a, b, c, d, e, f], where

  • a is width of a pixel in map coordinates

  • b is row rotation (typically zero)

  • c is the x-coordinate of the upper-left corner of a pixel in the pixel grid

  • d is the column rotation (typically zero)

  • e is the height of a pixel (typically negative)

  • f is the y-coordinate of the upper-left corner of a pixel in the pixel grid

Batch environment#

Recall, Earth Engine data types are server-side objects that are created and processed entirely on cloud infrastructure, and that there are two different processing environments for you to request information: 1) an interactive environment optimized for speedy and small requests, and 2) a batch environment optimized for high-latency parallel processing of large amounts of data.

Batch processing is triggered when you use an import or export function (e.g., Export.* or ee.batch.). The batch environment let’s you export large datasets to your Assets Folder or Google Cloud Storage. You can manage (view and cancel) tasks using the task manager webpage, the task tab on the online code editor, and programmatically in Python. Look here for more information.

Export to asset#

The export functions allow you to compute results in batch mode as well as save to intermediate outputs to your Earth Engine account as an asset. The code below exports the image maxNdvi to your assets. For the export, we need to specify the region, a projection (crs), and pixel size (either via scale or crsTransform, see Projections and scale). Here we define a pixel size of 30 x 30 m.

You can use an uploaded polygon shapefile to define the region, and that is usually the standard workflow. However, you can also specify the region manually, draw geometries in the Map window, or use the extent of the Map window.

roi1 = ee.Geometry.Polygon(
        [[[12.232347553388268, 53.243183673553276],
          [12.232347553388268, 52.0636255084833],
          [14.737230365888268, 52.0636255084833],
          [14.737230365888268, 53.243183673553276]]])

You can draw a rectangle on the map window and get its coordinates with Map.draw_last_feature.

roi2 = Map.draw_last_feature.geometry()

Alternatively, you can grab the extent of the Map window.

roi3 = ee.Geometry.Rectangle(Map.bounds)
task = ee.batch.Export.image.toAsset(image=maxNdvi,
                                     description='maxNdvi', assetId='projects/ee-pflugmacher/assets/maxNdvi',
                                     region=roi1,
                                     crs= 'EPSG:3035', 
                                     crsTransform= [30, 0, 100, 0, -30, 100]
)

# start task
task.start()

Export via geemap#

geemap.ee_export_image(
    maxNdvi, filename='maxNDVI.tif', scale=90, 
    region=bounds, file_per_band=True
)

Export to Google Drive#

task = ee.batch.Export.image.toDrive(image=maxNdvi, folder='export', 
                                     description= 'landsat',
                                     region=bounds, scale=30)

# task.start()

Task management in Python#

tasks = ee.batch.Task.list()
for task in tasks:
    print(f'Task ID: {task.id}, State: {task.state}, Description: {task.config.get("description", "No description")}')
tasks = ee.batch.Task.list()
for task in tasks:
    print(f'Task ID: {task.id}, State: {task.state}, Description: {task.config.get("description", "No description")}')
    
for task in tasks:
    print(f'Task ID: {task.id}, State: {task.state}, Description: {task.config.get("description", "No description")}')
    if task.state == 'FAILED':
        print(f'Error: {task.error_message}')

Reading#