Refactor WMS and WMTS asynchronous document retrieval

This commit is contained in:
Zach Glueckert 2017-02-22 15:39:54 -05:00
parent 8c12833494
commit 13ea3bd98f
3 changed files with 71 additions and 76 deletions

View File

@ -38,9 +38,15 @@ requirejs(['../src/WorldWind',
// Named layer displaying Average Temperature data
var layerName = "MOD_LSTD_CLIM_M";
// Asynchronous call to retrieve WMS GetCapabilities document, parse, and add as a layer
$.get(serviceAddress, function (xmlDom) {
// Create a WmsCapabilities object from the returned XML
var getDocument = function (serviceAddress) {
return $.get(serviceAddress)
.fail(function () {
console.log("There was an error while retrieving the WMS Capabilities document");
});
};
var createLayer = function (xmlDom) {
// Create a WmsCapabilities object from the XML DOM
var wms = new WorldWind.WmsCapabilities(xmlDom);
// Retrieve a WmsLayerCapabilities object by the desired layer name
var wmsLayerCapabilities = wms.getNamedLayer(layerName);
@ -54,8 +60,8 @@ requirejs(['../src/WorldWind',
// Add the layers to World Wind and create the layer manager
wwd.addLayer(wmsLayer);
layerManger.synchronizeLayerList();
})
.fail(function () {
console.log("Unable to load the WMS GetCapabilities document");
});
};
getDocument(serviceAddress).done(createLayer);
});

View File

@ -1,10 +1,7 @@
/*
* Copyright (C) 2014 United States Government as represented by the Administrator of the
* Copyright (C) 2016 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration. All Rights Reserved.
*/
/**
* @version $Id: WMTS.js 2016-07-12 rsirac $
*/
requirejs([
'../src/WorldWind',
@ -18,77 +15,53 @@ requirejs([
var wwd = new WorldWind.WorldWindow("canvasOne");
// Variable to store the capabilities documents
var wmtsCapabilities;
// Standard World Wind layers
var layers = [
{layer: new WorldWind.BMNGLayer(), enabled: true},
{layer: new WorldWind.BMNGLandsatLayer(), enabled: false},
{layer: new WorldWind.BingAerialLayer(null), enabled: false},
{layer: new WorldWind.BingAerialWithLabelsLayer(null), enabled: false},
{layer: new WorldWind.BingRoadsLayer(null), enabled: false},
{layer: new WorldWind.CompassLayer(), enabled: true},
{layer: new WorldWind.CoordinatesDisplayLayer(wwd), enabled: true},
{layer: new WorldWind.ViewControlsLayer(wwd), enabled: true}
];
// Fetch capabilities document
$.get('http://map1.vis.earthdata.nasa.gov/wmts-webmerc/wmts.cgi?SERVICE=WMTS&request=GetCapabilities', function (response) {
// Parse capabilities
wmtsCapabilities = new WorldWind.WmtsCapabilities(response);
})
.done(function () {
for (var l = 0; l < layers.length; l++) {
layers[l].layer.enabled = layers[l].enabled;
wwd.addLayer(layers[l].layer);
}
/* Create a layer from capabilities document */
{
// We can also precise a specific style, matrix set or image format in formLayerConfiguration method.
var config = WorldWind.WmtsLayer.formLayerConfiguration(wmtsCapabilities.contents.layer[0]);
var layer1 = new WorldWind.WmtsLayer(config, "2016-06-08");
}
// Create a layer manager for controlling layer visibility.
var layerManger = new LayerManager(wwd);
// Web Map Tiling Service information from
var serviceAddress = "https://tiles.geoservice.dlr.de/service/wmts?SERVICE=WMTS&REQUEST=GetCapabilities&VERSION=1.0.0";
// Layer displaying Global Hillshade based on GMTED2010
var layerName = "hillshade";
/* Create a layer on the fly / hardcoded */
{
// Create the resolutions array for tile matrix set
var maxResolution = 0.703125;
var resolutions = [];
for (var i = 0; i < 18; i++) {
resolutions.push(maxResolution / Math.pow(2, i));
}
var getDocument = function (serviceAddress) {
return $.get(serviceAddress)
.fail(function () {
console.log("There was an error while retrieving the WMTS Capabilities document");
});
};
// Create tile matrix set
var matrixset = WorldWind.WmtsLayer.createTileMatrixSet(
{
matrixSet: "EPSG:4326",
prefix: true,
projection: "EPSG:4326",
topLeftCorner: [90, -180],
extent: [-180, -90, 180, 90],
resolutions: resolutions,
tileSize: 256
}
);
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(layerName);
// Form a configuration object from the WmtsLayerCapablities object
var wmtsConfig = WorldWind.WmtsLayer.formLayerConfiguration(wmtsLayerCapabilities);
// Create the WMTS Layer from the configuration object
var wmtsLayer = new WorldWind.WmtsLayer(wmtsConfig);
// Create the layer
var layer2 = new WorldWind.WmtsLayer(
{
identifier: "eoc:world_relief_bw",
service: "https://tiles.geoservice.dlr.de/service/wmts?",
format: "image/png",
tileMatrixSet: matrixset,
style: "default",
title: "World Relief (GeoService)"
}
);
}
// Add the layers to World Wind and create the layer manager
wwd.addLayer(wmtsLayer);
layerManger.synchronizeLayerList();
}
getDocument(serviceAddress).done(createLayer);
// Create layer list
var layers = [
{layer: layer2, enabled: true},
{layer: layer1, enabled: true},
{layer: new WorldWind.CompassLayer(), enabled: true},
{layer: new WorldWind.CoordinatesDisplayLayer(wwd), enabled: true},
{layer: new WorldWind.ViewControlsLayer(wwd), enabled: true}
];
// Add the layers
for (var l = 0; l < layers.length; l++) {
layers[l].layer.enabled = layers[l].enabled;
wwd.addLayer(layers[l].layer);
}
// Create a layer manager for controlling layer visibility.
var layerManager = new LayerManager(wwd);
});
});

View File

@ -47,6 +47,22 @@ define([
this.assembleDocument(xmlDom);
};
WmtsCapabilities.prototype.getLayer = function (identifier) {
if (!identifier) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "WmtsCapabilities", "getLayer", "empty identifier"));
}
for (var i = 0, len = this.contents.layer.length; i < len; i++) {
var wmtsLayerCapabilities = this.contents.layer[i];
if (wmtsLayerCapabilities.identifier === identifier) {
return wmtsLayerCapabilities;
}
}
return null;
};
WmtsCapabilities.prototype.assembleDocument = function (dom) {
var root = dom.documentElement;