diff --git a/lib/OpenLayers/Format/GML.js b/lib/OpenLayers/Format/GML.js index e6913767f9..e454c848ea 100644 --- a/lib/OpenLayers/Format/GML.js +++ b/lib/OpenLayers/Format/GML.js @@ -147,7 +147,7 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, { nodeList = this.getElementsByTagNameNS(node, this.gmlns, type); if(nodeList.length > 0) { // only deal with first geometry of this type - var parser = this.parseGeometry[type.toLowerCase()]; + parser = this.parseGeometry[type.toLowerCase()]; if(parser) { geometry = parser.apply(this, [nodeList[0]]); if (this.internalProjection && this.externalProjection) { @@ -176,6 +176,14 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, { featureNSPrefix: node.firstChild.prefix }; + var boundedByNodes = this.getElementsByTagNameNS(node, this.gmlns, 'boundedBy'); + if (boundedByNodes.length === 1) { + parser = this.parseGeometry['box']; + if (parser) { + feature.bounds = parser.apply(this, [boundedByNodes[0]]); + } + } + // assign fid - this can come from a "fid" or "id" attribute var childNode = node.firstChild; var fid; @@ -522,7 +530,40 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, { envelope = new OpenLayers.Geometry.Polygon([ring]); } return envelope; + }, + + /** + * Method: parseGeometry.box + * Given a GML node representing a box geometry, create an + * OpenLayers.Bounds. + * + * Parameters: + * node - {DOMElement} A GML node. + * + * Returns: + * {} A bounds representing the box. + */ + box: function(node) { + var nodeList = this.getElementsByTagNameNS(node, this.gmlns, + "coordinates"); + var coordString; + var coords, beginPoint = null, endPoint = null; + if (nodeList.length > 0) { + coordString = nodeList[0].firstChild.nodeValue; + coords = coordString.split(" "); + if (coords.length == 2) { + beginPoint = coords[0].split(","); + endPoint = coords[1].split(","); + } + } + if (beginPoint !== null && endPoint !== null) { + return new OpenLayers.Bounds(parseFloat(beginPoint[0]), + parseFloat(beginPoint[1]), + parseFloat(endPoint[0]), + parseFloat(endPoint[1]) ); + } } + }, /** diff --git a/lib/OpenLayers/Format/WMSGetFeatureInfo.js b/lib/OpenLayers/Format/WMSGetFeatureInfo.js index 9d4589f3eb..4e1da1ce2a 100644 --- a/lib/OpenLayers/Format/WMSGetFeatureInfo.js +++ b/lib/OpenLayers/Format/WMSGetFeatureInfo.js @@ -121,10 +121,11 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, { if (featureNodes) { for (var j = 0; j < featureNodes.length; j++) { var featureNode = featureNodes[j]; - var geom = this.parseGeometry(featureNode); + var geomInfo = this.parseGeometry(featureNode); var attributes = this.parseAttributes(featureNode); - var feature = new OpenLayers.Feature.Vector(geom, + var feature = new OpenLayers.Feature.Vector(geomInfo.geometry, attributes, null); + feature.bounds = geomInfo.bounds; feature.type = layerName; response.push(feature); } @@ -252,13 +253,14 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, { /** * Method: parseGeometry - * Parse the geometry out of the node using Format.GML + * Parse the geometry and the feature bounds out of the node using + * Format.GML * * Parameters: * node - {} * * Returns: - * {} the geometry object + * {Object} An object containing the geometry and the feature bounds */ parseGeometry: function(node) { // we need to use the old Format.GML parser since we do not know the @@ -267,12 +269,13 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, { this.gmlFormat = new OpenLayers.Format.GML(); } var feature = this.gmlFormat.parseFeature(node); - var geometry = null; + var geometry, bounds = null; if (feature && feature.geometry) { geometry = feature.geometry.clone(); + bounds = feature.bounds && feature.bounds.clone(); feature.destroy(); } - return geometry; + return {geometry: geometry, bounds: bounds}; }, CLASS_NAME: "OpenLayers.Format.WMSGetFeatureInfo" diff --git a/tests/Format/WMSGetFeatureInfo.html b/tests/Format/WMSGetFeatureInfo.html index 07ea1716b3..41a906672e 100644 --- a/tests/Format/WMSGetFeatureInfo.html +++ b/tests/Format/WMSGetFeatureInfo.html @@ -52,7 +52,7 @@ } function test_read_msGMLOutput(t) { - t.plan(7); + t.plan(12); var parser = new OpenLayers.Format.WMSGetFeatureInfo(); @@ -96,6 +96,7 @@ ''; features = parser.read(text); + t.eq(features.length, 1, "Parsed 1 feature in total"); @@ -105,6 +106,13 @@ t.eq(features[0].type, 'AAA64', "Parsed the layer name correctly"); + var bounds = features[0].bounds; + t.ok(bounds instanceof OpenLayers.Bounds, "feature given a bounds"); + t.eq(bounds.left.toFixed(3), "107397.266", "Bounds left parsed correctly"); + t.eq(bounds.right.toFixed(3), "116568.188", "Bounds right parsed correctly"); + t.eq(bounds.bottom.toFixed(3), "460681.063", "Bounds bottom parsed correctly"); + t.eq(bounds.top.toFixed(3), "480609.250", "Bounds top parsed correctly"); + // read 2 features from 2 layers text = '' + @@ -263,7 +271,6 @@ } -