0

How can I pass a variable to Map.setCenter? I'm trying to use GEE to to view a location that comes from a variable. That is, the following works:

var scene = "LC80122482013122LGN01"
var compare = ee.String('LANDSAT_SCENE_ID == "').cat(scene).cat('"')
var im = ee.ImageCollection.load('LANDSAT/LC8_L1T_TOA').filter(ee.Filter.expression(compare))
var IM = ee.Image(im.first());
Map.setCenter(-27.4713134765625, 81.4474921754631, 10)
Map.addLayer(IM);

But if I try to center based on variables that come from a string, I get an error message. For example, by adding

var lon = ee.Number.parse(ee.String("-27.4713134765625"));
Map.setCenter(lon, 81.4474921754631, 10)

So the full example is:

var scene = "LC80122482013122LGN01"
var compare = ee.String('LANDSAT_SCENE_ID == "').cat(scene).cat('"')
var im = ee.ImageCollection.load('LANDSAT/LC8_L1T_TOA').filter(ee.Filter.expression(compare))
var IM = ee.Image(im.first());
var lon = ee.Number.parse(ee.String("-27.4713134765625"));
Map.setCenter(lon, 81.4474921754631, 10)
Map.addLayer(IM);

I get the error message

Provided center object has invalid values for lat, lon, or zoom. Expected numeric values.

2 Answers 2

1

The problem is that Map.setCenter is expecting number-objects on the client side as arguments, and in your example lon is actually a number-object on the server side. Thus, an easy way to transform a server side object to a client side one is using getInfo(). Here's the only change you need to make to your code. If you want to read more about server vs client side objects I suggest taking a look to the following link: https://developers.google.com/earth-engine/guides/client_server

Map.setCenter(lon.getInfo(), 81.4474921754631, 10)
1

Map.setCenter is expecting client-side JavaScript numeric literal arguments. The lon variable is a server-side ee.Number. When you pass it as an argument to Map.setCenter, it is interpreted literally as the serialized expression graph that represents the number.

The Map object is a special case of ui.Map and all ui components are client-side objects that expect client-side arguments, except when ee objects are specifically requested, like the eeObject parameter for Map.addLayer or the image parameter for ui.Thumbnail.

To transfer a server-side object client-side, use the evaluate method (ee.Number.evaluate in this case). The evaluate method is preferred over getInfo because it is asynchronous (i.e. it will not block any remaining computations while the object is retrieved – it will not cause your browser to hang).

var serverSideLon = ee.Number.parse(ee.String("-27.4713134765625"));

serverSideLon.evaluate(function(clientSideLon) {
  Map.setCenter(clientSideLon, 81.4474921754631, 10);
});
1
  • The fact that the majority of ui component arguments need to be client-side JavaScript literals is not well documented/highlighted in the Earth Engine Guide pages. I'll submit a request to add a linkable section that describes it. Sep 16 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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