Skip to content

Mathematical operations

Earth Engine supports many basic mathematical operators. They share some common features. Earth Engine performs math operations per pixel. When an operator is applied to an image, it's applied to each unmasked pixel of each band. In the case of operations on two images, the operation is only applied at the locations where pixels in both images are unmasked. Earth Engine automatically matches bands between images. When an operator is applied to two images, the images are expected to have the same number of bands so they can be matched pairwise. However, if one of the images has only a single band, it is matched with all of the bands in the other image, essentially replicating that band enough times to match the other image.

For a simple example, consider the task of creating the Normalized Difference Vegetation Index (NDVI) using Landsat imagery:

# Import earthengine API
import ee
import pprint

# Initialise
ee.Initialize()

# Configure the pretty printing output & initialize earthengine.
pp = pprint.PrettyPrinter(depth=4)

# Load two 5-year Landsat 7 composites.
landsat1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003')
landsat2008 = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012')

# Compute NDVI the hard way.
ndvi1999 = (landsat1999.select('B4').subtract(landsat1999.select('B3'))
    .divide(landsat1999.select('B4').add(landsat1999.select('B3'))).rename(['NDVI']))

# Compute NDVI the easy way.
ndvi2008 = landsat2008.normalizedDifference(['B4', 'B3']).rename(['NDVI'])

print('NDVI 1999')
pp.pprint(ndvi1999.getInfo())

print('\n'+'NDVI 2008')
pp.pprint(ndvi2008.getInfo())

As shown in the previous example, math operators perform basic arithmetic operations on image bands. The normalized difference operation is so common in remote sensing, Earth Engine provides a shortcut method, as shown in the second part of the example. Subtracting the images in this example results in a "change vector" for each pixel. Bands are matched automatically to perform the difference:

# Import earthengine API
import ee
import pprint

# Initialise
ee.Initialize()

# Configure the pretty printing output & initialize earthengine.
pp = pprint.PrettyPrinter(depth=4)

# Load two 5-year Landsat 7 composites get only 3bands from them
landsat1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(['B4', 'B3', 'B2'])
landsat2008 = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012').select(['B4', 'B3', 'B2'])

# Compute the multi-band difference image.
diff = landsat2008.subtract(landsat1999)
print('Difference')
pp.pprint(diff.getInfo())


# Compute the squared difference in each band.
squaredDifference = diff.pow(2)
print('\n'+'Squared Difference')
pp.pprint(squaredDifference.getInfo())

In the second part of this example, the squared difference is computed using image.pow(2). For the complete list of mathematical operators handling basic arithmetic, trigonometry, exponentiation, rounding, casting, bitwise operations and more, see the API documentation (in the Docs tab of the Earth Engine Code Editor).

Expressions

To implement more complex mathematical expressions, it may be more convenient to use image.expression(), which parses a text representation of a math operation. The following example uses expression() to compute the Enhanced Vegetation Index (EVI):

# Import earthengine API
import ee
import pprint

# Initialise
ee.Initialize()

# Configure the pretty printing output & initialize earthengine.
pp = pprint.PrettyPrinter(depth=4)


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

# Compute the EVI using an expression.
evi = image.expression(
    '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {
      'NIR': image.select('B5'),
      'RED': image.select('B4'),
      'BLUE': image.select('B2')
});

pp.pprint(evi.getInfo())

Observe that the first argument to expression is the textual representation of the math operation, the second argument is a dictionary where the keys are iable names used in the expression and the values are the image bands to which the iables should be mapped. Bands in the image may be referred to as b("band name") or b(index), for example b(0), instead of providing the dictionary. Note that division functions as it does in Python: dividing two integers results in an integer. For example 10 / 20 = 0. To change this behavior, multiply one of the operands by 1.0: 10 * 1.0 / 20 = 0.5. Supported expression operators are listed in the following table.

Type Symbol Operators for expression()
Arithmetic + - * / % ** Add, Subtract, Multiply, Divide, Modulus, Exponent
Comparison == != < > <= >= Equal, Not Equal, Less Than, Greater than, etc.
Logical &&
Ternary ? : If then else

  1. Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 4.0 Attribution License. Code samples are licensed under the Apache 2.0 License 

  2. Last updated August 4, 2019.