Skip to content

Commit d8131e2

Browse files
schwehrcopybara-github
authored andcommitted
Add ee.Reducer.count examples.
PiperOrigin-RevId: 865445624
1 parent fd12eba commit d8131e2

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2026 The Google Earth Engine Community Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START earthengine__apidocs__ee_reducer_count]
18+
print(ee.List([]).reduce(ee.Reducer.count())); // 0
19+
print(ee.List([0]).reduce(ee.Reducer.count())); // 1
20+
print(ee.List([-1]).reduce(ee.Reducer.count())); // 1
21+
print(ee.List([1, null, 3]).reduce(ee.Reducer.count())); // 2
22+
print(ee.List([1, '', 3]).reduce(ee.Reducer.count())); // 3
23+
24+
print(ee.Array([1, 0, 3]).reduce(ee.Reducer.count(), [0])); // [3]
25+
26+
var anArray = ee.Array([[1, 0, 3], [1, 2, 3]]);
27+
print(anArray.reduce(ee.Reducer.count(), [0])); // [[2, 2, 2]]
28+
print(anArray.reduce(ee.Reducer.count(), [1])); // [[3], [3]]
29+
print(anArray.reduce(ee.Reducer.count(), [1, 0])); // [[6]]
30+
31+
// Use reduceRegion to apply count().
32+
var elev = ee.Image('CGIAR/SRTM90_V4');
33+
var roi = ee.Geometry.Point([-119.86, 37.74]).buffer(5000);
34+
35+
// Create a mask where elevation is greater than 2000 meters.
36+
var highElevMask = elev.gt(2000);
37+
38+
// Update the image with the mask. Pixels = 0 in the mask become null/masked.
39+
var maskedElev = elev.updateMask(highElevMask);
40+
41+
// Run the count reducer. Masked pixels are ignored.
42+
var highElevCount = maskedElev.reduceRegion({
43+
reducer: ee.Reducer.count(),
44+
geometry: roi,
45+
scale: 90,
46+
maxPixels: 1e9
47+
});
48+
49+
print('Count of pixels > 2000m:', highElevCount.get('elevation')); // 20
50+
// [END earthengine__apidocs__ee_reducer_count]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2026 The Google Earth Engine Community Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START earthengine__apidocs__ee_reducer_count]
16+
display(ee.List([]).reduce(ee.Reducer.count())) # 0
17+
display(ee.List([0]).reduce(ee.Reducer.count())) # 1
18+
display(ee.List([-1]).reduce(ee.Reducer.count())) # 1
19+
display(ee.List([1, None, 3]).reduce(ee.Reducer.count())) # 2
20+
display(ee.List([1, '', 3]).reduce(ee.Reducer.count())) # 3
21+
22+
display(ee.Array([1, 0, 3]).reduce(ee.Reducer.count(), [0])) # [3]
23+
24+
an_array = ee.Array([[1, 0, 3], [1, 2, 3]])
25+
display(an_array.reduce(ee.Reducer.count(), [0])) # [[2, 2, 2]]
26+
display(an_array.reduce(ee.Reducer.count(), [1])) # [[3], [3]]
27+
display(an_array.reduce(ee.Reducer.count(), [1, 0])) # [[6]]
28+
29+
# Use reduceRegion to apply count().
30+
elev = ee.Image('CGIAR/SRTM90_V4')
31+
roi = ee.Geometry.Point([-119.86, 37.74]).buffer(5000)
32+
33+
# Create a mask where elevation is greater than 2000 meters.
34+
high_elev_mask = elev.gt(2000)
35+
36+
# Update the image with the mask. Pixels = 0 in the mask become null/masked.
37+
masked_elev = elev.updateMask(high_elev_mask)
38+
39+
# Run the count reducer. Masked pixels are ignored.
40+
high_elev_count = masked_elev.reduceRegion(
41+
reducer=ee.Reducer.count(),
42+
geometry=roi,
43+
scale=90,
44+
maxPixels=int(1e9)
45+
)
46+
47+
display('Count of pixels > 2000m:', high_elev_count.get('elevation')) # 20
48+
# [END earthengine__apidocs__ee_reducer_count]

0 commit comments

Comments
 (0)