Skip to content

Commit

Permalink
Merge pull request #50 from ahocevar/wmscaps-singlepass
Browse files Browse the repository at this point in the history
Process WMSCapabilities property inheritance and flat layers list in a single pass for increased performance.
  • Loading branch information
ahocevar committed Nov 14, 2011
2 parents 754312b + 5e4ee5e commit 553eeb5
Showing 1 changed file with 49 additions and 118 deletions.
167 changes: 49 additions & 118 deletions lib/OpenLayers/Format/WMSCapabilities/v1.js
Expand Up @@ -71,117 +71,10 @@ OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class(
// an exception must have occurred, so parse it
var parser = new OpenLayers.Format.OGCExceptionReport();
capabilities.error = parser.read(raw);
} else {
// postprocess the layer list
this.postProcessLayers(capabilities);
}
return capabilities;
},

/**
* Method: postProcessLayers
* Post process the layers, so that the nested layer structure is converted
* to a flat layer list with only named layers.
*
* Parameters:
* capabilities - {Object} The object (structure) returned by the parser with
* all the info from the GetCapabilities response.
*/
postProcessLayers: function(capabilities) {
if (capabilities.capability) {
capabilities.capability.layers = [];
var layers = capabilities.capability.nestedLayers;
for (var i=0, len = layers.length; i<len; ++i) {
var layer = layers[i];
this.processLayer(capabilities.capability, layer);
}
}
},

/**
* Method: processLayer
* Recursive submethod of postProcessLayers. This function will among
* others deal with property inheritance.
*
* Parameters:
* capability - {Object} The capability part of the capabilities object
* layer - {Object} The layer that needs processing
* parentLayer - {Object} The parent layer of the respective layer
*/
processLayer: function(capability, layer, parentLayer) {
if (layer.formats === undefined) {
layer.formats = capability.request.getmap.formats;
}
if (layer.infoFormats === undefined && capability.request.getfeatureinfo) {
layer.infoFormats = capability.request.getfeatureinfo.formats;
}

var i, len;

// deal with property inheritance
if(parentLayer) {
// add style
layer.styles = layer.styles.concat(parentLayer.styles);
var attributes = ["queryable",
"cascaded",
"fixedWidth",
"fixedHeight",
"opaque",
"noSubsets",
"llbbox",
"minScale",
"maxScale",
"attribution"];

var complexAttr = ["srs",
"bbox",
"dimensions",
"authorityURLs"];

var key;

for (i=0, len=attributes.length; i<len; i++) {
key = attributes[i];
if (key in parentLayer) {
// only take parent value if not present (null or undefined)
if (layer[key] == null) {
layer[key] = parentLayer[key];
}
// if attribute isn't present, and we haven't
// inherited anything from a parent layer
// set to default value
if (layer[key] == null) {
var intAttr = ["cascaded", "fixedWidth", "fixedHeight"];
var boolAttr = ["queryable", "opaque", "noSubsets"];
if (OpenLayers.Util.indexOf(intAttr, key) != -1) {
layer[key] = 0;
}
if (OpenLayers.Util.indexOf(boolAttr, key) != -1) {
layer[key] = false;
}
}
}
}

for (i=0, len=complexAttr.length; i<len; i++) {
key = complexAttr[i];
layer[key] = OpenLayers.Util.applyDefaults(
layer[key], parentLayer[key]);
}
}

// process sublayers
for (i=0, len=layer.nestedLayers.length; i<len; i++) {
var childLayer = layer.nestedLayers[i];
this.processLayer(capability, childLayer, layer);
}

if (layer.name) {
capability.layers.push(layer);
}

},

/**
* Property: readers
* Contains public functions, grouped by namespace prefix, that will
Expand Down Expand Up @@ -290,7 +183,10 @@ OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class(
}
},
"Capability": function(node, obj) {
obj.capability = {nestedLayers: []};
obj.capability = {
nestedLayers: [],
layers: []
};
this.readChildNodes(node, obj.capability);
},
"Request": function(node, obj) {
Expand Down Expand Up @@ -343,6 +239,13 @@ OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class(
this.readChildNodes(node, obj.exception);
},
"Layer": function(node, obj) {
var parentLayer, capability;
if (obj.capability) {
capability = obj.capability;
parentLayer = obj;
} else {
capability = obj;
}
var attrNode = node.getAttributeNode("queryable");
var queryable = (attrNode && attrNode.specified) ?
node.getAttribute("queryable") : null;
Expand All @@ -355,28 +258,56 @@ OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class(
var noSubsets = node.getAttribute('noSubsets');
var fixedWidth = node.getAttribute('fixedWidth');
var fixedHeight = node.getAttribute('fixedHeight');
var layer = {nestedLayers: [], styles: [], srs: {},
metadataURLs: [], bbox: {}, dimensions: {},
authorityURLs: {}, identifiers: {}, keywords: [],
var parent = parentLayer || {},
extend = OpenLayers.Util.extend;
var layer = {
nestedLayers: [],
styles: parentLayer ? [].concat(parentLayer.styles) : [],
srs: parentLayer ? extend({}, parent.srs) : {},
metadataURLs: [],
bbox: parentLayer ? extend({}, parent.bbox) : {},
llbbox: parent.llbbox,
dimensions: parentLayer ? extend({}, parent.dimensions) : {},
authorityURLs: parentLayer ? extend({}, parent.authorityURLs) : {},
identifiers: {},
keywords: [],
queryable: (queryable && queryable !== "") ?
( queryable === "1" || queryable === "true" ) : null,
cascaded: (cascaded !== null) ? parseInt(cascaded) : null,
(queryable === "1" || queryable === "true" ) :
(parent.queryable || false),
cascaded: (cascaded !== null) ? parseInt(cascaded) :
(parent.cascaded || 0),
opaque: opaque ?
(opaque === "1" || opaque === "true" ) : null,
(opaque === "1" || opaque === "true" ) :
(parent.opaque || false),
noSubsets: (noSubsets !== null) ?
( noSubsets === "1" || noSubsets === "true" ) : null,
(noSubsets === "1" || noSubsets === "true" ) :
(parent.noSubsets || false),
fixedWidth: (fixedWidth != null) ?
parseInt(fixedWidth) : null,
parseInt(fixedWidth) : (parent.fixedWidth || 0),
fixedHeight: (fixedHeight != null) ?
parseInt(fixedHeight) : null
parseInt(fixedHeight) : (parent.fixedHeight || 0),
minScale: parent.minScale,
maxScale: parent.maxScale,
attribution: parent.attribution,
};
obj.nestedLayers.push(layer);
layer.capability = capability;
this.readChildNodes(node, layer);
delete layer.capability;
if(layer.name) {
var parts = layer.name.split(":");
var parts = layer.name.split(":"),
request = capability.request,
gfi = request.getfeatureinfo;
if(parts.length > 0) {
layer.prefix = parts[0];
}
capability.layers.push(layer);
if (layer.formats === undefined) {
layer.formats = request.getmap.formats;
}
if (layer.infoFormats === undefined && gfi) {
layer.infoFormats = gfi.formats;
}
}
},
"Attribution": function(node, obj) {
Expand Down

0 comments on commit 553eeb5

Please sign in to comment.