Move mercator transforms to Projection.js.

The SphericalMercator mixin is not required for coordinate transforms.  Default transforms included whenever Projection.js is included.
This commit is contained in:
Tim Schaub
2012-01-16 22:39:44 -07:00
parent 0e8b3f2ff8
commit 4b949176d3
5 changed files with 75 additions and 125 deletions

View File

@@ -114,14 +114,14 @@ OpenLayers.Layer.SphericalMercator = {
* Returns:
* {<OpenLayers.LonLat>} 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:
* {<OpenLayers.LonLat>} 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<len; ++i) {
code = codes[i];
add("EPSG:4326", code, merc.projectForward);
add(code, "EPSG:4326", merc.projectInverse);
for (j=i+1; j<len; ++j) {
other = codes[j];
add(code, other, same);
add(other, code, same);
}
}
})();

View File

@@ -195,10 +195,14 @@ OpenLayers.Projection.transform = function(point, source, dest) {
}
if (source.proj && dest.proj) {
point = Proj4js.transform(source.proj, dest.proj, point);
} else if (OpenLayers.Projection.transforms[source.getCode()] &&
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
}
} else {
var sourceCode = source.getCode();
var destCode = dest.getCode();
var transforms = OpenLayers.Projection.transforms;
if (transforms[sourceCode] && transforms[sourceCode][destCode]) {
transforms[sourceCode][destCode](point);
}
}
}
return point;
};
@@ -209,10 +213,6 @@ OpenLayers.Projection.transform = function(point, source, dest) {
* proj4js is not available:
*
* (code)
* OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857",
* OpenLayers.Layer.SphericalMercator.projectForward);
* OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:3857",
* OpenLayers.Layer.SphericalMercator.projectInverse);
* OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:900913",
* OpenLayers.Projection.nullTransform);
* OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:3857",
@@ -222,3 +222,47 @@ OpenLayers.Projection.transform = function(point, source, dest) {
OpenLayers.Projection.nullTransform = function(point) {
return point;
};
/**
* 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() {
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<len; ++i) {
code = codes[i];
add("EPSG:4326", code, forwardMercator);
add(code, "EPSG:4326", inverseMercator);
for (j=i+1; j<len; ++j) {
other = codes[j];
add(code, other, same);
add(other, code, same);
}
}
})();