I calculated NDVI using Sentinel 2 image collection by mapping a function over the image collection and then returning the expression inside the function as a band .addBands(expression).rename('NDVI')

Upon printing, every image had a new band 'NDVI' in the console. Next I created a mean of image collection to make a single image and selected the 'NDVI' band I just created.

Next I clipped the NDVI image using a shape file. Now all I want is to print the pixel area of the clipped NDVI image inside Google Earth Engine console.

1 Answer 1


If the images is not masked at any pixel, it wouldn't make much sense to calculate the pixel area, since it will be the area of the geometry.

This would be an example on calculating the area of "vegetated" pixels

var s2 = ee.ImageCollection('COPERNICUS/S2')
var geometry = ee.Geometry.Polygon(
        [[[-54.706003083973854, -27.1180763669586],
          [-54.706003083973854, -27.12567743494121],
          [-54.69518841722581, -27.12567743494121],
          [-54.69518841722581, -27.1180763669586]]], null, false)

var col = s2.filterBounds(geometry).limit(10)
var ndvi = col.map(function(img){ return img.normalizedDifference(['B8', 'B4']).rename('ndvi')})
var mosaic = ndvi.mean().clip(geometry)

var veg_thres = 0.5

var area_image = ee.Image.pixelArea().divide(1e4)
var final = mosaic.addBands(area_image)
var masked = final.updateMask(mosaic.gte(veg_thres))

Map.addLayer(masked, {bands:['ndvi'], palette:['yellow', 'green']}, 'NDVI')

var vegetated_area = masked.select('area').reduceRegion(
    reducer: ee.Reducer.sum(),
    geometry: geometry,
    scale: 10

var total_area = final.select('area').reduceRegion(
    reducer: ee.Reducer.sum(),
    geometry: geometry,
    scale: 10

print('vegetated area (ha):', vegetated_area.get('area'))
print('total area (ha):', total_area.get('area'))

link: https://code.earthengine.google.com/e134cd8a50e68bcbb20e877ee1434fc0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.