Commit 45f1b58b authored by Kaitlyn's avatar Kaitlyn
Browse files

First batch of refactors.

parent 75b58fe4
Loading
Loading
Loading
Loading
+77 −11
Original line number Diff line number Diff line
@@ -38,23 +38,41 @@ export default L.Map.AstroMap = L.Map.extend({
    this._astroProj = new AstroProj();
    this._radii = this._astroProj.getRadii(this._target);

    // Set by layer collection or baselayerchange event
    this._currentLayer = null;

    // Store layers at map creation so we only need to create layers once.
    let cylLayerInfo = this.parseJSON("cylindrical");
    if (Object.entries(cylLayerInfo).length == 0) {
      throw "No entry in the JSON for {this._target}. Cannot instantiate a map.";
    }

    // Could not work with _
    this.layers = {
      northPolar: new LayerCollection(
        this._target,
        "north-polar stereographic"
      ),
      southPolar: new LayerCollection(
        this._target,
        "south-polar stereographic"
      ),
      cylindrical: new LayerCollection(this._target, "cylindrical")
      cylindrical: new LayerCollection("cylindrical", cylLayerInfo)
    };

    this._hasNorthPolar = !this.layers["northPolar"].isEmpty();
    this._hasSouthPolar = !this.layers["southPolar"].isEmpty();
    let northLayerInfo = this.parseJSON("north-polar stereographic");
    if (Object.entries(northLayerInfo).length == 0) {
      this._hasNorthPolar = false;
    } else {
      this._hasNorthPolar = true;
      this.layers[northPolar] = new LayerCollection(
        "north-polar stereographic",
        northLayerInfo
      );
    }

    let southLayerInfo = this.parseJSON("south-polar stereographic");
    if (Object.entries(southLayerInfo).length == 0) {
      this._hasSouthPolar = false;
    } else {
      this._hasSouthPolar = true;
      this.layers[northPolar] = new LayerCollection(
        "south-polar stereographic",
        southLayerInfo
      );
    }

    this._defaultProj = L.extend({}, L.CRS.EPSG4326, { R: this._radii["a"] });
    this.options["crs"] = this._defaultProj;
@@ -81,6 +99,54 @@ export default L.Map.AstroMap = L.Map.extend({
    this.layers[name].addTo(this);
  },

  /**
   * @function AstroMap.prototype.parseJSON
   * @description Parses the USGS JSON, creates layer objects for a particular target and projection,
   *              and stores them in a JS object.
   * @return {Object} - Dictionary containing the layer information in the format: {base: [], overlays: []}
   */
  parseJSON: function(projection) {
    let layers = {
      base: [],
      overlays: [],
      wfs: []
    };

    let targets = MY_JSON_MAPS["targets"];
    for (let i = 0; i < targets.length; i++) {
      let currentTarget = targets[i];

      if (currentTarget["name"].toLowerCase() == this._target.toLowerCase()) {
        let jsonLayers = currentTarget["webmap"];
        for (let j = 0; j < jsonLayers.length; j++) {
          let currentLayer = jsonLayers[j];
          if (
            currentLayer["projection"].toLowerCase() != projection.toLowerCase()
          ) {
            continue;
          }
          if (currentLayer["type"] == "WMS") {
            // Base layer check
            if (currentLayer["transparent"] == "false") {
              layers["base"].push(currentLayer);
              if (currentLayer["primary"] == "true") {
                this._defaultLayerIndex = layers["base"].length - 1;
              }
            } else {
              // Do not add "Show Feature Names" PNG layer.
              if (currentLayer["displayname"] != "Show Feature Names") {
                layers["overlays"].push(currentLayer);
              }
            }
          } else {
            layers["wfs"].push(currentLayer);
          }
        }
      }
    }
    return layers;
  },

  /**
   * @function AstroMap.prototype.changeProjection
   * @description Changes the projection of the map and resets the center and view.
+11 −62
Original line number Diff line number Diff line
@@ -18,8 +18,7 @@ export default L.LayerCollection = L.Class.extend({
   * @param {String} target Name of the target.
   * @param {String} projName Name of the projection.
   */
  initialize: function(target, projName) {
    this._target = target;
  initialize: function(projName, layerInfo) {
    this._projName = projName;
    this._baseLayers = {};
    this._overlays = {};
@@ -27,56 +26,8 @@ export default L.LayerCollection = L.Class.extend({
    this._wfsLayer = null;
    L.LayerCollection.layerControl = null;

    let layers = this.parseJSON();
    this.createBaseLayers(layers["base"]);
    this.createOverlays(layers["overlays"]);
  },

  /**
   * @function LayerCollection.prototype.parseJSON
   * @description Parses the USGS JSON, creates layer objects for a particular target and projection, and stores them in a JS object.
   * @return {Object} - Dictionary containing the layer information in the format: {base: [], overlays: []}
   */
  parseJSON: function() {
    let layers = {
      base: [],
      overlays: [],
      wfs: []
    };

    let targets = MY_JSON_MAPS["targets"];
    for (let i = 0; i < targets.length; i++) {
      let currentTarget = targets[i];

      if (currentTarget["name"].toLowerCase() == this._target.toLowerCase()) {
        let jsonLayers = currentTarget["webmap"];
        for (let j = 0; j < jsonLayers.length; j++) {
          let currentLayer = jsonLayers[j];
          if (
            currentLayer["projection"].toLowerCase() !=
            this._projName.toLowerCase()
          ) {
            continue;
          }
          if (currentLayer["type"] == "WMS") {
            // Base layer check
            if (currentLayer["transparent"] == "false") {
              layers["base"].push(currentLayer);
              if (currentLayer["primary"] == "true") {
                this._defaultLayerIndex = layers["base"].length - 1;
              }
            } else {
              if (currentLayer["displayname"] != "Show Feature Names") {
                layers["overlays"].push(currentLayer);
              }
            }
          } else {
            layers["wfs"].push(currentLayer);
          }
        }
      }
    }
    return layers;
    this.createBaseLayers(layerInfo["base"]);
    this.createOverlays(layerInfo["overlays"]);
  },

  /**
@@ -87,7 +38,6 @@ export default L.LayerCollection = L.Class.extend({
  createBaseLayers: function(layers) {
    for (let i = 0; i < layers.length; i++) {
      let layer = layers[i];
      if (layer["projection"] == this._projName) {
      let baseLayer = L.tileLayer.wms(
        String(layer["url"]) + "?map=" + String(layer["map"]),
        {
@@ -97,7 +47,6 @@ export default L.LayerCollection = L.Class.extend({
      let name = String(layer["displayname"]);
      this._baseLayers[name] = baseLayer;
    }
    }
  },

  /**
+4 −5
Original line number Diff line number Diff line
@@ -41,9 +41,8 @@ describe("AstroMap", function() {
    expect(testMap).to.be.an("Object");
    expect(testMap.target() == target);
  });

  // // Move JSON parsing to map
  // it("should throw an exception with an invalid target", function() {
  //   let testMap = new AstroMap("map", "invalidTarget");
  // });
  // Move JSON parsing to map
  it("should throw an exception with an invalid target", function() {
    let testMap = new AstroMap("map", "invalidTarget");
  });
});