diff --git a/src/ol/proj/proj.js b/src/ol/proj/proj.js index acb09060e5..421874a57f 100644 --- a/src/ol/proj/proj.js +++ b/src/ol/proj/proj.js @@ -10,6 +10,7 @@ goog.require('goog.object'); goog.require('ol'); goog.require('ol.Extent'); goog.require('ol.TransformFunction'); +goog.require('ol.extent'); goog.require('ol.sphere.NORMAL'); @@ -700,9 +701,11 @@ ol.proj.cloneTransform = function(input, opt_output, opt_dimension) { /** * Transforms a coordinate from source projection to destination projection. - * See {@link ol.extent.applyTransform} for extent transformation. - * See the applyTransform method of {@link ol.geom.SimpleGeometry} and its - * subclasses for geometry transforms. + * This returns a new coordinate (and does not modify the original). + * + * See {@link ol.proj.transformExtent} for extent transformation. + * See the transform method of {@link ol.geom.Geometry} and its subclasses for + * geometry transforms. * * @param {ol.Coordinate} coordinate Coordinate. * @param {ol.proj.ProjectionLike} source Source projection-like. @@ -716,6 +719,22 @@ ol.proj.transform = function(coordinate, source, destination) { }; +/** + * Transforms an extent from source projection to destination projection. This + * returns a new extent (and does not modify the original). + * + * @param {ol.Extent} extent The extent to transform. + * @param {ol.proj.ProjectionLike} source Source projection-like. + * @param {ol.proj.ProjectionLike} destination Destination projection-like. + * @return {ol.Extent} The transformed extent. + * @api + */ +ol.proj.transformExtent = function(extent, source, destination) { + var transformFn = ol.proj.getTransform(source, destination); + return ol.extent.applyTransform(extent, transformFn); +}; + + /** * Transforms the given point to the destination projection. * diff --git a/test/spec/ol/proj/proj.test.js b/test/spec/ol/proj/proj.test.js index 7cbc59d016..9cceae83d7 100644 --- a/test/spec/ol/proj/proj.test.js +++ b/test/spec/ol/proj/proj.test.js @@ -101,6 +101,23 @@ describe('ol.proj', function() { }); }); + describe('transformExtent()', function() { + + it('transforms an extent given projection identifiers', function() { + var sourceExtent = [-15, -30, 45, 60]; + var destinationExtent = ol.proj.transformExtent( + sourceExtent, 'EPSG:4326', 'EPSG:3857'); + expect(destinationExtent).not.to.be(undefined); + expect(destinationExtent).not.to.be(null); + expect(destinationExtent[0]) + .to.roughlyEqual(-1669792.3618991037, 1e-9); + expect(destinationExtent[2]).to.roughlyEqual(5009377.085697311, 1e-9); + expect(destinationExtent[1]).to.roughlyEqual(-3503549.843504376, 1e-8); + expect(destinationExtent[3]).to.roughlyEqual(8399737.889818361, 1e-8); + }); + + }); + describe('Proj4js integration', function() { it('allows Proj4js projections to be used transparently', function() {