Unverified Commit 5546b37f authored by gsn9's avatar gsn9 Committed by GitHub
Browse files

Merge pull request #9 from amystamile/Demo

Demo 1 - API Calls and Footprint Rendering
parents ddf68339 b63da727
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line

function callAPI() {
    return fetch("http://asc-stacbrowser.s3-website-us-west-2.amazonaws.com/catalog.json")
           .then(response => response.json());
}


function getStacTargetCatalog(name) {
  if (name == "Mars") {
    return callAPI().then(result => {
      console.log("STAC Catalog for USGS: ");
      console.log(result);
      for (let i = 0; i < result.links.length; i++) {
        if (result.links[i].title == 'Mars Analysis Ready Data') {
          return fetch(result.links[i].href)
            .then(response => response.json())
        }
      }
    });
  }
  if (name == "Europa") {
    return callAPI().then(result => {
      console.log("STAC Catalog for USGS: ");
      console.log(result);
      for (let i = 0; i < result.links.length; i++) {
        if (result.links[i].title == 'Jupiter Analysis Ready Data') {
          return fetch(result.links[i].href)
            .then(response => response.json())
        }
      }
    });
  }
}


function getStacMissionCatalogs(name) {
  return getStacTargetCatalog(name).then(result => {
    console.log("STAC Catalog(s) for Specific Target: ");
    console.log(result);
    for (let i = 0; i < result.links.length; i++) {
      if(result.links[i].rel == 'child'){
        return fetch(result.links[i].href)
          .then(response => response.json())
      }
    }
  });
}

function getItemCollection(name) {
  // ctx_dtms skips a step in which there is no mission catalog instead it
  // takes you straight to the collection from the target catalog
  if (name == "Mars"){
    return getStacTargetCatalog(name).then(result => {
      console.log("STAC Catalog(s) for Specific Target: ");
      console.log(result);
      for (let i = 0; i < result.links.length; i++) {
        if(result.links[i].rel == 'child'){
          return fetch(result.links[i].href)
            .then(response => response.json())
        }
      }
    });
  }
  if (name == "Europa") {
    return getStacMissionCatalogs(name).then(result => {
      console.log("STAC Collection for Specific Mission: ");
      console.log(result);
      return fetch(result.links[3].href)
        .then(response => response.json())
    })
  }
}

export{ getItemCollection };
+27 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import "proj4leaflet";

import AstroProj from "./AstroProj";
import LayerCollection from "./LayerCollection";
import { getItemCollection } from "./ApiJsonCollection";
import { MY_JSON_MAPS } from "./layers";

/**
@@ -90,6 +91,10 @@ export default L.Map.AstroMap = L.Map.extend({
    L.Map.prototype.initialize.call(this, this._mapDiv, this.options);
    this.loadLayerCollection("cylindrical");

    if(target == "Mars" || target == "Europa") {
      this.loadFootprintLayer(target);
    }

    // Listen to baselayerchange event so that we can set the current layer being
    // viewed by the map.
    this.on("baselayerchange", function(e) {
@@ -107,6 +112,28 @@ export default L.Map.AstroMap = L.Map.extend({
    this.layers[name].addTo(this);
  },

  /**
   * @function AstroMap.prototype.loadFootprintLayer
   * @description Adds the ApiJsonCollection with the requested name.
   *
   * @param {String} name - Name of the STAC Catalog. example "ctx_dtms"
   */
  loadFootprintLayer: function(name) {
    var footprintLayer = L.geoJSON().addTo(this);

    getItemCollection(name).then(result => {
        console.log("STAC Item Collection: ");
        console.log(result);
          for (let i = 0; i < result.links.length; i++) {
            if (result.links[i].rel == 'item') {
              fetch(result.links[i].href)
                .then(response => response.json())
                .then(data => footprintLayer.addData(data))
            }
          }
      });
  },

  /**
   * @function AstroMap.prototype.parseJSON
   * @description Parses the USGS JSON, creates layer objects for a particular target and projection,