Unverified Commit bb1cc16b authored by amystamile's avatar amystamile Committed by GitHub
Browse files

Merge pull request #12 from amystamile/toggleFootprints

Updated APIJsonCollection
parents d4ad0e9b 04c4b4ca
Loading
Loading
Loading
Loading
+29 −61
Original line number Diff line number Diff line


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


function getStacTargetCatalog(name) {
  if (name == "Mars") {
    return callAPI().then(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") {
function getItemCollection(name) {
  var urlArray = [];
  return callAPI().then(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 => {
    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())
    for (let i = 0; i < result.collections.length; i++) {
      if (result.collections[i].summaries["ssys:targets"] == name.toLowerCase()) {
        let length = result.collections[i].links.length;
        for (let j = 0; j < length; j++) {
          let link = result.collections[i].links[j];
          if (link.rel == 'items') {
            var url = new URL(result.collections[i].links[j].href);
            urlArray.push(url);
          }
         }
  });
       }

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 => {
      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 (urlArray.length == 0) {
       return;
     }
    });
     let promiseArray = [];
     for (let i = 0; i < urlArray.length; i++) {
       promiseArray.push(fetch(urlArray[i]))
     }
  if (name == "Europa") {
    return getStacMissionCatalogs(name).then(result => {
        return Promise.all([
          fetch(result.links[3].href),
          fetch(result.links[4].href)
        ]).then(function (responses) {
     return Promise.all(promiseArray).then(function (responses) {
        return Promise.all(responses.map(function (response) {
	           return response.json();
        }));
      });
   })
 }
}

export{ getItemCollection };
+10 −17
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import "proj4leaflet";

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

/**
@@ -91,9 +91,7 @@ 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.
@@ -123,21 +121,16 @@ export default L.Map.AstroMap = L.Map.extend({
    var footprintCollection = {};

    getItemCollection(name).then(result => {
      let geoLayers = new Array(result.length);
      if (result != undefined){
        for (let i = 0; i < result.length; i++) {
        geoLayers[i] = L.geoJSON().addTo(this);
        footprintCollection[result[i].id] = geoLayers[i];
        for (let j = 0; j < result[i].links.length; j++) {
          if (result[i].links[j].rel == 'item') {
            fetch(result[i].links[j].href)
              .then(response => response.json())
              .then(function(data) {
                geoLayers[i].addData(data);
              })
          }
          let geoLayer = L.geoJSON().addTo(this);
          footprintCollection[result[i].features[0].collection] = geoLayer;
          for (let j = 0; j < result[i].features.length; j++) {
            geoLayer.addData(result[i].features[j]);
          }
        }
        L.control.layers(null, footprintCollection).addTo(this);
      }
    });   
  },