Finish write support for features

This commit is contained in:
Bart van den Eijnden
2014-03-03 14:01:07 +01:00
parent 8dce3249d6
commit 9b4fc06a5b
3 changed files with 53 additions and 31 deletions

View File

@@ -321,6 +321,8 @@
* @property {boolean|undefined} multiSurface Write gml:multiSurface instead * @property {boolean|undefined} multiSurface Write gml:multiSurface instead
* of gml:MultiPolygon. Since the latter is deprecated in GML 3, the * of gml:MultiPolygon. Since the latter is deprecated in GML 3, the
* default is `true´. * default is `true´.
* @property {string|undefined} schemaLocation Optional schemaLocation to use
* when writing out the GML, this will override the default provided.
*/ */
/** /**

View File

@@ -80,6 +80,15 @@ ol.format.GML = function(opt_options) {
this.multiSurface_ = goog.isDef(options.multiSurface) ? this.multiSurface_ = goog.isDef(options.multiSurface) ?
options.multiSurface : true; options.multiSurface : true;
/**
* @private
* @type {string}
*/
this.schemaLocation_ = goog.isDef(options.schemaLocation) ?
options.schemaLocation : ('http://www.opengis.net/gml ' +
'http://schemas.opengis.net/gml/3.1.1/profiles/gmlsfProfile/' +
'1.0.0/gmlsf.xsd');
goog.base(this); goog.base(this);
}; };
goog.inherits(ol.format.GML, ol.format.XMLFeature); goog.inherits(ol.format.GML, ol.format.XMLFeature);
@@ -1472,8 +1481,12 @@ ol.format.GML.writeCurveSegments_ = function(node, line, objectStack) {
* @private * @private
*/ */
ol.format.GML.writeGeometry_ = function(node, geometry, objectStack) { ol.format.GML.writeGeometry_ = function(node, geometry, objectStack) {
var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context));
var item = goog.object.clone(context);
goog.object.set(item, 'node', node);
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
({node: node}), ol.format.GML.GEOMETRY_SERIALIZERS_, (item), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []); ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []);
}; };
@@ -1485,6 +1498,10 @@ ol.format.GML.writeGeometry_ = function(node, geometry, objectStack) {
* @private * @private
*/ */
ol.format.GML.writeFeature_ = function(node, feature, objectStack) { ol.format.GML.writeFeature_ = function(node, feature, objectStack) {
var fid = feature.getId();
if (goog.isDef(fid)) {
node.setAttribute('fid', fid);
}
var context = objectStack[objectStack.length - 1]; var context = objectStack[objectStack.length - 1];
goog.asserts.assert(goog.isObject(context)); goog.asserts.assert(goog.isObject(context));
var featureNS = goog.object.get(context, 'featureNS'); var featureNS = goog.object.get(context, 'featureNS');
@@ -1504,8 +1521,10 @@ ol.format.GML.writeFeature_ = function(node, feature, objectStack) {
ol.format.XSD.writeStringTextNode); ol.format.XSD.writeStringTextNode);
} }
} }
var item = goog.object.clone(context);
goog.object.set(item, 'node', node);
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
({node: node}), serializers, (item), serializers,
ol.xml.OBJECT_PROPERTY_NODE_FACTORY, ol.xml.OBJECT_PROPERTY_NODE_FACTORY,
values, values,
objectStack, keys); objectStack, keys);
@@ -1523,13 +1542,14 @@ ol.format.GML.writeFeatureMembers_ = function(node, features, objectStack) {
goog.asserts.assert(goog.isObject(context)); goog.asserts.assert(goog.isObject(context));
var featureType = goog.object.get(context, 'featureType'); var featureType = goog.object.get(context, 'featureType');
var featureNS = goog.object.get(context, 'featureNS'); var featureNS = goog.object.get(context, 'featureNS');
//featureNS = 'http://www.opengis.net/gml';
var serializers = {}; var serializers = {};
serializers[featureNS] = {}; serializers[featureNS] = {};
serializers[featureNS][featureType] = ol.xml.makeChildAppender( serializers[featureNS][featureType] = ol.xml.makeChildAppender(
ol.format.GML.writeFeature_); ol.format.GML.writeFeature_);
var item = goog.object.clone(context);
goog.object.set(item, 'node', node);
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
({node: node, featureType: featureType, featureNS: featureNS}), (item),
serializers, serializers,
ol.xml.makeSimpleNodeFactory(featureType, featureNS), features, []); ol.xml.makeSimpleNodeFactory(featureType, featureNS), features, []);
}; };
@@ -1653,18 +1673,6 @@ ol.format.GML.GEOMETRY_SERIALIZERS_ = {
}; };
/**
* @type {Object.<string, Object.<string, ol.xml.Serializer>>}
* @private
*/
ol.format.GML.GML_SERIALIZERS_ = {
'http://www.opengis.net/gml': {
'featureMembers': ol.xml.makeChildAppender(
ol.format.GML.writeFeatureMembers_)
}
};
/** /**
* @const * @const
* @param {*} value Value. * @param {*} value Value.
@@ -1718,10 +1726,12 @@ ol.format.GML.prototype.writeGeometryNode = function(geometry) {
* @inheritDoc * @inheritDoc
*/ */
ol.format.GML.prototype.writeFeaturesNode = function(features) { ol.format.GML.prototype.writeFeaturesNode = function(features) {
var collection = ol.xml.createElementNS('http://www.opengis.net/wfs', var node = ol.xml.createElementNS('http://www.opengis.net/gml',
'FeatureCollection'); 'featureMembers');
ol.xml.setAttributeNS(node, 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation', this.schemaLocation_);
var context = { var context = {
node: collection, node: node,
srsName: this.srsName_, srsName: this.srsName_,
curve: this.curve_, curve: this.curve_,
surface: this.surface_, surface: this.surface_,
@@ -1730,9 +1740,6 @@ ol.format.GML.prototype.writeFeaturesNode = function(features) {
featureNS: this.featureNS_, featureNS: this.featureNS_,
featureType: this.featureType_ featureType: this.featureType_
}; };
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ ol.format.GML.writeFeatureMembers_(node, features, [context]);
(context), ol.format.GML.GML_SERIALIZERS_, return node;
ol.xml.makeSimpleNodeFactory('featureMembers',
'http://www.opengis.net/gml'), [features], []);
return collection;
}; };

View File

@@ -678,15 +678,25 @@ describe('ol.format.GML', function() {
describe('when parsing TOPP states GML', function() { describe('when parsing TOPP states GML', function() {
var features; var features, text, gmlFormat;
before(function(done) { before(function(done) {
afterLoadText('spec/ol/format/gml/topp-states-gml.xml', function(xml) { afterLoadText('spec/ol/format/gml/topp-states-gml.xml', function(xml) {
try { try {
var schemaLoc = 'http://www.openplans.org/topp ' +
'http://demo.opengeo.org/geoserver/wfs?service=WFS&version=' +
'1.1.0&request=DescribeFeatureType&typeName=topp:states ' +
'http://www.opengis.net/gml ' +
'http://schemas.opengis.net/gml/3.2.1/gml.xsd';
var config = { var config = {
'featureNS': 'http://www.openplans.org/topp', 'featureNS': 'http://www.openplans.org/topp',
'featureType': 'states' 'featureType': 'states',
'multiSurface': true,
'srsName': 'urn:x-ogc:def:crs:EPSG:4326',
'schemaLocation': schemaLoc
}; };
features = new ol.format.GML(config).readFeatures(xml); text = xml;
gmlFormat = new ol.format.GML(config);
features = gmlFormat.readFeatures(xml);
} catch (e) { } catch (e) {
done(e); done(e);
} }
@@ -702,11 +712,16 @@ describe('ol.format.GML', function() {
expect(features[0].getId()).to.equal('states.1'); expect(features[0].getId()).to.equal('states.1');
}); });
it('writes back features as GML', function() {
var serialized = gmlFormat.writeFeatures(features);
expect(serialized).to.xmleql(ol.xml.load(text));
});
}); });
describe('when parsing TOPP states GML from WFS', function() { describe('when parsing TOPP states GML from WFS', function() {
var features, feature, text, wfsFormat; var features, feature;
before(function(done) { before(function(done) {
afterLoadText('spec/ol/format/gml/topp-states-wfs.xml', function(xml) { afterLoadText('spec/ol/format/gml/topp-states-wfs.xml', function(xml) {
try { try {
@@ -714,9 +729,7 @@ describe('ol.format.GML', function() {
'featureNS': 'http://www.openplans.org/topp', 'featureNS': 'http://www.openplans.org/topp',
'featureType': 'states' 'featureType': 'states'
}; };
text = xml; features = new ol.format.GML(config).readFeatures(xml);
wfsFormat = new ol.format.GML(config);
features = wfsFormat.readFeatures(xml);
} catch (e) { } catch (e) {
done(e); done(e);
} }