0

I am trying to set the null values of NDVI to zero for districts in Indonesia. However, when I export the table, the mean value for all districts come up as zero, which is not correct. Is there a way to correctly export the mean value for all districts, whilst setting the value of null to be zero? Here is my attempt at the code:

  var districtsindo = shp4.select('NAME_3'); 



 var water_district = Ndvi.reduceRegions({
  collection:districtsindo,
  reducer:ee.Reducer.mean(),
  scale:30, 
})  


function removegeo(feature) {
 var meanvalue=feature.get('mean')
 if (ee.Number(meanvalue).eq(null)){ 
 
   feature = feature.set({'mean': 0})
 }
  feature = feature.setGeometry(null);
  return feature
}

 var water_district = water_district.map(removegeo); 



Export.table.toDrive({
  collection:water_district,
  description: 'Tablebydistrict',
  fileFormat: 'CSV'
});  

1 Answer 1

1

First, you need to read and understand this: https://developers.google.com/earth-engine/guides/client_server#conditionals

But it's better to not use a conditional at all here (ie: don't use ee.Algorithms.If, except as a last resort). Instead, you can get the value with a default and re-set it. (But you need to convert the feature to a dictionary first to use the get-with-default).

function removegeo(feature) {
   var meanvalue = feature.toDictionary().get('mean', 0)
   return feature.set('mean', meanvalue)
}

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.