1

I have a script that calculates number of pixels with NDVI values that are higher than 0.3, 0.5 and 0.7.

My code runs with no errors, but it get value 0 and doesn't really count.

what can be the reason?

var meanImage=ee.Image(byYear.get[0]);

var gt03 = meanImage.gt(0.3).selfMask().rename('NDVI_gt03');
var gt04 = meanImage.gt(0.5).selfMask().rename('NDVI_gt05');
var gt05 = meanImage.gt(0.7).selfMask().rename('NDVI_gt07');

Map.addLayer(gt03,NDVIcolor,'gt03');
Map.addLayer(gt04,NDVIcolor,'gt05');
Map.addLayer(gt05,NDVIcolor,'gt07');

//count the number of total pixels
var c03 = gt03.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });

var c05 = gt05.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry:geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });

  var c07 = gt07.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry:geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });
// print the dictionary output
print(c03,'Total Pixels gt 0.3 ');
print(c05,'Total Pixels gt 0.5 ');
print(c07,'Total Pixels gt 0.7 ');


enter image description here

I have tried also to use listOfImages but still got 0.

My end goal is to calculte the number of pixels with the given values for one image.

link for my code: https://code.earthengine.google.com/559d0922b50d67cf01e4d5b03a21666d

1 Answer 1

1

If you share your assets we can make sure the issue is the way you select the images:

By year is and object of type imageCollection, .get() won't select images for you. This should work:

var meanImage=ee.Image(byYear.first());
var gt03 = meanImage.gt(0.3).selfMask().rename('NDVI_gt03');
var gt05 = meanImage.gt(0.5).selfMask().rename('NDVI_gt05');
var gt07 = meanImage.gt(0.7).selfMask().rename('NDVI_gt07');
//count the number of total pixels
var c03 = gt03.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry: geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });

var c05 = gt05.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry:geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });

  var c07 = gt07.reduceRegion({
  reducer: ee.Reducer.count(),
  geometry:geometry,
  crs: 'EPSG:4326',
  scale: 30,
  });
// print the dictionary output
print(c03,'Total Pixels gt 0.3 ');
print(c05,'Total Pixels gt 0.5 ');
print(c07,'Total Pixels gt 0.7 ');

If you want to use .get() you'll have to turn the collection into a list first and select the element which you then cast to an ee.Image object.

In theory the 0 can also come from the fact there are no pixels in the ranges you define.

// EDIT

If you want to map over the images and return for each image the pixel count, one way of doing it could be like this:


    var listOfImages =(byYear.toList(byYear.size()));
    print(listOfImages, 'list')

    var stats = listOfImages.map(function(img){
      var image = ee.Image(img).select('NDVI_mean')

      var c03 = image.gt(0.3).selfMask().reduceRegion({
        reducer: ee.Reducer.count(),
        geometry: geometry,
        crs: 'EPSG:4326',
        scale: 30,
      }).values().get(0);

      var c05 = image.gt(0.5).selfMask().reduceRegion({
        reducer: ee.Reducer.count(),
        geometry: geometry,
        crs: 'EPSG:4326',
        scale: 30,
      }).values().get(0);

        var c07 = image.gt(0.7).selfMask().reduceRegion({
        reducer: ee.Reducer.count(),
        geometry: geometry,
        crs: 'EPSG:4326',
        scale: 30,
      }).values().get(0);

      return image.set('c03', c03).set('c05', c05).set('c07', c07)
    })
print(stats)

It is not a very elegant solution, but i'm no sure if this is what you are looking for.

7
  • it worked when I used first() but I don't want all the images. if you saw the link, I have the list of images as variable listOfImages but when I try to use this to count number if pixels it doesn't work and I get again 0
    – ReutKeller
    Commented Dec 10, 2019 at 7:50
  • I added the .map() version of the answer. It is not an elegant solution but it should work for your purpose. Please consider next time to share your tables/assets or provide a script that illustrates your problem with the geometry inside the script
    – Jobbo90
    Commented Dec 10, 2019 at 10:38
  • This code looks great but for some reason I got also empty list and I haven't gotten the number of pixels gt 0.3,0.5 and 0.7 .I liked the idea but don't know why it doesn't work. You can see here my code which suppse to have no geometry code.earthengine.google.com/49553ca984ad022b96f5dd0a3ebca9ae
    – ReutKeller
    Commented Dec 10, 2019 at 21:21
  • If you are not going to share your assets I can't help you with your problem: developers.google.com/earth-engine/asset_manager Please read the link and ask a new question
    – Jobbo90
    Commented Dec 11, 2019 at 7:50
  • 1
    code.earthengine.google.com/cf5d03f927dbed5128e828e826277ee4 Not an issue here.
    – Jobbo90
    Commented Dec 11, 2019 at 10:31

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.