Using single argument for callback and string for projection

This makes the asynchronous and synchronous versions of
readFeatures work with the same data structures, and leaves
projection handling outside the parsers.
This commit is contained in:
ahocevar
2013-07-24 22:19:21 +02:00
parent a4ceb41938
commit 109ec71877
6 changed files with 48 additions and 55 deletions
+4 -3
View File
@@ -516,9 +516,10 @@
* @property {string|undefined} logo Logo. * @property {string|undefined} logo Logo.
* @property {ol.parser.Parser} parser Parser instance to parse data * @property {ol.parser.Parser} parser Parser instance to parse data
* provided as `data` or fetched from `url`. * provided as `data` or fetched from `url`.
* @property {ol.ProjectionLike|undefined} projection Projection. EPSG:4326 * @property {ol.ProjectionLike|undefined} projection Projection. Usually the
* is assumed if not defined. TODO: Get projection from the parser instead * projection is provided by the parser, so this only needs to be set if
* of assuming EPSG:4326. * the parser does not know the SRS (e.g. in some GML flavors), or if the
* projection determined by the parser needs to be overridden.
* @property {string|undefined} url Server url providing the vector data. * @property {string|undefined} url Server url providing the vector data.
*/ */
+5 -6
View File
@@ -423,8 +423,6 @@ ol.layer.Vector.prototype.groupFeaturesBySymbolizerLiteral =
* one projection. * one projection.
*/ */
ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) { ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
var features;
var lookup = {}; var lookup = {};
lookup[ol.geom.GeometryType.POINT] = this.pointVertices_; lookup[ol.geom.GeometryType.POINT] = this.pointVertices_;
lookup[ol.geom.GeometryType.LINESTRING] = this.lineVertices_; lookup[ol.geom.GeometryType.LINESTRING] = this.lineVertices_;
@@ -437,10 +435,11 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
return lookup[type]; return lookup[type];
}; };
var addFeatures = function(features, metadata) { var addFeatures = function(data) {
var features = data.features;
var sourceProjection = this.getSource().getProjection(); var sourceProjection = this.getSource().getProjection();
if (goog.isNull(sourceProjection)) { if (goog.isNull(sourceProjection)) {
sourceProjection = metadata.projection; sourceProjection = data.metadata.projection;
} }
var transform = ol.proj.getTransform(sourceProjection, projection); var transform = ol.proj.getTransform(sourceProjection, projection);
@@ -472,7 +471,7 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
goog.isFunction(parser.readFeaturesWithMetadataFromString), goog.isFunction(parser.readFeaturesWithMetadataFromString),
'Expected parser with a readFeaturesWithMetadataFromString method.'); 'Expected parser with a readFeaturesWithMetadataFromString method.');
result = parser.readFeaturesWithMetadataFromString(data, options); result = parser.readFeaturesWithMetadataFromString(data, options);
addFeatures.call(this, result.features, result.metadata); addFeatures.call(this, result);
} }
} else if (goog.isObject(data)) { } else if (goog.isObject(data)) {
if (goog.isFunction(parser.readFeaturesFromObjectAsync)) { if (goog.isFunction(parser.readFeaturesFromObjectAsync)) {
@@ -483,7 +482,7 @@ ol.layer.Vector.prototype.parseFeatures = function(data, parser, projection) {
goog.isFunction(parser.readFeaturesWithMetadataFromObject), goog.isFunction(parser.readFeaturesWithMetadataFromObject),
'Expected parser with a readFeaturesWithMetadataFromObject method.'); 'Expected parser with a readFeaturesWithMetadataFromObject method.');
result = parser.readFeaturesWithMetadataFromObject(data, options); result = parser.readFeaturesWithMetadataFromObject(data, options);
addFeatures.call(this, result.features, result.metadata); addFeatures.call(this, result);
} }
} else { } else {
// TODO: parse more data types // TODO: parse more data types
+17 -19
View File
@@ -8,19 +8,6 @@ goog.provide('ol.parser.StringFeatureParser');
goog.require('ol.Feature'); goog.require('ol.Feature');
/**
* @typedef {{projection: ol.Projection}}
*/
ol.parser.ReadFeaturesMetadata;
/**
* @typedef {{features: Array.<ol.Feature>,
* metadata: ol.parser.ReadFeaturesMetadata}}
*/
ol.parser.ReadFeaturesResult;
/** /**
* @interface * @interface
@@ -78,9 +65,8 @@ ol.parser.AsyncStringFeatureParser = function() {};
/** /**
* @param {string} data String data. * @param {string} data String data.
* @param {function(Array.<ol.Feature>, ol.parser.ReadFeaturesMetadata)} * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* callback Callback which is called * called after parsing.
* after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync = ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync =
@@ -96,9 +82,8 @@ ol.parser.AsyncObjectFeatureParser = function() {};
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(Array.<ol.Feature>, ol.parser.ReadFeaturesMetadata)} * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* callback Callback which is called * called after parsing.
* after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync = ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync =
@@ -111,7 +96,20 @@ ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync =
ol.parser.ReadFeaturesCallback; ol.parser.ReadFeaturesCallback;
/**
* @typedef {{projection: ol.ProjectionLike}}
*/
ol.parser.ReadFeaturesMetadata;
/** /**
* @typedef {{callback: ol.parser.ReadFeaturesCallback}} * @typedef {{callback: ol.parser.ReadFeaturesCallback}}
*/ */
ol.parser.ReadFeaturesOptions; ol.parser.ReadFeaturesOptions;
/**
* @typedef {{features: Array.<ol.Feature>,
* metadata: ol.parser.ReadFeaturesMetadata}}
*/
ol.parser.ReadFeaturesResult;
+2 -3
View File
@@ -16,7 +16,6 @@ goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions'); goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.proj');
@@ -65,7 +64,7 @@ ol.parser.GeoJSON.prototype.readFeaturesWithMetadataFromString =
function(str, opt_options) { function(str, opt_options) {
var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str)); var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str));
return {features: this.parseFeatureCollection_(json, opt_options), return {features: this.parseFeatureCollection_(json, opt_options),
metadata: {projection: ol.proj.get('EPSG:4326')}}; metadata: {projection: 'EPSG:4326'}};
}; };
@@ -79,7 +78,7 @@ ol.parser.GeoJSON.prototype.readFeaturesWithMetadataFromString =
ol.parser.GeoJSON.prototype.readFeaturesWithMetadataFromObject = ol.parser.GeoJSON.prototype.readFeaturesWithMetadataFromObject =
function(object, opt_options) { function(object, opt_options) {
return {features: this.parseFeatureCollection_(object, opt_options), return {features: this.parseFeatureCollection_(object, opt_options),
metadata: {projection: ol.proj.get('EPSG:4326')}}; metadata: {projection: 'EPSG:4326'}};
}; };
+2 -3
View File
@@ -12,7 +12,6 @@ goog.require('ol.parser.ObjectFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions'); goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
goog.require('ol.proj');
@@ -47,7 +46,6 @@ ol.parser.GPX = function(opt_options) {
'gpx': function(node, obj) { 'gpx': function(node, obj) {
if (!goog.isDef(obj.features)) { if (!goog.isDef(obj.features)) {
obj.features = []; obj.features = [];
obj.metadata = {projection: ol.proj.get('EPSG:4326')};
} }
this.readChildNodes(node, obj); this.readChildNodes(node, obj);
}, },
@@ -245,7 +243,8 @@ ol.parser.GPX.prototype.read = function(data) {
if (data && data.nodeType == 9) { if (data && data.nodeType == 9) {
data = data.documentElement; data = data.documentElement;
} }
var obj = {features: [], metadata: {projection: ol.proj.get('EPSG:4326')}}; var obj = /** @type {ol.parser.ReadFeaturesResult} */
({metadata: {projection: 'EPSG:4326'}});
this.readNode(data, obj); this.readNode(data, obj);
return obj; return obj;
}; };
+18 -21
View File
@@ -27,7 +27,6 @@ goog.require('ol.parser.DomFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions'); goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
goog.require('ol.proj');
goog.require('ol.style.Icon'); goog.require('ol.style.Icon');
goog.require('ol.style.Line'); goog.require('ol.style.Line');
goog.require('ol.style.LineLiteral'); goog.require('ol.style.LineLiteral');
@@ -66,7 +65,6 @@ ol.parser.KML = function(opt_options) {
'kml': function(node, obj) { 'kml': function(node, obj) {
if (!goog.isDef(obj.features)) { if (!goog.isDef(obj.features)) {
obj.features = []; obj.features = [];
obj.metadata = {projection: ol.proj.get('EPSG:4326')};
} }
if (!goog.isDef(obj.links)) { if (!goog.isDef(obj.links)) {
obj.links = []; obj.links = [];
@@ -823,9 +821,8 @@ goog.inherits(ol.parser.KML, ol.parser.XML);
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(Array.<ol.Feature>, ol.parser.ReadFeaturesMetadata)} * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* callback Callback which is called * called after parsing.
* after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromObjectAsync = ol.parser.KML.prototype.readFeaturesFromObjectAsync =
@@ -836,16 +833,15 @@ ol.parser.KML.prototype.readFeaturesFromObjectAsync =
/** /**
* @param {string} data String data. * @param {string} str String data.
* @param {function(Array.<ol.Feature>, ol.parser.ReadFeaturesMetadata)} * @param {function(ol.parser.ReadFeaturesResult)}
* callback Callback which is called * callback Callback which is called after parsing.
* after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options. * @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromStringAsync = ol.parser.KML.prototype.readFeaturesFromStringAsync =
function(data, callback, opt_options) { function(str, callback, opt_options) {
this.readFeaturesOptions_ = opt_options; this.readFeaturesOptions_ = opt_options;
this.read(data, callback); this.read(str, callback);
}; };
@@ -858,7 +854,7 @@ ol.parser.KML.prototype.readFeaturesFromStringAsync =
ol.parser.KML.prototype.readFeaturesWithMetadataFromString = ol.parser.KML.prototype.readFeaturesWithMetadataFromString =
function(str, opt_options) { function(str, opt_options) {
this.readFeaturesOptions_ = opt_options; this.readFeaturesOptions_ = opt_options;
return this.read(str); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(str));
}; };
@@ -871,7 +867,7 @@ ol.parser.KML.prototype.readFeaturesWithMetadataFromString =
ol.parser.KML.prototype.readFeaturesWithMetadataFromNode = ol.parser.KML.prototype.readFeaturesWithMetadataFromNode =
function(node, opt_options) { function(node, opt_options) {
this.readFeaturesOptions_ = opt_options; this.readFeaturesOptions_ = opt_options;
return this.read(node); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(node));
}; };
@@ -883,7 +879,7 @@ ol.parser.KML.prototype.readFeaturesWithMetadataFromNode =
ol.parser.KML.prototype.readFeaturesWithMetadataFromObject = ol.parser.KML.prototype.readFeaturesWithMetadataFromObject =
function(obj, opt_options) { function(obj, opt_options) {
this.readFeaturesOptions_ = opt_options; this.readFeaturesOptions_ = opt_options;
return this.read(obj); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(obj));
}; };
@@ -934,10 +930,11 @@ ol.parser.KML.prototype.parseLinks = function(deferreds, obj, done) {
/** /**
* @param {string|Document|Element|Object} data Data to read. * @param {string|Document|Element|Object} data Data to read.
* @param {Function=} opt_callback Optional callback to call when reading * @param {function(ol.parser.ReadFeaturesResult)=} opt_callback Optional
* is done. * callback to call when reading is done. If provided, this method will
* @return {ol.parser.ReadFeaturesResult} An object representing the * return undefined.
* document. * @return {ol.parser.ReadFeaturesResult|undefined} An object representing the
* document if `opt_callback` was not provided.
*/ */
ol.parser.KML.prototype.read = function(data, opt_callback) { ol.parser.KML.prototype.read = function(data, opt_callback) {
if (goog.isString(data)) { if (goog.isString(data)) {
@@ -946,7 +943,8 @@ ol.parser.KML.prototype.read = function(data, opt_callback) {
if (data && data.nodeType == 9) { if (data && data.nodeType == 9) {
data = data.documentElement; data = data.documentElement;
} }
var obj = {features: [], metadata: {projection: ol.proj.get('EPSG:4326')}}; var obj = /** @type {ol.parser.ReadFeaturesResult} */
({metadata: {projection: 'EPSG:4326'}});
this.readNode(data, obj); this.readNode(data, obj);
if (goog.isDef(opt_callback)) { if (goog.isDef(opt_callback)) {
var deferreds = []; var deferreds = [];
@@ -966,9 +964,8 @@ ol.parser.KML.prototype.read = function(data, opt_callback) {
}, this); }, this);
}); });
} else { } else {
return /* ol.parser.ReadFeaturesResult */(obj); return obj;
} }
return {features: null, metadata: {projection: ol.proj.get('EPSG:4326')}};
}; };