the old-but-still-used GML format creates features with geometries of type OpenLayers.Bounds, p=fvanderbiest, r=me (closes #2724)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10614 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2010-08-11 14:24:51 +00:00
parent 602cf46c10
commit 5a8775cd16
3 changed files with 64 additions and 12 deletions

View File

@@ -140,7 +140,9 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
// only accept one geometry per feature - look for highest "order" // only accept one geometry per feature - look for highest "order"
var order = ["MultiPolygon", "Polygon", var order = ["MultiPolygon", "Polygon",
"MultiLineString", "LineString", "MultiLineString", "LineString",
"MultiPoint", "Point", "Envelope", "Box"]; "MultiPoint", "Point", "Envelope"];
// FIXME: In case we parse a feature with no geometry, but boundedBy an Envelope,
// this code creates a geometry derived from the Envelope. This is not correct.
var type, nodeList, geometry, parser; var type, nodeList, geometry, parser;
for(var i=0; i<order.length; ++i) { for(var i=0; i<order.length; ++i) {
type = order[i]; type = order[i];
@@ -163,12 +165,28 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
} }
} }
var bounds;
var boxNodes = this.getElementsByTagNameNS(node, this.gmlns, "Box");
for(i=0; i<boxNodes.length; ++i) {
var boxNode = boxNodes[i];
var box = this.parseGeometry["box"].apply(this, [boxNode]);
var parentNode = boxNode.parentNode;
var parentName = parentNode.localName ||
parentNode.nodeName.split(":").pop();
if(parentName === "boundedBy") {
bounds = box;
} else {
geometry = box.toGeometry();
}
}
// construct feature (optionally with attributes) // construct feature (optionally with attributes)
var attributes; var attributes;
if(this.extractAttributes) { if(this.extractAttributes) {
attributes = this.parseAttributes(node); attributes = this.parseAttributes(node);
} }
var feature = new OpenLayers.Feature.Vector(geometry, attributes); var feature = new OpenLayers.Feature.Vector(geometry, attributes);
feature.bounds = bounds;
feature.gml = { feature.gml = {
featureType: node.firstChild.nodeName.split(":")[1], featureType: node.firstChild.nodeName.split(":")[1],
@@ -176,14 +194,6 @@ 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;

View File

@@ -270,8 +270,8 @@ OpenLayers.Format.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Format.XML, {
} }
var feature = this.gmlFormat.parseFeature(node); var feature = this.gmlFormat.parseFeature(node);
var geometry, bounds = null; var geometry, bounds = null;
if (feature && feature.geometry) { if (feature) {
geometry = feature.geometry.clone(); geometry = feature.geometry && feature.geometry.clone();
bounds = feature.bounds && feature.bounds.clone(); bounds = feature.bounds && feature.bounds.clone();
feature.destroy(); feature.destroy();
} }

View File

@@ -339,6 +339,48 @@
t.xml_eq(output, expect, "[xy true] Bounds correctly written as gml:Box"); t.xml_eq(output, expect, "[xy true] Bounds correctly written as gml:Box");
} }
function test_parseFeatureBox(t) {
t.plan(8);
var parser = new OpenLayers.Format.GML();
var xmlparser = new OpenLayers.Format.XML();
var data = xmlparser.read(test_box[0]);
var feature = parser.parseFeature(data);
t.ok(feature.bounds instanceof OpenLayers.Bounds,
"got bounds object for feature.bounds when with boundedBy Box, without geometry");
t.eq(feature.geometry, null, 'geometry is null for a feature with boundedBy Box, but no geometry');
var data = xmlparser.read(test_box[1]);
feature = parser.parseFeature(data);
t.eq(feature.bounds, null,
"feature is null when without boundedBy Box, with Box geometry");
t.ok(feature.geometry instanceof OpenLayers.Geometry.Polygon,
"got polygon object for feature.geometry when without boundedBy Box, with Box geometry");
data = xmlparser.read(test_box[2]);
feature = parser.parseFeature(data);
t.eq(feature.bounds, null,
"feature.bounds is null when without boundedBy Box, without geometry");
t.eq(feature.geometry, null, 'geometry is null when without boundedBy Box, without geometry');
data = xmlparser.read(test_box[3]);
feature = parser.parseFeature(data);
t.ok(feature.bounds instanceof OpenLayers.Bounds,
"got bounds object for feature.bounds when with boundedBy Box, with Box geometry");
t.ok(feature.geometry instanceof OpenLayers.Geometry.Polygon,
"got polygon object for feature.geometry when with boundedBy Box, with Box geometry");
}
var test_box = [
// with boundedBy Box, without geometry
'<Sentiers_littoraux_feature><gml:boundedBy xmlns:gml="http://www.opengis.net/gml"><gml:Box srsName="EPSG:2154"><gml:coordinates>143564.081753,6817901.121957 144209.641321,6819104.781451</gml:coordinates></gml:Box></gml:boundedBy><DEPARTEMENT>Finistère</DEPARTEMENT></Sentiers_littoraux_feature>',
// without boundedBy Box, with Box geometry
'<Sentiers_littoraux_feature><msGeometry><gml:Box srsName="EPSG:2154" xmlns:gml="http://www.opengis.net/gml"><gml:coordinates>143564.081753,6817901.121957 144209.641321,6819104.781451</gml:coordinates></gml:Box></msGeometry><DEPARTEMENT>Finistère</DEPARTEMENT></Sentiers_littoraux_feature>',
// without boundedBy, without geometry
'<Sentiers_littoraux_feature><DEPARTEMENT>Finistère</DEPARTEMENT></Sentiers_littoraux_feature>',
// with boundedBy Box, with Box geometry
'<Sentiers_littoraux_feature><msGeometry><gml:Box srsName="EPSG:2154" xmlns:gml="http://www.opengis.net/gml"><gml:coordinates>143564.081753,6817901.121957 144209.641321,6819104.781451</gml:coordinates></gml:Box></msGeometry><gml:boundedBy xmlns:gml="http://www.opengis.net/gml"><gml:Box srsName="EPSG:2154"><gml:coordinates>143564.081753,6817901.121957 144209.641321,6819104.781451</gml:coordinates></gml:Box></gml:boundedBy><DEPARTEMENT>Finistère</DEPARTEMENT></Sentiers_littoraux_feature>'];
var test_content = ['<?xml version="1.0" encoding="utf-8" ?>\n<ogr:FeatureCollection\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://ogr.maptools.org/ testoutput.xsd"\n xmlns:ogr="http://ogr.maptools.org/"\n xmlns:gml="http://www.opengis.net/gml">\n <gml:boundedBy>\n <gml:Box>\n <gml:coord><gml:X>-1254041.389711702</gml:X><gml:Y>250906.9515983529</gml:Y></gml:coord>\n <gml:coord><gml:X>-634517.1199908922</gml:X><gml:Y>762236.2940800377</gml:Y></gml:coord>\n </gml:Box>\n </gml:boundedBy> \n <gml:featureMember>\n <ogr:states fid="F0">\n <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-634517.11999089224,691849.77929356066,0 -653761.64509297756,471181.53429472551,0 -673343.60852865304,250906.9515983529,0 -1088825.734430399,299284.85108220269,0 -1254041.3897117018,324729.27754874947,0 -1235750.4212498858,434167.33911316615,0 -1190777.7803201093,704392.96327195223,0 -1181607.835811228,762236.29408003774,0 -634517.11999089224,691849.77929356066,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>\n <ogr:NAME>WY</ogr:NAME>\n <ogr:EMPTYATTR/><ogr:LONGNAME><![CDATA[Wyoming]]></ogr:LONGNAME>\n </ogr:states>\n </gml:featureMember>\n</ogr:FeatureCollection>\n', var test_content = ['<?xml version="1.0" encoding="utf-8" ?>\n<ogr:FeatureCollection\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://ogr.maptools.org/ testoutput.xsd"\n xmlns:ogr="http://ogr.maptools.org/"\n xmlns:gml="http://www.opengis.net/gml">\n <gml:boundedBy>\n <gml:Box>\n <gml:coord><gml:X>-1254041.389711702</gml:X><gml:Y>250906.9515983529</gml:Y></gml:coord>\n <gml:coord><gml:X>-634517.1199908922</gml:X><gml:Y>762236.2940800377</gml:Y></gml:coord>\n </gml:Box>\n </gml:boundedBy> \n <gml:featureMember>\n <ogr:states fid="F0">\n <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-634517.11999089224,691849.77929356066,0 -653761.64509297756,471181.53429472551,0 -673343.60852865304,250906.9515983529,0 -1088825.734430399,299284.85108220269,0 -1254041.3897117018,324729.27754874947,0 -1235750.4212498858,434167.33911316615,0 -1190777.7803201093,704392.96327195223,0 -1181607.835811228,762236.29408003774,0 -634517.11999089224,691849.77929356066,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>\n <ogr:NAME>WY</ogr:NAME>\n <ogr:EMPTYATTR/><ogr:LONGNAME><![CDATA[Wyoming]]></ogr:LONGNAME>\n </ogr:states>\n </gml:featureMember>\n</ogr:FeatureCollection>\n',
'<wfs:FeatureCollection' + '<wfs:FeatureCollection' +
' xmlns:fs="http://example.com/featureserver"' + ' xmlns:fs="http://example.com/featureserver"' +