1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Layer.js |
---|
8 | * @requires OpenLayers/Projection.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Layer.SphericalMercator |
---|
13 | * A mixin for layers that wraps up the pieces neccesary to have a coordinate |
---|
14 | * conversion for working with commercial APIs which use a spherical |
---|
15 | * mercator projection. Using this layer as a base layer, additional |
---|
16 | * layers can be used as overlays if they are in the same projection. |
---|
17 | * |
---|
18 | * A layer is given properties of this object by setting the sphericalMercator |
---|
19 | * property to true. |
---|
20 | * |
---|
21 | * More projection information: |
---|
22 | * - http://spatialreference.org/ref/user/google-projection/ |
---|
23 | * |
---|
24 | * Proj4 Text: |
---|
25 | * +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 |
---|
26 | * +k=1.0 +units=m +nadgrids=@null +no_defs |
---|
27 | * |
---|
28 | * WKT: |
---|
29 | * 900913=PROJCS["WGS84 / Simple Mercator", GEOGCS["WGS 84", |
---|
30 | * DATUM["WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]], |
---|
31 | * PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], |
---|
32 | * AXIS["Longitude", EAST], AXIS["Latitude", NORTH]], |
---|
33 | * PROJECTION["Mercator_1SP_Google"], |
---|
34 | * PARAMETER["latitude_of_origin", 0.0], PARAMETER["central_meridian", 0.0], |
---|
35 | * PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 0.0], |
---|
36 | * PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["x", EAST], |
---|
37 | * AXIS["y", NORTH], AUTHORITY["EPSG","900913"]] |
---|
38 | */ |
---|
39 | OpenLayers.Layer.SphericalMercator = { |
---|
40 | |
---|
41 | /** |
---|
42 | * Method: getExtent |
---|
43 | * Get the map's extent. |
---|
44 | * |
---|
45 | * Returns: |
---|
46 | * {<OpenLayers.Bounds>} The map extent. |
---|
47 | */ |
---|
48 | getExtent: function() { |
---|
49 | var extent = null; |
---|
50 | if (this.sphericalMercator) { |
---|
51 | extent = this.map.calculateBounds(); |
---|
52 | } else { |
---|
53 | extent = OpenLayers.Layer.FixedZoomLevels.prototype.getExtent.apply(this); |
---|
54 | } |
---|
55 | return extent; |
---|
56 | }, |
---|
57 | |
---|
58 | /** |
---|
59 | * Method: getLonLatFromViewPortPx |
---|
60 | * Get a map location from a pixel location |
---|
61 | * |
---|
62 | * Parameters: |
---|
63 | * viewPortPx - {<OpenLayers.Pixel>} |
---|
64 | * |
---|
65 | * Returns: |
---|
66 | * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view |
---|
67 | * port OpenLayers.Pixel, translated into lon/lat by map lib |
---|
68 | * If the map lib is not loaded or not centered, returns null |
---|
69 | */ |
---|
70 | getLonLatFromViewPortPx: function (viewPortPx) { |
---|
71 | return OpenLayers.Layer.prototype.getLonLatFromViewPortPx.apply(this, arguments); |
---|
72 | }, |
---|
73 | |
---|
74 | /** |
---|
75 | * Method: getViewPortPxFromLonLat |
---|
76 | * Get a pixel location from a map location |
---|
77 | * |
---|
78 | * Parameters: |
---|
79 | * lonlat - {<OpenLayers.LonLat>} |
---|
80 | * |
---|
81 | * Returns: |
---|
82 | * {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in |
---|
83 | * OpenLayers.LonLat, translated into view port pixels by map lib |
---|
84 | * If map lib is not loaded or not centered, returns null |
---|
85 | */ |
---|
86 | getViewPortPxFromLonLat: function (lonlat) { |
---|
87 | return OpenLayers.Layer.prototype.getViewPortPxFromLonLat.apply(this, arguments); |
---|
88 | }, |
---|
89 | |
---|
90 | /** |
---|
91 | * Method: initMercatorParameters |
---|
92 | * Set up the mercator parameters on the layer: resolutions, |
---|
93 | * projection, units. |
---|
94 | */ |
---|
95 | initMercatorParameters: function() { |
---|
96 | // set up properties for Mercator - assume EPSG:900913 |
---|
97 | this.RESOLUTIONS = []; |
---|
98 | var maxResolution = 156543.0339; |
---|
99 | for(var zoom=0; zoom<=this.MAX_ZOOM_LEVEL; ++zoom) { |
---|
100 | this.RESOLUTIONS[zoom] = maxResolution / Math.pow(2, zoom); |
---|
101 | } |
---|
102 | this.units = "m"; |
---|
103 | this.projection = this.projection || "EPSG:900913"; |
---|
104 | }, |
---|
105 | |
---|
106 | /** |
---|
107 | * APIMethod: forwardMercator |
---|
108 | * Given a lon,lat in EPSG:4326, return a point in Spherical Mercator. |
---|
109 | * |
---|
110 | * Parameters: |
---|
111 | * lon - {float} |
---|
112 | * lat - {float} |
---|
113 | * |
---|
114 | * Returns: |
---|
115 | * {<OpenLayers.LonLat>} The coordinates transformed to Mercator. |
---|
116 | */ |
---|
117 | forwardMercator: function(lon, lat) { |
---|
118 | var x = lon * 20037508.34 / 180; |
---|
119 | var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180); |
---|
120 | |
---|
121 | y = y * 20037508.34 / 180; |
---|
122 | |
---|
123 | return new OpenLayers.LonLat(x, y); |
---|
124 | }, |
---|
125 | |
---|
126 | /** |
---|
127 | * APIMethod: inverseMercator |
---|
128 | * Given a x,y in Spherical Mercator, return a point in EPSG:4326. |
---|
129 | * |
---|
130 | * Parameters: |
---|
131 | * x - {float} A map x in Spherical Mercator. |
---|
132 | * y - {float} A map y in Spherical Mercator. |
---|
133 | * |
---|
134 | * Returns: |
---|
135 | * {<OpenLayers.LonLat>} The coordinates transformed to EPSG:4326. |
---|
136 | */ |
---|
137 | inverseMercator: function(x, y) { |
---|
138 | |
---|
139 | var lon = (x / 20037508.34) * 180; |
---|
140 | var lat = (y / 20037508.34) * 180; |
---|
141 | |
---|
142 | lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2); |
---|
143 | |
---|
144 | return new OpenLayers.LonLat(lon, lat); |
---|
145 | }, |
---|
146 | |
---|
147 | /** |
---|
148 | * Method: projectForward |
---|
149 | * Given an object with x and y properties in EPSG:4326, modify the x,y |
---|
150 | * properties on the object to be the Spherical Mercator projected |
---|
151 | * coordinates. |
---|
152 | * |
---|
153 | * Parameters: |
---|
154 | * point - {Object} An object with x and y properties. |
---|
155 | * |
---|
156 | * Returns: |
---|
157 | * {Object} The point, with the x and y properties transformed to spherical |
---|
158 | * mercator. |
---|
159 | */ |
---|
160 | projectForward: function(point) { |
---|
161 | var lonlat = OpenLayers.Layer.SphericalMercator.forwardMercator(point.x, point.y); |
---|
162 | point.x = lonlat.lon; |
---|
163 | point.y = lonlat.lat; |
---|
164 | return point; |
---|
165 | }, |
---|
166 | |
---|
167 | /** |
---|
168 | * Method: projectInverse |
---|
169 | * Given an object with x and y properties in Spherical Mercator, modify |
---|
170 | * the x,y properties on the object to be the unprojected coordinates. |
---|
171 | * |
---|
172 | * Parameters: |
---|
173 | * point - {Object} An object with x and y properties. |
---|
174 | * |
---|
175 | * Returns: |
---|
176 | * {Object} The point, with the x and y properties transformed from |
---|
177 | * spherical mercator to unprojected coordinates.. |
---|
178 | */ |
---|
179 | projectInverse: function(point) { |
---|
180 | var lonlat = OpenLayers.Layer.SphericalMercator.inverseMercator(point.x, point.y); |
---|
181 | point.x = lonlat.lon; |
---|
182 | point.y = lonlat.lat; |
---|
183 | return point; |
---|
184 | } |
---|
185 | |
---|
186 | }; |
---|
187 | |
---|
188 | /** |
---|
189 | * Note: Two transforms declared |
---|
190 | * Transforms from EPSG:4326 to EPSG:900913 and from EPSG:900913 to EPSG:4326 |
---|
191 | * are set by this class. |
---|
192 | */ |
---|
193 | OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:900913", |
---|
194 | OpenLayers.Layer.SphericalMercator.projectForward); |
---|
195 | OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:4326", |
---|
196 | OpenLayers.Layer.SphericalMercator.projectInverse); |
---|