From c4fdbacc124e402522bb67c0a9a29349a7c6747f Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 30 Jul 2014 22:19:23 +0200 Subject: [PATCH] Make options complete in ol.format.Feature already --- src/ol/format/featureformat.js | 26 ++++++++++++++++-- src/ol/format/geojsonformat.js | 4 +-- src/ol/format/gmlformat.js | 38 ++++++++++++++++++--------- src/ol/format/jsonfeatureformat.js | 29 +++----------------- test/spec/ol/format/gmlformat.test.js | 2 +- 5 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/ol/format/featureformat.js b/src/ol/format/featureformat.js index 99f5c0a870..aad4e4a91e 100644 --- a/src/ol/format/featureformat.js +++ b/src/ol/format/featureformat.js @@ -26,6 +26,27 @@ ol.format.Feature = function() { ol.format.Feature.prototype.getExtensions = goog.abstractMethod; +/** + * Adds the data projection to the read options. + * @param {Document|Node|Object|string} source Source. + * @param {olx.format.ReadOptions=} opt_options Options. + * @return {olx.format.ReadOptions|undefined} Options. + * @protected + */ +ol.format.Feature.prototype.getReadOptions = function( + source, opt_options) { + var options; + if (goog.isDef(opt_options)) { + options = { + dataProjection: goog.isDef(opt_options.dataProjection) ? + opt_options.dataProjection : this.readProjection(source), + featureProjection: opt_options.featureProjection + }; + } + return options; +}; + + /** * @return {ol.format.FormatType} Format. */ @@ -103,13 +124,14 @@ ol.format.Feature.prototype.writeGeometry = goog.abstractMethod; /** * @param {ol.geom.Geometry} geometry Geometry. - * @param {boolean} write Set to true for writing, false for reading. + * @param {boolean} write Set to true for writing, false for reading. For + * writing, the geometry will be cloned before transforming. * @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options * Options. * @return {ol.geom.Geometry} Transformed geometry. * @protected */ -ol.format.Feature.transformGeometry = function( +ol.format.Feature.transformWithOptions = function( geometry, write, opt_options) { var featureProjection = goog.isDef(opt_options) ? ol.proj.get(opt_options.featureProjection) : null; diff --git a/src/ol/format/geojsonformat.js b/src/ol/format/geojsonformat.js index cf6c1acd90..f4c3e1fe67 100644 --- a/src/ol/format/geojsonformat.js +++ b/src/ol/format/geojsonformat.js @@ -75,7 +75,7 @@ ol.format.GeoJSON.readGeometry_ = function(object, opt_options) { } var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type]; goog.asserts.assert(goog.isDef(geometryReader)); - return ol.format.Feature.transformGeometry( + return ol.format.Feature.transformWithOptions( geometryReader(object), false, opt_options); }; @@ -177,7 +177,7 @@ ol.format.GeoJSON.writeGeometry_ = function(geometry, opt_options) { var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()]; goog.asserts.assert(goog.isDef(geometryWriter)); return geometryWriter( - ol.format.Feature.transformGeometry(geometry, true, opt_options)); + ol.format.Feature.transformWithOptions(geometry, true, opt_options)); }; diff --git a/src/ol/format/gmlformat.js b/src/ol/format/gmlformat.js index e757d6547a..6ee8620a1e 100644 --- a/src/ol/format/gmlformat.js +++ b/src/ol/format/gmlformat.js @@ -164,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 ol.format.Feature.transformGeometry(geometry, false, context); + return ol.format.Feature.transformWithOptions(geometry, false, context); } else { return undefined; } @@ -1040,12 +1040,8 @@ ol.format.GML.RING_PARSERS_ = { * @inheritDoc */ 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]); + var geometry = ol.format.GML.readGeometry(node, + [this.getReadOptions(node, goog.isDef(opt_options) ? opt_options : {})]); return (goog.isDef(geometry) ? geometry : null); }; @@ -1071,13 +1067,21 @@ ol.format.GML.prototype.readFeaturesFromNode = function(node, opt_options) { 'featureNS': this.featureNS_ }; if (goog.isDef(opt_options)) { - goog.object.extend(options, opt_options); - //FIXME Get dataProjection from data + goog.object.extend(options, this.getReadOptions(node, opt_options)); } return ol.format.GML.readFeatures_(node, [options]); }; +/** + * @inheritDoc + */ +ol.format.GML.prototype.readProjectionFromNode = function(node) { + //TODO read this from data + return ol.proj.get(this.srsName_); +}; + + /** * @param {Node} node Node. * @param {ol.geom.Point} value Point geometry. @@ -1455,11 +1459,21 @@ ol.format.GML.writeGeometry = function(node, geometry, objectStack) { goog.asserts.assert(goog.isObject(context)); var item = goog.object.clone(context); item.node = node; + var value; + if (goog.isArray(geometry)) { + if (goog.isDef(context.dataProjection)) { + value = ol.proj.transformExtent( + geometry, context.featureProjection, context.dataProjection); + } else { + value = geometry; + } + } else { + goog.asserts.assertInstanceof(geometry, ol.geom.Geometry); + value = ol.format.Feature.transformWithOptions(geometry, true, context); + } ol.xml.pushSerializeAndPop(/** @type {ol.xml.NodeStackItem} */ (item), ol.format.GML.GEOMETRY_SERIALIZERS_, - ol.format.GML.GEOMETRY_NODE_FACTORY_, - [ol.format.Feature.transformGeometry(geometry, true, context)], - objectStack); + ol.format.GML.GEOMETRY_NODE_FACTORY_, [value], objectStack); }; diff --git a/src/ol/format/jsonfeatureformat.js b/src/ol/format/jsonfeatureformat.js index 7b594bb69b..3fe530b574 100644 --- a/src/ol/format/jsonfeatureformat.js +++ b/src/ol/format/jsonfeatureformat.js @@ -46,26 +46,6 @@ ol.format.JSONFeature.prototype.getObject_ = function(source) { }; -/** - * Adds the data projection to the read options. - * @param {Object} obj Data object. - * @param {olx.format.ReadOptions=} opt_options Options. - * @return {olx.format.ReadOptions|undefined} Options. - * @private - */ -ol.format.JSONFeature.prototype.getReadOptions_ = function(obj, opt_options) { - var options; - if (goog.isDef(opt_options)) { - options = { - dataProjection: goog.isDef(opt_options.dataProjection) ? - opt_options.dataProjection : this.readProjectionFromObject(obj), - featureProjection: opt_options.featureProjection - }; - } - return options; -}; - - /** * @inheritDoc */ @@ -78,9 +58,8 @@ ol.format.JSONFeature.prototype.getType = function() { * @inheritDoc */ ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) { - var obj = this.getObject_(source); return this.readFeatureFromObject( - obj, this.getReadOptions_(obj, opt_options)); + this.getObject_(source), this.getReadOptions(source, opt_options)); }; @@ -88,9 +67,8 @@ ol.format.JSONFeature.prototype.readFeature = function(source, opt_options) { * @inheritDoc */ ol.format.JSONFeature.prototype.readFeatures = function(source, opt_options) { - var obj = this.getObject_(source); return this.readFeaturesFromObject( - obj, this.getReadOptions_(obj, opt_options)); + this.getObject_(source), this.getReadOptions(source, opt_options)); }; @@ -116,9 +94,8 @@ ol.format.JSONFeature.prototype.readFeaturesFromObject = goog.abstractMethod; * @inheritDoc */ ol.format.JSONFeature.prototype.readGeometry = function(source, opt_options) { - var obj = this.getObject_(source); return this.readGeometryFromObject( - obj, this.getReadOptions_(obj, opt_options)); + this.getObject_(source), this.getReadOptions(source, opt_options)); }; diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 9b10a7fe00..fc2e5ffcbe 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -35,7 +35,6 @@ describe('ol.format.GML', function() { it('can read, transform and write a point geometry', function() { var config = { - dataProjection: 'CRS:84', featureProjection: 'EPSG:3857' }; var text = @@ -48,6 +47,7 @@ describe('ol.format.GML', function() { var coordinates = g.getCoordinates(); expect(coordinates.splice(0, 2)).to.eql( ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857')); + config.dataProjection = 'CRS:84'; var serialized = format.writeGeometry(g, config); var pos = serialized.firstElementChild.firstElementChild.textContent; var coordinate = pos.split(' ');