From 0ea55b7f27053943a28f09e4df82c9224bc2b7c3 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Fri, 18 Jul 2014 20:47:36 +0200 Subject: [PATCH] Use a ReadFeatures object instead of sourceProjection/targetProjection --- externs/olx.js | 23 ++++++++++++ src/ol/format/featureformat.js | 9 ++--- src/ol/format/geojsonformat.js | 58 ++++++++++++------------------ src/ol/format/jsonfeatureformat.js | 9 ++--- 4 files changed, 52 insertions(+), 47 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 8ada72db1a..b0fd30c027 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -1144,6 +1144,29 @@ olx.control.ZoomToExtentOptions.prototype.tipLabel; olx.control.ZoomToExtentOptions.prototype.extent; +/** + * @typedef {{dataProjection: (ol.proj.ProjectionLike|undefined), + * featureProjection: (ol.proj.ProjectionLike|undefined)}} + */ +olx.format.ReadOptions; + + +/** + * Projection of the data we are reading. Default is the projection derived from + * the data. + * @type {ol.proj.ProjectionLike|undefined} + */ +olx.format.ReadOptions.prototype.dataProjection; + + +/** + * Projection of the feature geometries created by the format reader. Default + * is the `dataProjection`. + * @type {ol.proj.ProjectionLike|undefined} + */ +olx.format.ReadOptions.prototype.featureProjection; + + /** * @typedef {{defaultProjection: ol.proj.ProjectionLike, * geometryName: (string|undefined)}} diff --git a/src/ol/format/featureformat.js b/src/ol/format/featureformat.js index 41026bfa02..c466747771 100644 --- a/src/ol/format/featureformat.js +++ b/src/ol/format/featureformat.js @@ -35,8 +35,7 @@ ol.format.Feature.prototype.getType = goog.abstractMethod; * Read a single feature from a source. * * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. */ ol.format.Feature.prototype.readFeature = goog.abstractMethod; @@ -46,8 +45,7 @@ ol.format.Feature.prototype.readFeature = goog.abstractMethod; * Read all features from a source. * * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {Array.} Features. */ ol.format.Feature.prototype.readFeatures = goog.abstractMethod; @@ -57,8 +55,7 @@ ol.format.Feature.prototype.readFeatures = goog.abstractMethod; * Read a single geometry from a source. * * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.geom.Geometry} Geometry. */ ol.format.Feature.prototype.readGeometry = goog.abstractMethod; diff --git a/src/ol/format/geojsonformat.js b/src/ol/format/geojsonformat.js index 18847ff2db..1bfceb9742 100644 --- a/src/ol/format/geojsonformat.js +++ b/src/ol/format/geojsonformat.js @@ -64,22 +64,26 @@ ol.format.GeoJSON.EXTENSIONS_ = ['.geojson']; /** * @param {GeoJSONObject} object Object. - * @param {ol.proj.Projection} targetProjection Target projection. - * @param {ol.proj.Projection} sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @private * @return {ol.geom.Geometry} Geometry. */ -ol.format.GeoJSON.readGeometry_ = function( - object, targetProjection, sourceProjection) { +ol.format.GeoJSON.readGeometry_ = function(object, opt_options) { if (goog.isNull(object)) { return null; } + var featureProjection = goog.isDef(opt_options) && + goog.isDef(opt_options.featureProjection) ? + ol.proj.get(opt_options.featureProjection) : null; + var dataProjection = goog.isDef(opt_options) && + goog.isDef(opt_options.dataProjection) ? + ol.proj.get(opt_options.dataProjection) : null; var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type]; goog.asserts.assert(goog.isDef(geometryReader)); var geometry = geometryReader(object); - if (!goog.isNull(targetProjection) && !goog.isNull(sourceProjection) && - !ol.proj.equivalent(targetProjection, sourceProjection)) { - geometry.transform(sourceProjection, targetProjection); + if (!goog.isNull(featureProjection) && !goog.isNull(dataProjection) && + !ol.proj.equivalent(featureProjection, dataProjection)) { + geometry.transform(dataProjection, featureProjection); } return geometry; }; @@ -87,13 +91,12 @@ ol.format.GeoJSON.readGeometry_ = function( /** * @param {GeoJSONGeometryCollection} object Object. - * @param {ol.proj.Projection} targetProjection Target projection. - * @param {ol.proj.Projection} sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @private * @return {ol.geom.GeometryCollection} Geometry collection. */ ol.format.GeoJSON.readGeometryCollectionGeometry_ = function( - object, targetProjection, sourceProjection) { + object, opt_options) { goog.asserts.assert(object.type == 'GeometryCollection'); var geometries = goog.array.map(object.geometries, /** @@ -101,8 +104,7 @@ ol.format.GeoJSON.readGeometryCollectionGeometry_ = function( * @return {ol.geom.Geometry} geometry Geometry. */ function(geometry) { - return ol.format.GeoJSON.readGeometry_( - geometry, targetProjection, sourceProjection); + return ol.format.GeoJSON.readGeometry_(geometry, opt_options); }); return new ol.geom.GeometryCollection(geometries); }; @@ -348,8 +350,7 @@ ol.format.GeoJSON.prototype.getExtensions = function() { * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. * @api */ @@ -362,8 +363,7 @@ ol.format.GeoJSON.prototype.readFeature; * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {Array.} Features. * @api */ @@ -374,16 +374,11 @@ ol.format.GeoJSON.prototype.readFeatures; * @inheritDoc */ ol.format.GeoJSON.prototype.readFeatureFromObject = function( - object, opt_targetProjection, opt_sourceProjection) { + object, opt_options) { var geoJSONFeature = /** @type {GeoJSONFeature} */ (object); goog.asserts.assert(geoJSONFeature.type == 'Feature'); - var targetProjection = goog.isDef(opt_targetProjection) ? - ol.proj.get(opt_targetProjection) : null; - var sourceProjection = goog.isDef(opt_sourceProjection) ? - ol.proj.get(opt_sourceProjection) : - this.readProjectionFromObject(object); var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry, - targetProjection, sourceProjection); + opt_options); var feature = new ol.Feature(); if (goog.isDef(this.geometryName_)) { feature.setGeometryName(this.geometryName_); @@ -403,7 +398,7 @@ ol.format.GeoJSON.prototype.readFeatureFromObject = function( * @inheritDoc */ ol.format.GeoJSON.prototype.readFeaturesFromObject = function( - object, opt_targetProjection, opt_sourceProjection) { + object, opt_options) { var geoJSONObject = /** @type {GeoJSONObject} */ (object); if (geoJSONObject.type == 'Feature') { return [this.readFeatureFromObject(object)]; @@ -416,7 +411,7 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function( var i, ii; for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) { features.push(this.readFeatureFromObject(geoJSONFeatures[i], - opt_targetProjection, opt_sourceProjection)); + opt_options)); } return features; } else { @@ -431,8 +426,7 @@ ol.format.GeoJSON.prototype.readFeaturesFromObject = function( * * @function * @param {ArrayBuffer|Document|Node|Object|string} source Source. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.geom.Geometry} Geometry. * @api */ @@ -443,15 +437,9 @@ ol.format.GeoJSON.prototype.readGeometry; * @inheritDoc */ ol.format.GeoJSON.prototype.readGeometryFromObject = function( - object, opt_targetProjection, opt_sourceProjection) { - var targetProjection = goog.isDef(opt_targetProjection) ? - ol.proj.get(opt_targetProjection) : null; - var sourceProjection = goog.isDef(opt_sourceProjection) ? - ol.proj.get(opt_sourceProjection) : - this.readProjectionFromObject(object); + object, opt_options) { return ol.format.GeoJSON.readGeometry_( - /** @type {GeoJSONGeometry} */ (object), - targetProjection, sourceProjection); + /** @type {GeoJSONGeometry} */ (object), opt_options); }; diff --git a/src/ol/format/jsonfeatureformat.js b/src/ol/format/jsonfeatureformat.js index ae3a04be53..edecc8497c 100644 --- a/src/ol/format/jsonfeatureformat.js +++ b/src/ol/format/jsonfeatureformat.js @@ -72,8 +72,7 @@ ol.format.JSONFeature.prototype.readFeatures = function(source) { /** * @param {Object} object Object. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {ol.Feature} Feature. */ @@ -82,8 +81,7 @@ ol.format.JSONFeature.prototype.readFeatureFromObject = goog.abstractMethod; /** * @param {Object} object Object. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {Array.} Features. */ @@ -100,8 +98,7 @@ ol.format.JSONFeature.prototype.readGeometry = function(source) { /** * @param {Object} object Object. - * @param {ol.proj.ProjectionLike=} opt_targetProjection Target projection. - * @param {ol.proj.ProjectionLike=} opt_sourceProjection Source projection. + * @param {olx.format.ReadOptions=} opt_options Read options. * @protected * @return {ol.geom.Geometry} Geometry. */