Adding a format for parsing WMS capabilities docs. r=ahocevar (closes #1176)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9212 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-04-06 18:55:20 +00:00
parent 64b4752592
commit ebb7a08325
9 changed files with 5273 additions and 0 deletions

View File

@@ -241,6 +241,10 @@
"OpenLayers/Format/WMC/v1.js",
"OpenLayers/Format/WMC/v1_0_0.js",
"OpenLayers/Format/WMC/v1_1_0.js",
"OpenLayers/Format/WMSCapabilities.js",
"OpenLayers/Format/WMSCapabilities/v1_1.js",
"OpenLayers/Format/WMSCapabilities/v1_1_0.js",
"OpenLayers/Format/WMSCapabilities/v1_1_1.js",
"OpenLayers/Format/WMSGetFeatureInfo.js",
"OpenLayers/Layer/WFS.js",
"OpenLayers/Control/GetFeature.js",

View File

@@ -0,0 +1,77 @@
/**
* @requires OpenLayers/Format.js
*/
/**
* Class: OpenLayers.Format.WMSCapabilities
* Read WMS Capabilities.
*
* Inherits from:
* - <OpenLayers.Format>
*/
OpenLayers.Format.WMSCapabilities = OpenLayers.Class(OpenLayers.Format.XML, {
/**
* APIProperty: defaultVersion
* {String} Version number to assume if none found. Default is "1.1.1".
*/
defaultVersion: "1.1.1",
/**
* APIProperty: version
* {String} Specify a version string if one is known.
*/
version: null,
/**
* Property: parser
* {<OpenLayers.Format>} A cached versioned format used for reading.
*/
parser: null,
/**
* Constructor: OpenLayers.Format.WMSCapabilities
* Create a new parser for WMS capabilities.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.prototype.initialize.apply(this, [options]);
this.options = options;
},
/**
* APIMethod: read
* Read capabilities data from a string, and return a list of layers.
*
* Parameters:
* data - {String} or {DOMElement} data to read/parse.
*
* Returns:
* {Array} List of named layers.
*/
read: function(data) {
if(typeof data == "string") {
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
}
var root = data.documentElement;
var version = this.version || root.getAttribute("version") || this.defaultVersion;
if(!this.parser || this.parser.version !== version) {
var constr = OpenLayers.Format.WMSCapabilities[
"v" + version.replace(/\./g, "_")
];
if(!constr) {
throw "Can't find a WMS capabilities parser for version " + version;
}
var parser = new constr(this.options);
}
var capabilities = parser.read(data);
capabilities.version = version;
return capabilities;
},
CLASS_NAME: "OpenLayers.Format.WMSCapabilities"
});

View File

@@ -0,0 +1,266 @@
/**
* @requires OpenLayers/Format/WMSCapabilities.js
* @requires OpenLayers/Format/XML.js
*/
/**
* Class: OpenLayers.Format.WMSCapabilities.v1_1
* Abstract class not to be instantiated directly.
*
* Inherits from:
* - <OpenLayers.Format.XML>
*/
OpenLayers.Format.WMSCapabilities.v1_1 = OpenLayers.Class(
OpenLayers.Format.XML, {
/**
* Constructor: OpenLayers.Format.WMSCapabilities.v1_1
* Create an instance of one of the subclasses.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
this.options = options;
},
/**
* APIMethod: read
* Read capabilities data from a string, and return a list of layers.
*
* Parameters:
* data - {String} or {DOMElement} data to read/parse.
*
* Returns:
* {Array} List of named layers.
*/
read: function(data) {
if(typeof data == "string") {
data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
}
var capabilities = {};
var root = data.documentElement;
this.runChildNodes(capabilities, root);
return capabilities;
},
/**
* Method: runChildNodes
*/
runChildNodes: function(obj, node) {
var children = node.childNodes;
var childNode, processor;
for(var i=0; i<children.length; ++i) {
childNode = children[i];
if(childNode.nodeType == 1) {
processor = this["read_cap_" + childNode.nodeName];
if(processor) {
processor.apply(this, [obj, childNode]);
}
}
}
},
/**
* Method: read_cap_Capability
*/
read_cap_Capability: function(capabilities, node) {
var capability = {
layers: []
};
this.runChildNodes(capability, node);
capabilities.capability = capability;
},
/**
* Method: read_cap_Request
*/
read_cap_Request: function(obj, node) {
var request = {};
this.runChildNodes(request, node);
obj.request = request;
},
/**
* Method: read_cap_GetMap
*/
read_cap_GetMap: function(request, node) {
var getmap = {
formats: []
};
this.runChildNodes(getmap, node);
request.getmap = getmap;
},
/**
* Method: read_cap_Format
*/
read_cap_Format: function(obj, node) {
if(obj.formats) {
obj.formats.push(this.getChildValue(node));
}
},
/**
* Method: read_cap_DCPType
* Super simplified HTTP href extractor. Assumes the first online resource
* will work.
*/
read_cap_DCPType: function(obj, node) {
var children = node.getElementsByTagName("OnlineResource");
if(children.length > 0) {
this.read_cap_OnlineResource(obj, children[0]);
}
},
/**
* Method: read_cap_Service
*/
read_cap_Service: function(capabilities, node) {
var service = {};
this.runChildNodes(service, node);
capabilities.service = service;
},
/**
* Method: read_cap_Layer
*/
read_cap_Layer: function(capability, node, parentLayer) {
var layer = {
formats: capability.request.getmap.formats || [],
styles: [],
queryable: (node.getAttribute("queryable") === "1"
|| node.getAttribute("queryable") === "true")
};
// deal with property inheritance
if(parentLayer) {
// add style
layer.styles = layer.styles.concat(parentLayer.styles);
// use llbbox
layer.llbbox = parentLayer.llbbox;
// use min/maxScale
layer.minScale = parentLayer.minScale;
layer.maxScale = parentLayer.maxScale;
}
var children = node.childNodes;
var childNode, nodeName, processor;
for(var i=0; i<children.length; ++i) {
childNode = children[i];
nodeName = childNode.nodeName;
processor = this["read_cap_" + childNode.nodeName];
if(processor) {
if(nodeName == "Layer") {
processor.apply(this, [capability, childNode, layer]);
} else {
processor.apply(this, [layer, childNode]);
}
}
}
if(layer.name) {
var index = layer.name.indexOf(":");
if(index > 0) {
layer.prefix = layer.name.substring(0, index);
}
capability.layers.push(layer);
}
},
/**
* Method: read_cap_ScaleHint
* The Layer ScaleHint element has min and max attributes that relate to
* the minimum and maximum resolution that the server supports. The
* values are pixel diagonals measured in meters (on the ground).
*/
read_cap_ScaleHint: function(layer, node) {
var min = node.getAttribute("min");
var max = node.getAttribute("max");
var rad2 = Math.pow(2, 0.5);
var ipm = OpenLayers.INCHES_PER_UNIT["m"];
layer.maxScale = parseFloat(
((rad2 * min) * ipm * OpenLayers.DOTS_PER_INCH).toPrecision(13)
);
layer.minScale = parseFloat(
((rad2 * max) * ipm * OpenLayers.DOTS_PER_INCH).toPrecision(13)
);
},
/**
* Method: read_cap_Name
*/
read_cap_Name: function(obj, node) {
var name = this.getChildValue(node);
if(name) {
obj.name = name;
}
},
/**
* Method: read_cap_Title
*/
read_cap_Title: function(obj, node) {
var title = this.getChildValue(node);
if(title) {
obj.title = title;
}
},
/**
* Method: read_cap_Abstract
*/
read_cap_Abstract: function(obj, node) {
var abst = this.getChildValue(node);
if(abst) {
obj["abstract"] = abst;
}
},
/**
* Method: read_cap_LatLonBoundingBox
*/
read_cap_LatLonBoundingBox: function(layer, node) {
layer.llbbox = [
parseFloat(node.getAttribute("minx")),
parseFloat(node.getAttribute("miny")),
parseFloat(node.getAttribute("maxx")),
parseFloat(node.getAttribute("maxy"))
];
},
/**
* Method: read_cap_Style
*/
read_cap_Style: function(layer, node) {
var style = {};
this.runChildNodes(style, node);
layer.styles.push(style);
},
/**
* Method: read_cap_LegendURL
*/
read_cap_LegendURL: function(style, node) {
var legend = {
width: node.getAttribute('width'),
height: node.getAttribute('height')
};
var links = node.getElementsByTagName("OnlineResource");
if(links.length > 0) {
this.read_cap_OnlineResource(legend, links[0]);
}
style.legend = legend;
},
/**
* Method: read_cap_OnlineResource
*/
read_cap_OnlineResource: function(obj, node) {
obj.href = this.getAttributeNS(
node, "http://www.w3.org/1999/xlink", "href"
);
},
CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1_1"
});

View File

@@ -0,0 +1,37 @@
/**
* @requires OpenLayers/Format/WMSCapabilities/v1_1.js
*/
/**
* Class: OpenLayers.Format.WMSCapabilities/v1_1_0
* Read WMS Capabilities version 1.1.0.
*
* Inherits from:
* - <OpenLayers.Format.WMSCapabilities.v1_1>
*/
OpenLayers.Format.WMSCapabilities.v1_1_0 = OpenLayers.Class(
OpenLayers.Format.WMSCapabilities.v1_1, {
/**
* Property: version
* {String} The specific parser version.
*/
version: "1.1.0",
/**
* Constructor: OpenLayers.Format.WMSCapabilities.v1_1_0
* Create a new parser for WMS capabilities version 1.1.0.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.WMSCapabilities.v1_1.prototype.initialize.apply(
this, [options]
);
},
CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1_1_0"
});

View File

@@ -0,0 +1,37 @@
/**
* @requires OpenLayers/Format/WMSCapabilities/v1_1.js
*/
/**
* Class: OpenLayers.Format.WMSCapabilities/v1_1_1
* Read WMS Capabilities version 1.1.1.
*
* Inherits from:
* - <OpenLayers.Format.WMSCapabilities>
*/
OpenLayers.Format.WMSCapabilities.v1_1_1 = OpenLayers.Class(
OpenLayers.Format.WMSCapabilities.v1_1, {
/**
* Property: version
* {String} The specific parser version.
*/
version: "1.1.0",
/**
* Constructor: OpenLayers.Format.WMSCapabilities.v1_1_1
* Create a new parser for WMS capabilities version 1.1.1.
*
* Parameters:
* options - {Object} An optional object whose properties will be set on
* this instance.
*/
initialize: function(options) {
OpenLayers.Format.WMSCapabilities.v1_1.prototype.initialize.apply(
this, [options]
);
},
CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1_1_1"
});

View File

@@ -0,0 +1,20 @@
<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_initialize(t) {
t.plan(1);
var format = new OpenLayers.Format.WMSCapabilities({
version: "foo"
});
t.eq(format.version, "foo", "version set on format");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -0,0 +1,57 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script src="v1_1_1.js"></script>
<script type="text/javascript">
function test_read(t) {
t.plan(13);
var format = new OpenLayers.Format.WMSCapabilities();
var obj = format.read(doc);
var capability = obj.capability;
t.ok(capability, "object contains capability property");
var getmap = capability.request.getmap;
t.eq(getmap.formats.length, 28, "getmap formats parsed");
t.eq(
getmap.href,
"http://publicus.opengeo.org:80/geoserver/wms?SERVICE=WMS&",
"getmap href parsed"
);
t.ok(capability.layers, "layers parsed");
t.eq(capability.layers.length, 22, "correct number of layers parsed");
var layer = capability.layers[2];
t.eq(layer.name, "tiger:tiger_roads", "[2] correct layer name");
t.eq(layer.title, "Manhattan (NY) roads", "[2] correct layer title");
t.eq(
layer["abstract"],
"Highly simplified road layout of Manhattan in New York..",
"[2] correct layer abstract"
);
t.eq(
layer.llbbox,
[-74.08769307536667, 40.660618924633326, -73.84653192463333, 40.90178007536667],
"[2] correct layer bbox"
);
t.eq(layer.styles.length, 1, "[2] correct styles length");
t.eq(layer.styles[0].name, "tiger_roads", "[2] correct style name");
t.eq(
layer.styles[0].legend.href,
"http://publicus.opengeo.org:80/geoserver/wms/GetLegendGraphic?VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=tiger:tiger_roads",
"[2] correct legend url"
);
t.eq(layer.queryable, true, "[2] correct queryable attribute");
}
</script>
</head>
<body>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@@ -70,6 +70,8 @@
<li>Format/WMC.html</li>
<li>Format/WMC/v1_1_0.html</li>
<li>Format/WMC/v1.html</li>
<li>Format/WMSCapabilities.html</li>
<li>Format/WMSCapabilities/v1_1_1.html</li>
<li>Format/WMSGetFeatureInfo.html</li>
<li>Format/XML.html</li>
<li>Geometry.html</li>