Auto configure ol.format.GML if not configured with a featureNS/featureType

This commit is contained in:
Bart van den Eijnden
2014-09-11 19:04:12 +02:00
parent 593532cb5a
commit 19319356d7
2 changed files with 61 additions and 3 deletions

View File

@@ -119,14 +119,20 @@ ol.format.GML.schemaLocation_ = 'http://www.opengis.net/gml ' +
ol.format.GML.readFeatures_ = function(node, objectStack) {
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
var localName = ol.xml.getLocalName(node);
var context = objectStack[0];
goog.asserts.assert(goog.isObject(context));
var featureType = goog.object.get(context, 'featureType');
var features;
if (localName == 'FeatureCollection') {
features = ol.xml.pushParseAndPop(null,
ol.format.GML.FEATURE_COLLECTION_PARSERS, node, objectStack);
} else if (localName == 'featureMembers' || localName == 'featureMember') {
var context = objectStack[0];
goog.asserts.assert(goog.isObject(context));
var featureType = goog.object.get(context, 'featureType');
if (!goog.isDef(featureType) && !goog.isNull(node.firstElementChild)) {
var member = node.firstElementChild;
featureType = member.nodeName.split(':').pop();
goog.object.set(context, 'featureType', featureType);
goog.object.set(context, 'featureNS', member.namespaceURI);
}
var parsers = {};
var parsersNS = {};
parsers[featureType] = (localName == 'featureMembers') ?

View File

@@ -768,6 +768,58 @@ describe('ol.format.GML', function() {
});
});
describe('when parsing TOPP states WFS with autoconfigure', function() {
var features, text, gmlFormat;
before(function(done) {
afterLoadText('spec/ol/format/gml/topp-states-wfs.xml', function(xml) {
try {
text = xml;
gmlFormat = new ol.format.GML();
features = gmlFormat.readFeatures(xml);
} catch (e) {
done(e);
}
done();
});
});
it('creates 3 features', function() {
expect(features).to.have.length(3);
});
it('creates the right id for the feature', function() {
expect(features[0].getId()).to.equal('states.1');
});
it('can reuse the parser for a different featureNS', function() {
var text =
'<gml:featureMembers xmlns:gml="http://www.opengis.net/gml">' +
' <foo:gnis_pop gml:id="gnis_pop.148604" xmlns:foo="' +
'http://foo">' +
' <gml:name>Aflu</gml:name>' +
' <foo:the_geom>' +
' <gml:Point srsName="urn:x-ogc:def:crs:EPSG:4326">' +
' <gml:pos>34.12 2.09</gml:pos>' +
' </gml:Point>' +
' </foo:the_geom>' +
' <foo:population>84683</foo:population>' +
' </foo:gnis_pop>' +
'</gml:featureMembers>';
features = gmlFormat.readFeatures(text);
expect(features).to.have.length(1);
expect(features[0].get('population')).to.equal('84683');
});
it('can read an empty collection', function() {
var text =
'<gml:featureMembers xmlns:gml="http://www.opengis.net/gml">' +
'</gml:featureMembers>';
features = gmlFormat.readFeatures(text);
expect(features).to.have.length(0);
});
});
describe('when parsing TOPP states GML', function() {
var features, text, gmlFormat;