0

I'm using Google Earth Engine for a GIS project and I'm extracting an average density from a WorldPop layer and for a precise area defined by a geometry. Further in my code, I want to use this average density as a float, here is my code :

// Layer used for population density
var datapop = ee.ImageCollection("WorldPop/GP/100m/pop").mosaic().clip(geometry_pop) ;

// Average, max and minimum population density for the selected area
var density = datapop.reduce(ee.Reducer.first()).reduceRegion(ee.Reducer.mean(),geometry_pop,100).get('first');

I've tried some prints to see what was the type conversion problem :

print(typeof density)                 //prints JSON object
print(typeof parseFloat(density))     //prints JSON number

var test = 9.0 ;
print(typeof test)                    //prints JSON number
print(density)                //prints 9.960041574584405
print(parseFloat(density))    //prints NaN

Does someone know how can I convert my JSON object into a JSON number without getting its value to NaN ?

1 Answer 1

0

Everything Earth Engine does is server-side; all of the variables you have in your browser are just proxy objects describing what computation to perform. In order to get a computed value into the browser, you have request the result using either getInfo() (print does this for you automatically) or evaluate().

See this page for more information: https://developers.google.com/earth-engine/guides/client_server

That said, if you're going to continue to use the result in Earth Engine, you don't need to do that, you just pass the object into the next function.

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.