0

Has anyone been able to use Earth Engine API inside their front-end JavaScript Code. I have been trying to follow the demo on the earth-engine repo to apply a layer on a map but with no results. I don't know exactly what's wrong but the function ee.data.authenticate doesn't seem to fire though I have my client ID passed to it.

Ramy Farid
  • 144
  • 1
  • 2
  • 15
  • Hey @Ramy Farid, have you figured out this functionality? – meDeepakJain May 24 '20 at 06:40
  • @meDeepakJain Not exactly but I could achieve this later with some program called maptiler. So earth engine was used to download the images only. For the images to be displayed as a layer on the map, I had to tile the images using MapTiler program and use a google maps image layer to show that NDVI image tiles on different zoom levels. – Ramy Farid May 26 '20 at 17:27

1 Answers1

0

You'll need to authenticate using the client-side OAuth method described here: https://developers.google.com/earth-engine/npm_install. Beyond that, you can use Google Maps and Earth Engine APIs as usual:

HTML:

<div id="map" style="width: 600px; height: 400px"></div>

JS:

// Load client library.
const ee = window.ee = require('@google/earthengine');
const CLIENT_ID = 'YOUR_CLIENT_ID';

window.initMap = function () {
  // Initialize client library.
  const initialize = function () {
    ee.initialize(null, null, () => {
      createMap();
    }, (e) => {
      console.error('Initialization error: ' + e);
    });
  };

  // Authenticate using an OAuth pop-up.
  ee.data.authenticateViaOauth(CLIENT_ID, initialize, (e) => {
    console.error('Authentication error: ' + e);
  }, null, () => {
    ee.data.authenticateViaPopup(initialize);
  });
};

function createMap() {
  // Initialize map.
  const mapEl = document.querySelector('#map');
  const map = new google.maps.Map(mapEl, {
    center: new google.maps.LatLng(39.8282, -98.5795),
    zoom: 5
  });

  // Load EE image.
  const image = ee.Image('srtm90_v4');
  image.getMap({ min: 0, max: 1000 }, ({ mapid, token }) => {

    // Create Google Maps overlay.
    const mapType = new google.maps.ImageMapType({
      getTileUrl: ({ x, y }, z) =>
        `https://earthengine.googleapis.com/map/${mapid}/${z}/${x}/${y}?token=${token}`,
      tileSize: new google.maps.Size(256, 256)
    });

    // Add the EE layer to the map.
    map.overlayMapTypes.push(mapType);

  });
}

In a real application you should also show a "Log in" button and not open the popup until then — otherwise the browser's popup blocking may prevent it from appearing.

Don McCurdy
  • 10,975
  • 2
  • 37
  • 75
  • I used exact code and also tried the short one from Google's website. But, nothing is working out. This above api gives response as `{valid:false}` while Google version shows error as *request is missing required authentication credentials*. Would you plz clarify what am I missing? – meDeepakJain May 24 '20 at 06:39