2 * @requires OpenLayers/Layer.js
3 * @requires OpenLayers/Projection.js
7 * Class: OpenLayers.Layer.SphericalMercator
8 * A mixin for layers that wraps up the pieces neccesary to have a coordinate
9 * conversion for working with commercial APIs which use a spherical
10 * mercator projection. Using this layer as a base layer, additional
11 * layers can be used as overlays if they are in the same projection.
13 * A layer is given properties of this object by setting the sphericalMercator
16 * More projection information:
17 * - http://spatialreference.org/ref/user/google-projection/
20 * +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0
21 * +k=1.0 +units=m +nadgrids=@null +no_defs
24 * 900913=PROJCS["WGS84 / Simple Mercator", GEOGCS["WGS 84",
25 * DATUM["WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]],
26 * PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295],
27 * AXIS["Longitude", EAST], AXIS["Latitude", NORTH]],
28 * PROJECTION["Mercator_1SP_Google"],
29 * PARAMETER["latitude_of_origin", 0.0], PARAMETER["central_meridian", 0.0],
30 * PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 0.0],
31 * PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["x", EAST],
32 * AXIS["y", NORTH], AUTHORITY["EPSG","900913"]]
34 OpenLayers.Layer.SphericalMercator = {
38 * Get the map's extent.
41 * {<OpenLayers.Bounds>} The map extent.
43 getExtent: function() {
45 if (this.sphericalMercator) {
46 extent = this.map.calculateBounds();
48 extent = OpenLayers.Layer.FixedZoomLevels.prototype.getExtent.apply(this);
54 * Method: initMercatorParameters
55 * Set up the mercator parameters on the layer: resolutions,
58 initMercatorParameters: function() {
59 // set up properties for Mercator - assume EPSG:900913
60 this.RESOLUTIONS = [];
61 var maxResolution = 156543.0339;
62 for(var zoom=0; zoom<=this.MAX_ZOOM_LEVEL; ++zoom) {
63 this.RESOLUTIONS[zoom] = maxResolution / Math.pow(2, zoom);
66 this.projection = "EPSG:900913";
70 * APIMethod: forwardMercator
71 * Given a lon,lat in EPSG:4326, return a point in Spherical Mercator.
78 * {<OpenLayers.LonLat>} The coordinates transformed to Mercator.
80 forwardMercator: function(lon, lat) {
81 var x = lon * 20037508.34 / 180;
82 var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
84 y = y * 20037508.34 / 180;
86 return new OpenLayers.LonLat(x, y);
90 * APIMethod: inverseMercator
91 * Given a x,y in Spherical Mercator, return a point in EPSG:4326.
94 * x - {float} A map x in Spherical Mercator.
95 * y - {float} A map y in Spherical Mercator.
98 * {<OpenLayers.LonLat>} The coordinates transformed to EPSG:4326.
100 inverseMercator: function(x, y) {
102 var lon = (x / 20037508.34) * 180;
103 var lat = (y / 20037508.34) * 180;
105 lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
107 return new OpenLayers.LonLat(lon, lat);
111 * Method: projectForward
112 * Given an object with x and y properties in EPSG:4326, modify the x,y
113 * properties on the object to be the Spherical Mercator projected
117 * point - {Object} An object with x and y properties.
120 * {Object} The point, with the x and y properties transformed to spherical
123 projectForward: function(point) {
124 var lonlat = OpenLayers.Layer.SphericalMercator.forwardMercator(point.x, point.y);
125 point.x = lonlat.lon;
126 point.y = lonlat.lat;
131 * Method: projectInverse
132 * Given an object with x and y properties in Spherical Mercator, modify
133 * the x,y properties on the object to be the unprojected coordinates.
136 * point - {Object} An object with x and y properties.
139 * {Object} The point, with the x and y properties transformed from
140 * spherical mercator to unprojected coordinates..
142 projectInverse: function(point) {
143 var lonlat = OpenLayers.Layer.SphericalMercator.inverseMercator(point.x, point.y);
144 point.x = lonlat.lon;
145 point.y = lonlat.lat;
152 * Note: Two transforms declared
153 * Transforms from EPSG:4326 to EPSG:900913 and from EPSG:900913 to EPSG:4326
154 * are set by this class.
156 OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:900913",
157 OpenLayers.Layer.SphericalMercator.projectForward);
158 OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:4326",
159 OpenLayers.Layer.SphericalMercator.projectInverse);