ee.Reducer.count

  • Computes the number of non-null inputs.

  • The output name is "count".

  • No arguments are required for this Reducer.

Returns a Reducer that computes the number of non-null inputs. Where applicable, the output name is "count".

UsageReturns
ee.Reducer.count()Reducer

No arguments.

Examples

Code Editor (JavaScript)

print(ee.List([]).reduce(ee.Reducer.count()));  // 0
print(ee.List([0]).reduce(ee.Reducer.count()));  // 1
print(ee.List([-1]).reduce(ee.Reducer.count()));  // 1
print(ee.List([1, null, 3]).reduce(ee.Reducer.count()));  // 2
print(ee.List([1, '', 3]).reduce(ee.Reducer.count()));  // 3

print(ee.Array([1, 0, 3]).reduce(ee.Reducer.count(), [0]));  // [3]

var anArray = ee.Array([[1, 0, 3], [1, 2, 3]]);
print(anArray.reduce(ee.Reducer.count(), [0]));  // [[2, 2, 2]]
print(anArray.reduce(ee.Reducer.count(), [1]));  // [[3], [3]]
print(anArray.reduce(ee.Reducer.count(), [1, 0]));  // [[6]]

// Use reduceRegion to apply count().
var elev = ee.Image('CGIAR/SRTM90_V4');
var roi = ee.Geometry.Point([-119.86, 37.74]).buffer(5000);

// Create a mask where elevation is greater than 2000 meters.
var highElevMask = elev.gt(2000);

// Update the image with the mask. Pixels = 0 in the mask become null/masked.
var maskedElev = elev.updateMask(highElevMask);

// Run the count reducer. Masked pixels are ignored.
var highElevCount = maskedElev.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: roi,
  scale: 90,
  maxPixels: 1e9
});

print('Count of pixels > 2000m:', highElevCount.get('elevation'));  // 20

Python setup

See the Python Environment page for information on the Python API and using geemap for interactive development.

import ee
import geemap.core as geemap

Colab (Python)

display(ee.List([]).reduce(ee.Reducer.count()))  # 0
display(ee.List([0]).reduce(ee.Reducer.count()))  # 1
display(ee.List([-1]).reduce(ee.Reducer.count()))  # 1
display(ee.List([1, None, 3]).reduce(ee.Reducer.count()))  # 2
display(ee.List([1, '', 3]).reduce(ee.Reducer.count()))  # 3

display(ee.Array([1, 0, 3]).reduce(ee.Reducer.count(), [0]))  # [3]

an_array = ee.Array([[1, 0, 3], [1, 2, 3]])
display(an_array.reduce(ee.Reducer.count(), [0]))  # [[2, 2, 2]]
display(an_array.reduce(ee.Reducer.count(), [1]))  # [[3], [3]]
display(an_array.reduce(ee.Reducer.count(), [1, 0]))  # [[6]]

# Use reduceRegion to apply count().
elev = ee.Image('CGIAR/SRTM90_V4')
roi = ee.Geometry.Point([-119.86, 37.74]).buffer(5000)

# Create a mask where elevation is greater than 2000 meters.
high_elev_mask = elev.gt(2000)

# Update the image with the mask. Pixels = 0 in the mask become null/masked.
masked_elev = elev.updateMask(high_elev_mask)

# Run the count reducer. Masked pixels are ignored.
high_elev_count = masked_elev.reduceRegion(
    reducer=ee.Reducer.count(),
    geometry=roi,
    scale=90,
    maxPixels=int(1e9)
)

display('Count of pixels > 2000m:', high_elev_count.get('elevation'))  # 20