|
| 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] |
0 commit comments