SphericalMercator.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Layer.js
  7. * @requires OpenLayers/Projection.js
  8. */
  9. /**
  10. * Class: OpenLayers.Layer.SphericalMercator
  11. * A mixin for layers that wraps up the pieces neccesary to have a coordinate
  12. * conversion for working with commercial APIs which use a spherical
  13. * mercator projection. Using this layer as a base layer, additional
  14. * layers can be used as overlays if they are in the same projection.
  15. *
  16. * A layer is given properties of this object by setting the sphericalMercator
  17. * property to true.
  18. *
  19. * More projection information:
  20. * - http://spatialreference.org/ref/user/google-projection/
  21. *
  22. * Proj4 Text:
  23. * +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0
  24. * +k=1.0 +units=m +nadgrids=@null +no_defs
  25. *
  26. * WKT:
  27. * 900913=PROJCS["WGS84 / Simple Mercator", GEOGCS["WGS 84",
  28. * DATUM["WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]],
  29. * PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295],
  30. * AXIS["Longitude", EAST], AXIS["Latitude", NORTH]],
  31. * PROJECTION["Mercator_1SP_Google"],
  32. * PARAMETER["latitude_of_origin", 0.0], PARAMETER["central_meridian", 0.0],
  33. * PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 0.0],
  34. * PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["x", EAST],
  35. * AXIS["y", NORTH], AUTHORITY["EPSG","900913"]]
  36. */
  37. OpenLayers.Layer.SphericalMercator = {
  38. /**
  39. * Method: getExtent
  40. * Get the map's extent.
  41. *
  42. * Returns:
  43. * {<OpenLayers.Bounds>} The map extent.
  44. */
  45. getExtent: function() {
  46. var extent = null;
  47. if (this.sphericalMercator) {
  48. extent = this.map.calculateBounds();
  49. } else {
  50. extent = OpenLayers.Layer.FixedZoomLevels.prototype.getExtent.apply(this);
  51. }
  52. return extent;
  53. },
  54. /**
  55. * Method: getLonLatFromViewPortPx
  56. * Get a map location from a pixel location
  57. *
  58. * Parameters:
  59. * viewPortPx - {<OpenLayers.Pixel>}
  60. *
  61. * Returns:
  62. * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in view
  63. * port OpenLayers.Pixel, translated into lon/lat by map lib
  64. * If the map lib is not loaded or not centered, returns null
  65. */
  66. getLonLatFromViewPortPx: function (viewPortPx) {
  67. return OpenLayers.Layer.prototype.getLonLatFromViewPortPx.apply(this, arguments);
  68. },
  69. /**
  70. * Method: getViewPortPxFromLonLat
  71. * Get a pixel location from a map location
  72. *
  73. * Parameters:
  74. * lonlat - {<OpenLayers.LonLat>}
  75. *
  76. * Returns:
  77. * {<OpenLayers.Pixel>} An OpenLayers.Pixel which is the passed-in
  78. * OpenLayers.LonLat, translated into view port pixels by map lib
  79. * If map lib is not loaded or not centered, returns null
  80. */
  81. getViewPortPxFromLonLat: function (lonlat) {
  82. return OpenLayers.Layer.prototype.getViewPortPxFromLonLat.apply(this, arguments);
  83. },
  84. /**
  85. * Method: initMercatorParameters
  86. * Set up the mercator parameters on the layer: resolutions,
  87. * projection, units.
  88. */
  89. initMercatorParameters: function() {
  90. // set up properties for Mercator - assume EPSG:900913
  91. this.RESOLUTIONS = [];
  92. var maxResolution = 156543.03390625;
  93. for(var zoom=0; zoom<=this.MAX_ZOOM_LEVEL; ++zoom) {
  94. this.RESOLUTIONS[zoom] = maxResolution / Math.pow(2, zoom);
  95. }
  96. this.units = "m";
  97. this.projection = this.projection || "EPSG:900913";
  98. },
  99. /**
  100. * APIMethod: forwardMercator
  101. * Given a lon,lat in EPSG:4326, return a point in Spherical Mercator.
  102. *
  103. * Parameters:
  104. * lon - {float}
  105. * lat - {float}
  106. *
  107. * Returns:
  108. * {<OpenLayers.LonLat>} The coordinates transformed to Mercator.
  109. */
  110. forwardMercator: (function() {
  111. var gg = new OpenLayers.Projection("EPSG:4326");
  112. var sm = new OpenLayers.Projection("EPSG:900913");
  113. return function(lon, lat) {
  114. var point = OpenLayers.Projection.transform({x: lon, y: lat}, gg, sm);
  115. return new OpenLayers.LonLat(point.x, point.y);
  116. };
  117. })(),
  118. /**
  119. * APIMethod: inverseMercator
  120. * Given a x,y in Spherical Mercator, return a point in EPSG:4326.
  121. *
  122. * Parameters:
  123. * x - {float} A map x in Spherical Mercator.
  124. * y - {float} A map y in Spherical Mercator.
  125. *
  126. * Returns:
  127. * {<OpenLayers.LonLat>} The coordinates transformed to EPSG:4326.
  128. */
  129. inverseMercator: (function() {
  130. var gg = new OpenLayers.Projection("EPSG:4326");
  131. var sm = new OpenLayers.Projection("EPSG:900913");
  132. return function(x, y) {
  133. var point = OpenLayers.Projection.transform({x: x, y: y}, sm, gg);
  134. return new OpenLayers.LonLat(point.x, point.y);
  135. };
  136. })()
  137. };