From 4b949176d3cce6b1de9f0737087dd09ea92e69af Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 16 Jan 2012 22:39:44 -0700 Subject: [PATCH 1/5] Move mercator transforms to Projection.js. The SphericalMercator mixin is not required for coordinate transforms. Default transforms included whenever Projection.js is included. --- examples/web-mercator.js | 8 -- lib/OpenLayers/Layer/SphericalMercator.js | 103 ++++------------------ lib/OpenLayers/Projection.js | 60 +++++++++++-- tests/Layer/Google/v3.html | 6 +- tests/Layer/SphericalMercator.html | 23 ++--- 5 files changed, 75 insertions(+), 125 deletions(-) diff --git a/examples/web-mercator.js b/examples/web-mercator.js index ed14d940b7..7a25d373c6 100644 --- a/examples/web-mercator.js +++ b/examples/web-mercator.js @@ -1,14 +1,6 @@ // make map available for easy debugging var map; -// if your application transforms coordinates to and from EPSG:102113 then -// you must uncomment the lines below - -// OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:102113", -// OpenLayers.Layer.SphericalMercator.projectForward); -// OpenLayers.Projection.addTransform("EPSG:102113", "EPSG:4326", -// OpenLayers.Layer.SphericalMercator.projectInverse); - function init() { var options = { diff --git a/lib/OpenLayers/Layer/SphericalMercator.js b/lib/OpenLayers/Layer/SphericalMercator.js index 2b5a764353..7f0e35321d 100644 --- a/lib/OpenLayers/Layer/SphericalMercator.js +++ b/lib/OpenLayers/Layer/SphericalMercator.js @@ -114,14 +114,14 @@ OpenLayers.Layer.SphericalMercator = { * Returns: * {} The coordinates transformed to Mercator. */ - forwardMercator: function(lon, lat) { - var x = lon * 20037508.34 / 180; - var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180); - - y = y * 20037508.34 / 180; - - return new OpenLayers.LonLat(x, y); - }, + forwardMercator: (function() { + var gg = new OpenLayers.Projection("EPSG:4326"); + var sm = new OpenLayers.Projection("EPSG:900913"); + return function(lon, lat) { + var point = OpenLayers.Projection.transform({x: lon, y: lat}, gg, sm); + return new OpenLayers.LonLat(point.x, point.y); + }; + })(), /** * APIMethod: inverseMercator @@ -134,84 +134,13 @@ OpenLayers.Layer.SphericalMercator = { * Returns: * {} The coordinates transformed to EPSG:4326. */ - inverseMercator: function(x, y) { - - var lon = (x / 20037508.34) * 180; - var lat = (y / 20037508.34) * 180; - - lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2); - - return new OpenLayers.LonLat(lon, lat); - }, - - /** - * Method: projectForward - * Given an object with x and y properties in EPSG:4326, modify the x,y - * properties on the object to be the Spherical Mercator projected - * coordinates. - * - * Parameters: - * point - {Object} An object with x and y properties. - * - * Returns: - * {Object} The point, with the x and y properties transformed to spherical - * mercator. - */ - projectForward: function(point) { - var lonlat = OpenLayers.Layer.SphericalMercator.forwardMercator(point.x, point.y); - point.x = lonlat.lon; - point.y = lonlat.lat; - return point; - }, - - /** - * Method: projectInverse - * Given an object with x and y properties in Spherical Mercator, modify - * the x,y properties on the object to be the unprojected coordinates. - * - * Parameters: - * point - {Object} An object with x and y properties. - * - * Returns: - * {Object} The point, with the x and y properties transformed from - * spherical mercator to unprojected coordinates.. - */ - projectInverse: function(point) { - var lonlat = OpenLayers.Layer.SphericalMercator.inverseMercator(point.x, point.y); - point.x = lonlat.lon; - point.y = lonlat.lat; - return point; - } + inverseMercator: (function() { + var gg = new OpenLayers.Projection("EPSG:4326"); + var sm = new OpenLayers.Projection("EPSG:900913"); + return function(x, y) { + var point = OpenLayers.Projection.transform({x: x, y: y}, sm, gg); + return new OpenLayers.LonLat(point.x, point.y); + }; + })() }; - -/** - * Note: Transforms for web mercator <-> EPSG:4326 - * OpenLayers recognizes EPSG:3857, EPSG:900913, EPSG:102113 and EPSG:102100. - * OpenLayers originally started referring to EPSG:900913 as web mercator. - * The EPSG has declared EPSG:3857 to be web mercator. - * ArcGIS 10 recognizes the EPSG:3857, EPSG:102113, and EPSG:102100 as - * equivalent. See http://blogs.esri.com/Dev/blogs/arcgisserver/archive/2009/11/20/ArcGIS-Online-moving-to-Google-_2F00_-Bing-tiling-scheme_3A00_-What-does-this-mean-for-you_3F00_.aspx#12084 - */ -(function() { - - // list of equivalent codes for web mercator - var codes = ["EPSG:900913", "EPSG:3857", "EPSG:102113", "EPSG:102100"]; - - var add = OpenLayers.Projection.addTransform; - var merc = OpenLayers.Layer.SphericalMercator; - var same = OpenLayers.Projection.nullTransform; - - var i, len, code, other, j; - for (i=0, len=codes.length; i EPSG:4326 + * OpenLayers recognizes EPSG:3857, EPSG:900913, EPSG:102113 and EPSG:102100. + * OpenLayers originally started referring to EPSG:900913 as web mercator. + * The EPSG has declared EPSG:3857 to be web mercator. + * ArcGIS 10 recognizes the EPSG:3857, EPSG:102113, and EPSG:102100 as + * equivalent. See http://blogs.esri.com/Dev/blogs/arcgisserver/archive/2009/11/20/ArcGIS-Online-moving-to-Google-_2F00_-Bing-tiling-scheme_3A00_-What-does-this-mean-for-you_3F00_.aspx#12084 + */ +(function() { + + var pole = 20037508.34; + + function inverseMercator(xy) { + xy.x = 180 * xy.x / pole; + xy.y = 180 / Math.PI * (2 * Math.atan(Math.exp((xy.y / pole) * Math.PI)) - Math.PI / 2); + return xy; + } + + function forwardMercator(xy) { + xy.x = xy.x * pole / 180; + xy.y = Math.log(Math.tan((90 + xy.y) * Math.PI / 360)) / Math.PI * pole; + return xy; + } + + // list of equivalent codes for web mercator + var codes = ["EPSG:900913", "EPSG:3857", "EPSG:102113", "EPSG:102100"]; + + var add = OpenLayers.Projection.addTransform; + var same = OpenLayers.Projection.nullTransform; + + var i, len, code, other, j; + for (i=0, len=codes.length; i Date: Mon, 16 Jan 2012 22:59:23 -0700 Subject: [PATCH 2/5] Remove SphericalMercator dependency from Bing. --- lib/OpenLayers/Layer/Bing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OpenLayers/Layer/Bing.js b/lib/OpenLayers/Layer/Bing.js index 85c27acdeb..d707b8fe84 100644 --- a/lib/OpenLayers/Layer/Bing.js +++ b/lib/OpenLayers/Layer/Bing.js @@ -5,7 +5,7 @@ /** * @requires OpenLayers/Layer/XYZ.js - * @requires OpenLayers/Layer/SphericalMercator.js + * @requires OpenLayers/Projection.js */ /** From 6134ad6d914b3d2883fa9b6d75722e04b5618124 Mon Sep 17 00:00:00 2001 From: tschaub Date: Tue, 17 Jan 2012 12:34:40 -0700 Subject: [PATCH 3/5] Removing redundant dependency. Projection.js gets pulled in as a transitive dependency of all layers. --- lib/OpenLayers/Layer/Bing.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/OpenLayers/Layer/Bing.js b/lib/OpenLayers/Layer/Bing.js index d707b8fe84..a9dbd017a1 100644 --- a/lib/OpenLayers/Layer/Bing.js +++ b/lib/OpenLayers/Layer/Bing.js @@ -5,7 +5,6 @@ /** * @requires OpenLayers/Layer/XYZ.js - * @requires OpenLayers/Projection.js */ /** From bfa6f06bf33cc3a89204b133133e09cd4b4296b1 Mon Sep 17 00:00:00 2001 From: tschaub Date: Tue, 17 Jan 2012 12:35:50 -0700 Subject: [PATCH 4/5] Removing dependency on SphericalMercator. The default transforms (b/w EPSG:4326 & EPSG:900913) now come with any layer type. --- lib/OpenLayers/Layer/OSM.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/OpenLayers/Layer/OSM.js b/lib/OpenLayers/Layer/OSM.js index a1874e3453..320c4540ef 100644 --- a/lib/OpenLayers/Layer/OSM.js +++ b/lib/OpenLayers/Layer/OSM.js @@ -5,7 +5,6 @@ /** * @requires OpenLayers/Layer/XYZ.js - * @requires OpenLayers/Layer/SphericalMercator.js */ /** From 9a6827f6b3e166e6f762a323f08cd1108836f06c Mon Sep 17 00:00:00 2001 From: tschaub Date: Tue, 17 Jan 2012 12:38:31 -0700 Subject: [PATCH 5/5] Documenting the functionality included in Projection.js. This adds documentation for projection methods and includes detail in the release notes about build configuration related changes. --- lib/OpenLayers/Projection.js | 19 +++++++++++++++---- notes/2.12.md | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/OpenLayers/Projection.js b/lib/OpenLayers/Projection.js index 8c1f6a6a66..f4b02d0749 100644 --- a/lib/OpenLayers/Projection.js +++ b/lib/OpenLayers/Projection.js @@ -9,10 +9,21 @@ */ /** - * Class: OpenLayers.Projection - * Class for coordinate transforms between coordinate systems. - * Depends on the proj4js library. If proj4js is not available, - * then this is just an empty stub. + * Namespace: OpenLayers.Projection + * Methods for coordinate transforms between coordinate systems. By default, + * OpenLayers ships with the ability to transform coordinates between + * geographic (EPSG:4326) and web or spherical mercator (EPSG:900913 et al.) + * coordinate reference systems. See the method for details + * on usage. + * + * Additional transforms may be added by using the + * library. If the proj4js library is included, the method + * will work between any two coordinate reference systems with proj4js + * definitions. + * + * If the proj4js library is not included, or if you wish to allow transforms + * between arbitrary coordinate reference systems, use the + * method to register a custom transform method. */ OpenLayers.Projection = OpenLayers.Class({ diff --git a/notes/2.12.md b/notes/2.12.md index 7013846924..3626cc7b59 100644 --- a/notes/2.12.md +++ b/notes/2.12.md @@ -54,6 +54,12 @@ Without the WKT format included (by default), the `OpenLayers.Geometry::toString `Layer.OSM` is now defined in its own script file, namely `OpenLayers/Layer/OSM.js`. So people using `Layer.OSM` should now include `OpenLayers/Layer/OSM.js`, as opposed to `OpenLayers/Layer/XYZ.js`, in their OpenLayers builds. (See https://github.com/openlayers/openlayers/issues/138) +## Projection & SphericalMercator + +In previous releases, coordinate transforms between EPSG:4326 and EPSG:900913 were defined in the SphericalMercator.js script. In 2.12, these default transforms are included in the Projection.js script. The Projection.js script is included as a dependency in builds with any layer types, so no special build configuration is necessary to get the web mercator transforms. + +If you were previously using the `OpenLayers.Layer.SphericalMercator.forwardMercator` or `inverseMercator` methods, you may have to explicitly include the SphericalMercator.js script in your build. The Google layer is the only layer that depends on the SphericalMercator mixin. If you are not using the Google layer but want to use the SphericalMercator methods listed above, you have to explicitly include the SphericalMercator.js script in your build. + ## QueryStringFilter `OpenLayers.Protocol.HTTP` no longer requires `OpenLayers.Format.QueryStringFilter`. It you need this, make sure it is included in your build config file.