From 0168c2e46f6a1997870b3f0a3cbec2cf2bd9c42f Mon Sep 17 00:00:00 2001 From: ahocevar Date: Mon, 22 Jul 2013 15:45:59 +0200 Subject: [PATCH] Better interface for passing projection and other metadata --- src/ol/layer/vectorlayer.js | 22 ++++++++++++---------- src/ol/parser/featureparser.js | 29 +++++++++++++++-------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/ol/layer/vectorlayer.js b/src/ol/layer/vectorlayer.js index 3c7ac2104d..01a5550035 100644 --- a/src/ol/layer/vectorlayer.js +++ b/src/ol/layer/vectorlayer.js @@ -437,10 +437,10 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { return lookup[type]; }; - var addFeatures = function(features) { + var addFeatures = function(features, metadata) { var sourceProjection = this.getSource().getProjection(); if (goog.isNull(sourceProjection)) { - sourceProjection = parser.getProjection(); + sourceProjection = metadata.projection; } var transform = ol.proj.getTransform(sourceProjection, projection); @@ -468,20 +468,22 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { parser.readFeaturesFromStringAsync(data, goog.bind(addFeatures, this), options); } else { - goog.asserts.assert(goog.isFunction(parser.readFeaturesFromString), - 'Expected a parser with readFeaturesFromString method.'); - features = parser.readFeaturesFromString(data, options); - addFeatures.call(this, features); + goog.asserts.assert( + goog.isFunction(parser.readFeaturesWithMetadataFromString), + 'Expected parser with a readFeaturesWithMetadataFromString method.'); + result = parser.readFeaturesWithMetadataFromString(data, options); + addFeatures.call(this, result.features, result.metadata); } } else if (goog.isObject(data)) { if (goog.isFunction(parser.readFeaturesFromObjectAsync)) { parser.readFeaturesFromObjectAsync(data, goog.bind(addFeatures, this), options); } else { - goog.asserts.assert(goog.isFunction(parser.readFeaturesFromObject), - 'Expected a parser with a readFeaturesFromObject method.'); - features = parser.readFeaturesFromObject(data, options); - addFeatures.call(this, features); + goog.asserts.assert( + goog.isFunction(parser.readFeaturesWithMetadataFromObject), + 'Expected parser with a readFeaturesWithMetadataFromObject method.'); + result = parser.readFeaturesWithMetadataFromObject(data, options); + addFeatures.call(this, result.features, result.metadata); } } else { // TODO: parse more data types diff --git a/src/ol/parser/featureparser.js b/src/ol/parser/featureparser.js index c149d4c6f6..a249335789 100644 --- a/src/ol/parser/featureparser.js +++ b/src/ol/parser/featureparser.js @@ -8,23 +8,22 @@ goog.provide('ol.parser.StringFeatureParser'); goog.require('ol.Feature'); - /** - * @interface + * @typedef {{projection: ol.Projection}} */ -ol.parser.FeatureParser = function() {}; +ol.parser.ReadFeaturesMetadata; /** - * @return {ol.Projection} Data projection. + * @typedef {{features: Array., + * metadata: ol.parser.ReadFeaturesMetadata}} */ -ol.parser.FeatureParser.prototype.getProjection = goog.abstractMethod; +ol.parser.ReadFeaturesResult; /** * @interface - * @extends {ol.parser.FeatureParser} */ ol.parser.DomFeatureParser = function() {}; @@ -32,9 +31,9 @@ ol.parser.DomFeatureParser = function() {}; /** * @param {Element|Document} node Document or element node. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. - * @return {Array.} Array of features. + * @return {ol.parser.ReadFeaturesResult} Features and metadata. */ -ol.parser.DomFeatureParser.prototype.readFeaturesFromNode = +ol.parser.DomFeatureParser.prototype.readFeaturesWithMetadataFromNode = goog.abstractMethod; @@ -48,9 +47,9 @@ ol.parser.ObjectFeatureParser = function() {}; /** * @param {Object} obj Object representing features. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. - * @return {Array.} Array of features. + * @return {ol.parser.ReadFeaturesResult} Features and metadata. */ -ol.parser.ObjectFeatureParser.prototype.readFeaturesFromObject = +ol.parser.ObjectFeatureParser.prototype.readFeaturesWithMetadataFromObject = goog.abstractMethod; @@ -64,9 +63,9 @@ ol.parser.StringFeatureParser = function() {}; /** * @param {string} data String data. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. - * @return {Array.} Array of features. + * @return {ol.parser.ReadFeaturesResult} Features and metadata. */ -ol.parser.StringFeatureParser.prototype.readFeaturesFromString = +ol.parser.StringFeatureParser.prototype.readFeaturesWithMetadataFromString = goog.abstractMethod; @@ -79,7 +78,8 @@ ol.parser.AsyncStringFeatureParser = function() {}; /** * @param {string} data String data. - * @param {function(Array.)} callback Callback which is called + * @param {function(Array., ol.parser.ReadFeaturesMetadata)} + * callback Callback which is called * after parsing. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. */ @@ -96,7 +96,8 @@ ol.parser.AsyncObjectFeatureParser = function() {}; /** * @param {Object} obj Object representing features. - * @param {function(Array.)} callback Callback which is called + * @param {function(Array.), ol.parser.ReadFeaturesMetadata} + * callback Callback which is called * after parsing. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. */