SphericalMercator.js 7.0 KB

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