1

I want to filter a country border feature by countryname from GAUL_0 dataset.

When i change CountryName to "Turkey", this code works and draw Country border on the map.

But when i changed is for an undefined country, like below example, it does not print an error message.

How can i control or handle empty filter results?

var CountryName = "1Turkey"
var Gaul_0 = ee.FeatureCollection("FAO/GAUL/2015/level0")
var filtered0 = Gaul_0.filter(ee.Filter(ee.Filter.eq('ADM0_NAME',CountryName)))

print (filtered0)

if (filtered0!==null ) {
Map.addLayer(filtered0)
}
else
{
 print('undefined')
}

1 Answer 1

2

If you want to handle it on the client side, you only have to replace

if (filtered0!==null ) {

for

if (filtered0.size().getInfo() > 0 ) {

But sooner or later you'll need to handle it on the server side. Then, you'll need to use the function ee.Algorithms.If on the size of the filtered collection.

var result = ee.Algorithms.If(filtered.size(), true, false)

Don't forget to cast the result. For example:

var result = ee.String(ee.Algorithms.If(filtered.size(), 'true', 'false'))

I leave you here a working example: https://code.earthengine.google.com/ce0a9912f58afafb8b449636240464e0

3
  • Thank you very much, Rodrigo... Could you please take a look this question? gis.stackexchange.com/questions/402355/… Commented Jun 30, 2021 at 12:23
  • Any idea why ee.Algorithms.If(filtered.size()) seems to work but ee.Algorithms.If(filtered.size()>0) does not? Commented Feb 11, 2022 at 20:02
  • filtered.size() is computed on the server side, and the conditional >0 is checked on the client side. I think that is the problem. Commented Feb 13, 2022 at 18:21

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.