add parsing of boundedBy in Format.WMSGetFeatureInfo, r=elemoine (closes #2329)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9800 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
bartvde
2009-11-16 10:44:02 +00:00
parent ebc5d7c021
commit 10920f6f93
3 changed files with 60 additions and 9 deletions
+42 -1
View File
@@ -147,7 +147,7 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
nodeList = this.getElementsByTagNameNS(node, this.gmlns, type); nodeList = this.getElementsByTagNameNS(node, this.gmlns, type);
if(nodeList.length > 0) { if(nodeList.length > 0) {
// only deal with first geometry of this type // only deal with first geometry of this type
var parser = this.parseGeometry[type.toLowerCase()]; parser = this.parseGeometry[type.toLowerCase()];
if(parser) { if(parser) {
geometry = parser.apply(this, [nodeList[0]]); geometry = parser.apply(this, [nodeList[0]]);
if (this.internalProjection && this.externalProjection) { if (this.internalProjection && this.externalProjection) {
@@ -176,6 +176,14 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
featureNSPrefix: node.firstChild.prefix 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 // assign fid - this can come from a "fid" or "id" attribute
var childNode = node.firstChild; var childNode = node.firstChild;
var fid; var fid;
@@ -522,7 +530,40 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
envelope = new OpenLayers.Geometry.Polygon([ring]); envelope = new OpenLayers.Geometry.Polygon([ring]);
} }
return envelope; return envelope;
},
/**
* Method: parseGeometry.box
* Given a GML node representing a box geometry, create an
* OpenLayers.Bounds.
*
* Parameters:
* node - {DOMElement} A GML node.
*
* Returns:
* {<OpenLayers.Bounds>} 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]) );
}
} }
}, },
/** /**
+9 -6
View File
@@ -121,10 +121,11 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, {
if (featureNodes) { if (featureNodes) {
for (var j = 0; j < featureNodes.length; j++) { for (var j = 0; j < featureNodes.length; j++) {
var featureNode = featureNodes[j]; var featureNode = featureNodes[j];
var geom = this.parseGeometry(featureNode); var geomInfo = this.parseGeometry(featureNode);
var attributes = this.parseAttributes(featureNode); var attributes = this.parseAttributes(featureNode);
var feature = new OpenLayers.Feature.Vector(geom, var feature = new OpenLayers.Feature.Vector(geomInfo.geometry,
attributes, null); attributes, null);
feature.bounds = geomInfo.bounds;
feature.type = layerName; feature.type = layerName;
response.push(feature); response.push(feature);
} }
@@ -252,13 +253,14 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, {
/** /**
* Method: parseGeometry * 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: * Parameters:
* node - {<DOMElement>} * node - {<DOMElement>}
* *
* Returns: * Returns:
* {<OpenLayers.Geometry>} the geometry object * {Object} An object containing the geometry and the feature bounds
*/ */
parseGeometry: function(node) { parseGeometry: function(node) {
// we need to use the old Format.GML parser since we do not know the // 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(); this.gmlFormat = new OpenLayers.Format.GML();
} }
var feature = this.gmlFormat.parseFeature(node); var feature = this.gmlFormat.parseFeature(node);
var geometry = null; var geometry, bounds = null;
if (feature && feature.geometry) { if (feature && feature.geometry) {
geometry = feature.geometry.clone(); geometry = feature.geometry.clone();
bounds = feature.bounds && feature.bounds.clone();
feature.destroy(); feature.destroy();
} }
return geometry; return {geometry: geometry, bounds: bounds};
}, },
CLASS_NAME: "OpenLayers.Format.WMSGetFeatureInfo" CLASS_NAME: "OpenLayers.Format.WMSGetFeatureInfo"
+9 -2
View File
@@ -52,7 +52,7 @@
} }
function test_read_msGMLOutput(t) { function test_read_msGMLOutput(t) {
t.plan(7); t.plan(12);
var parser = new OpenLayers.Format.WMSGetFeatureInfo(); var parser = new OpenLayers.Format.WMSGetFeatureInfo();
@@ -96,6 +96,7 @@
'</msGMLOutput>'; '</msGMLOutput>';
features = parser.read(text); features = parser.read(text);
t.eq(features.length, 1, t.eq(features.length, 1,
"Parsed 1 feature in total"); "Parsed 1 feature in total");
@@ -105,6 +106,13 @@
t.eq(features[0].type, 'AAA64', t.eq(features[0].type, 'AAA64',
"Parsed the layer name correctly"); "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 // read 2 features from 2 layers
text = text =
'<?xml version="1.0" encoding="ISO-8859-1"?>' + '<?xml version="1.0" encoding="ISO-8859-1"?>' +
@@ -263,7 +271,6 @@
} }
</script> </script>
</head> </head>
<body> <body>