WorldWindEarth / wildfire

The Wildfire Management Tool - WMT (v2.0) - a geo-browser that provides weather and fire behavior forecasts to ensure the safefy of firefighters and effective use of resources
https://worldwind.earth/wildfire
MIT License
15 stars 3 forks source link

Convert USGS from WMS services to WMTS #36

Closed emxsys closed 5 years ago

emxsys commented 5 years ago

The USGS Topo WMTS map service from the National Map is much faster than the WMS service and also contains Cache-Control headers.

emxsys commented 5 years ago

Attempted to use WMTS service. Was FAST, but georeferencing alignment was off E/W ~100m.

/* 
 * Copyright (c) 2016 Bruce Schubert.
 * The MIT License
 * http://www.opensource.org/licenses/mit-license
 */

/*global define, WorldWind, $ */

/**
 * The USGS TNM Topo Base Map layer.
 * 
 * @returns {UsgsTopoBaseMapLayer}
 */
define([
    'jquery',
    'worldwind'],
    function () {
    "use strict";
    /**
     * Constructs a USGS Topo map layer.
     * @constructor
     */
    var UsgsTopoBaseMapLayer = function () {
        WorldWind.Layer.call(this, "USGS Topo Basemap");

        // Web Map Tiling Service information from
        var serviceAddress = "https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/WMTS/1.0.0/WMTSCapabilities.xml";
        var layerIdentifier = "USGSTopo";
        var self = this;

        // Called asynchronously to parse and create the WMTS layer
        var createLayer = function (xmlDom) {
            // Create a WmtsCapabilities object from the XML DOM
            var wmtsCapabilities = new WorldWind.WmtsCapabilities(xmlDom);
            // Retrieve a WmtsLayerCapabilities object by the desired layer name
            var wmtsLayerCapabilities = wmtsCapabilities.getLayer(layerIdentifier);
            // Form a configuration object from the WmtsLayerCapabilities object
            var wmtsConfig = WorldWind.WmtsLayer.formLayerConfiguration(wmtsLayerCapabilities);
            // Create the WMTS Layer from the configuration object
            self.wmtsLayer = new WorldWind.WmtsLayer(wmtsConfig);
        };

        // Called if an error occurs during WMTS Capabilities document retrieval
        var logError = function (jqXhr, text, exception) {
            console.log("There was a failure retrieving the capabilities document: " + text + " exception: " + exception);
        };

        $.get(serviceAddress).done(createLayer).fail(logError);
    };
    UsgsTopoBaseMapLayer.prototype = Object.create(WorldWind.Layer.prototype);

    /**
     * Refreshes the data associated with this layer. The behavior of this function varies with the layer
     * type. For image layers, it causes the images to be re-retrieved from their origin.
     */
    UsgsTopoBaseMapLayer.prototype.refresh = function () {
       return this.wmtsLayer.refresh(dc);
    };

    /**
     * Subclass method called to display this layer. Subclasses should implement this method rather than the
     * [render]{@link Layer#render} method, which determines enable, pick and active altitude status and does not
     * call this doRender method if the layer should not be displayed.
     * @param {DrawContext} dc The current draw context.
     * @protected
     */
    UsgsTopoBaseMapLayer.prototype.doRender = function (dc) {
       return this.wmtsLayer.doRender(dc);
    };

    /**
     * Indicates whether this layer is within the current view. Subclasses may override this method and
     * when called determine whether the layer contents are visible in the current view frustum. The default
     * implementation always returns true.
     * @param {DrawContext} dc The current draw context.
     * @returns {boolean} true If this layer is within the current view, otherwise false.
     * @protected
     */
    UsgsTopoBaseMapLayer.prototype.isLayerInView = function (dc) {
        return this.wmtsLayer.isLayerInView(dc);
    };

    return UsgsTopoBaseMapLayer;
});
emxsys commented 5 years ago

Resolved the misregistration with the workaournd specified in this issue: https://github.com/NASAWorldWind/WebWorldWind/issues/793