Add preliminary read support for features
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -304,6 +304,12 @@
|
|||||||
* default style is the same as Google Earth.
|
* default style is the same as Google Earth.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} olx.format.GMLOptions
|
||||||
|
* @property {string} featureNS Feature namespace.
|
||||||
|
* @property {string} featureType Feature type to parse.
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} olx.interaction.DoubleClickZoomOptions
|
* @typedef {Object} olx.interaction.DoubleClickZoomOptions
|
||||||
* @property {number|undefined} duration Animation duration in milliseconds. Default is `250`.
|
* @property {number|undefined} duration Animation duration in milliseconds. Default is `250`.
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ goog.require('goog.asserts');
|
|||||||
goog.require('goog.dom');
|
goog.require('goog.dom');
|
||||||
goog.require('goog.dom.NodeType');
|
goog.require('goog.dom.NodeType');
|
||||||
goog.require('goog.dom.TagName');
|
goog.require('goog.dom.TagName');
|
||||||
|
goog.require('ol.Feature');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
goog.require('ol.format.XML');
|
goog.require('ol.format.XML');
|
||||||
goog.require('ol.geom.GeometryCollection');
|
goog.require('ol.geom.GeometryCollection');
|
||||||
@@ -20,10 +21,16 @@ goog.require('ol.xml');
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @param {olx.format.GMLOptions=} opt_options
|
||||||
|
* Optional configuration object.
|
||||||
* @extends {ol.format.XML}
|
* @extends {ol.format.XML}
|
||||||
* @todo stability experimental
|
* @todo stability experimental
|
||||||
*/
|
*/
|
||||||
ol.format.GML = function() {
|
ol.format.GML = function(opt_options) {
|
||||||
|
var options = /** @type {olx.format.GMLOptions} */
|
||||||
|
(goog.isDef(opt_options) ? opt_options : {});
|
||||||
|
this.featureType_ = options.featureType;
|
||||||
|
this.featureNS_ = options.featureNS;
|
||||||
goog.base(this);
|
goog.base(this);
|
||||||
};
|
};
|
||||||
goog.inherits(ol.format.GML, ol.format.XML);
|
goog.inherits(ol.format.GML, ol.format.XML);
|
||||||
@@ -39,6 +46,45 @@ ol.format.GML.NAMESPACE_URIS_ = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
ol.format.GML.prototype.readFeaturesFromNode = function(node) {
|
||||||
|
if (goog.array.indexOf(ol.format.GML.NAMESPACE_URIS_,
|
||||||
|
this.featureNS_) === -1) {
|
||||||
|
ol.format.GML.NAMESPACE_URIS_.push(this.featureNS_);
|
||||||
|
}
|
||||||
|
goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT);
|
||||||
|
if (goog.array.indexOf(ol.format.GML.NAMESPACE_URIS_, node.namespaceURI) ==
|
||||||
|
-1) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
var features;
|
||||||
|
var localName = ol.xml.getLocalName(node);
|
||||||
|
if (localName === this.featureType_) {
|
||||||
|
var feature = this.readFeature_(node, []);
|
||||||
|
if (goog.isDef(feature)) {
|
||||||
|
return [feature];
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
} else if (localName == 'featureMembers') {
|
||||||
|
features = [];
|
||||||
|
var n;
|
||||||
|
for (n = node.firstElementChild; !goog.isNull(n);
|
||||||
|
n = n.nextElementSibling) {
|
||||||
|
var fs = this.readFeaturesFromNode(n);
|
||||||
|
if (goog.isDef(fs)) {
|
||||||
|
goog.array.extend(features, fs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return features;
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
@@ -73,6 +119,33 @@ ol.format.GML.prototype.readGeometryFromNode = function(node) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Node} node Node.
|
||||||
|
* @param {Array.<*>} objectStack Object stack.
|
||||||
|
* @private
|
||||||
|
* @return {ol.Feature} Feature.
|
||||||
|
*/
|
||||||
|
ol.format.GML.prototype.readFeature_ = function(node, objectStack) {
|
||||||
|
var n;
|
||||||
|
var values = {};
|
||||||
|
for (n = node.firstElementChild; !goog.isNull(n);
|
||||||
|
n = n.nextElementSibling) {
|
||||||
|
// Assume attribute elements have one child node and that the child
|
||||||
|
// is a text node. Otherwise assume it is a geometry node.
|
||||||
|
if (n.childNodes.length === 0 ||
|
||||||
|
(n.childNodes.length === 1 &&
|
||||||
|
n.firstChild.nodeType === 3)) {
|
||||||
|
values[ol.xml.getLocalName(n)] = ol.xml.getAllTextContent(n, false);
|
||||||
|
} else {
|
||||||
|
values[ol.xml.getLocalName(n)] = this.readGeometryFromNode(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var feature = new ol.Feature();
|
||||||
|
feature.setValues(values);
|
||||||
|
return feature;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Node} node Node.
|
* @param {Node} node Node.
|
||||||
* @param {Array.<*>} objectStack Object stack.
|
* @param {Array.<*>} objectStack Object stack.
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -543,6 +543,30 @@ describe('ol.format.GML', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when parsing TOPP states GML', function() {
|
||||||
|
|
||||||
|
var features;
|
||||||
|
before(function(done) {
|
||||||
|
afterLoadText('spec/ol/format/gml/topp-states-gml.xml', function(xml) {
|
||||||
|
try {
|
||||||
|
var config = {
|
||||||
|
'featureNS': 'http://www.openplans.org/topp',
|
||||||
|
'featureType': 'states'
|
||||||
|
};
|
||||||
|
features = new ol.format.GML(config).readFeatures(xml);
|
||||||
|
} catch (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates 10 features', function() {
|
||||||
|
expect(features).to.have.length(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user