Implement ReadOptions and WriteOptions for ol.format.GML

This commit is contained in:
Andreas Hocevar
2014-07-30 16:13:53 +02:00
parent da0988f5f4
commit 4825cba48a
2 changed files with 87 additions and 18 deletions

View File

@@ -1,3 +1,5 @@
//FIXME Implement readProjectionFrom*
goog.provide('ol.format.GML');
goog.require('goog.asserts');
@@ -8,6 +10,7 @@ goog.require('goog.string');
goog.require('ol.Feature');
goog.require('ol.array');
goog.require('ol.extent');
goog.require('ol.format.Feature');
goog.require('ol.format.XMLFeature');
goog.require('ol.format.XSD');
goog.require('ol.geom.Geometry');
@@ -161,7 +164,7 @@ ol.format.GML.readGeometry = function(node, objectStack) {
var geometry = ol.xml.pushParseAndPop(/** @type {ol.geom.Geometry} */(null),
ol.format.GML.GEOMETRY_PARSERS_, node, objectStack);
if (goog.isDefAndNotNull(geometry)) {
return geometry;
return ol.format.Feature.transformGeometry(geometry, false, context);
} else {
return undefined;
}
@@ -1036,9 +1039,14 @@ ol.format.GML.RING_PARSERS_ = {
/**
* @inheritDoc
*/
ol.format.GML.prototype.readGeometryFromNode = function(node) {
var geometry = ol.format.GML.readGeometry(node, [{}]);
return (goog.isDef(geometry)) ? geometry : null;
ol.format.GML.prototype.readGeometryFromNode = function(node, opt_options) {
var obj = {};
if (goog.isDef(opt_options)) {
goog.object.extend(obj, opt_options);
//FIXME Get dataProjection from data
}
var geometry = ol.format.GML.readGeometry(node, [obj]);
return (goog.isDef(geometry) ? geometry : null);
};
@@ -1047,6 +1055,7 @@ ol.format.GML.prototype.readGeometryFromNode = function(node) {
*
* @function
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @param {olx.format.ReadOptions=} opt_options Options.
* @return {Array.<ol.Feature>} Features.
* @api
*/
@@ -1056,12 +1065,16 @@ ol.format.GML.prototype.readFeatures;
/**
* @inheritDoc
*/
ol.format.GML.prototype.readFeaturesFromNode = function(node) {
var objectStack = [{
ol.format.GML.prototype.readFeaturesFromNode = function(node, opt_options) {
var options = {
'featureType': this.featureType_,
'featureNS': this.featureNS_
}];
return ol.format.GML.readFeatures_(node, objectStack);
};
if (goog.isDef(opt_options)) {
goog.object.extend(options, opt_options);
//FIXME Get dataProjection from data
}
return ol.format.GML.readFeatures_(node, [options]);
};
@@ -1444,7 +1457,9 @@ ol.format.GML.writeGeometry = function(node, geometry, objectStack) {
item.node = node;
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(item), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], objectStack);
ol.format.GML.GEOMETRY_NODE_FACTORY_,
[ol.format.Feature.transformGeometry(geometry, true, context)],
objectStack);
};
@@ -1672,14 +1687,15 @@ ol.format.GML.GEOMETRY_NODE_FACTORY_ = function(value, objectStack,
/**
* @inheritDoc
*/
ol.format.GML.prototype.writeGeometryNode = function(geometry) {
ol.format.GML.prototype.writeGeometryNode = function(geometry, opt_options) {
var geom = ol.xml.createElementNS('http://www.opengis.net/gml', 'geom');
var context = {node: geom, srsName: this.srsName_,
curve: this.curve_, surface: this.surface_,
multiSurface: this.multiSurface_, multiCurve: this.multiCurve_};
ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */
(context), ol.format.GML.GEOMETRY_SERIALIZERS_,
ol.format.GML.GEOMETRY_NODE_FACTORY_, [geometry], []);
if (goog.isDef(opt_options)) {
goog.object.extend(context, opt_options);
}
ol.format.GML.writeGeometry(geom, geometry, [context]);
return geom;
};
@@ -1689,6 +1705,7 @@ ol.format.GML.prototype.writeGeometryNode = function(geometry) {
*
* @function
* @param {Array.<ol.Feature>} features Features.
* @param {olx.format.WriteOptions=} opt_options Options.
* @return {Node} Result.
* @api
*/
@@ -1698,7 +1715,7 @@ ol.format.GML.prototype.writeFeatures;
/**
* @inheritDoc
*/
ol.format.GML.prototype.writeFeaturesNode = function(features) {
ol.format.GML.prototype.writeFeaturesNode = function(features, opt_options) {
var node = ol.xml.createElementNS('http://www.opengis.net/gml',
'featureMembers');
ol.xml.setAttributeNS(node, 'http://www.w3.org/2001/XMLSchema-instance',
@@ -1712,6 +1729,9 @@ ol.format.GML.prototype.writeFeaturesNode = function(features) {
featureNS: this.featureNS_,
featureType: this.featureType_
};
if (goog.isDef(opt_options)) {
goog.object.extend(context, opt_options);
}
ol.format.GML.writeFeatureMembers_(node, features, [context]);
return node;
};

View File

@@ -1,11 +1,11 @@
goog.provide('ol.test.format.GML');
var readGeometry = function(format, text) {
var readGeometry = function(format, text, opt_options) {
var doc = ol.xml.load(text);
// we need an intermediate node for testing purposes
var node = goog.dom.createElement(goog.dom.TagName.PRE);
node.appendChild(doc.documentElement);
return format.readGeometryFromNode(node);
return format.readGeometryFromNode(node, opt_options);
};
describe('ol.format.GML', function() {
@@ -33,6 +33,28 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a point geometry', function() {
var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857'
};
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:pos>1 2</gml:pos>' +
'</gml:Point>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.Point);
var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
var serialized = format.writeGeometry(g, config);
var pos = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = pos.split(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
});
it('can read and write a point geometry in EPSG:4326', function() {
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
@@ -63,6 +85,32 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a linestring geometry', function() {
var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857'
};
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:posList>1 2 3 4</gml:posList>' +
'</gml:LineString>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.LineString);
var coordinates = g.getCoordinates();
expect(coordinates[0].slice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
expect(coordinates[1].slice(0, 2)).to.eql(
ol.proj.transform([3, 4], 'CRS:84', 'EPSG:3857'));
var serialized = format.writeGeometry(g, config);
var poss = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = poss.split(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
expect(coordinate[2]).to.roughlyEqual(3, 1e-9);
expect(coordinate[3]).to.roughlyEqual(4, 1e-9);
});
it('can read and write a linestring geometry in EPSG:4326', function() {
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
@@ -779,7 +827,7 @@ describe('ol.format.GML', function() {
describe('when parsing more than one geometry', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/more-geoms.xml', function(xml) {
try {
@@ -805,7 +853,7 @@ describe('ol.format.GML', function() {
describe('when parsing an attribute name equal to featureType', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/repeated-name.xml', function(xml) {
try {
@@ -842,3 +890,4 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.xml');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');