Projection.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright (c) 2006-2011 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. * @requires OpenLayers/BaseTypes/Class.js
  7. * @requires OpenLayers/Util.js
  8. */
  9. /**
  10. * Class: OpenLayers.Projection
  11. * Class for coordinate transforms between coordinate systems.
  12. * Depends on the proj4js library. If proj4js is not available,
  13. * then this is just an empty stub.
  14. */
  15. OpenLayers.Projection = OpenLayers.Class({
  16. /**
  17. * Property: proj
  18. * {Object} Proj4js.Proj instance.
  19. */
  20. proj: null,
  21. /**
  22. * Property: projCode
  23. * {String}
  24. */
  25. projCode: null,
  26. /**
  27. * Property: titleRegEx
  28. * {RegExp} regular expression to strip the title from a proj4js definition
  29. */
  30. titleRegEx: /\+title=[^\+]*/,
  31. /**
  32. * Constructor: OpenLayers.Projection
  33. * This class offers several methods for interacting with a wrapped
  34. * pro4js projection object.
  35. *
  36. * Parameters:
  37. * projCode - {String} A string identifying the Well Known Identifier for
  38. * the projection.
  39. * options - {Object} An optional object to set additional properties
  40. * on the layer.
  41. *
  42. * Returns:
  43. * {<OpenLayers.Projection>} A projection object.
  44. */
  45. initialize: function(projCode, options) {
  46. OpenLayers.Util.extend(this, options);
  47. this.projCode = projCode;
  48. if (window.Proj4js) {
  49. this.proj = new Proj4js.Proj(projCode);
  50. }
  51. },
  52. /**
  53. * APIMethod: getCode
  54. * Get the string SRS code.
  55. *
  56. * Returns:
  57. * {String} The SRS code.
  58. */
  59. getCode: function() {
  60. return this.proj ? this.proj.srsCode : this.projCode;
  61. },
  62. /**
  63. * APIMethod: getUnits
  64. * Get the units string for the projection -- returns null if
  65. * proj4js is not available.
  66. *
  67. * Returns:
  68. * {String} The units abbreviation.
  69. */
  70. getUnits: function() {
  71. return this.proj ? this.proj.units : null;
  72. },
  73. /**
  74. * Method: toString
  75. * Convert projection to string (getCode wrapper).
  76. *
  77. * Returns:
  78. * {String} The projection code.
  79. */
  80. toString: function() {
  81. return this.getCode();
  82. },
  83. /**
  84. * Method: equals
  85. * Test equality of two projection instances. Determines equality based
  86. * soley on the projection code.
  87. *
  88. * Returns:
  89. * {Boolean} The two projections are equivalent.
  90. */
  91. equals: function(projection) {
  92. var p = projection, equals = false;
  93. if (p) {
  94. if (window.Proj4js && this.proj.defData && p.proj.defData) {
  95. equals = this.proj.defData.replace(this.titleRegEx, "") ==
  96. p.proj.defData.replace(this.titleRegEx, "");
  97. } else if (p.getCode) {
  98. var source = this.getCode(), target = p.getCode();
  99. equals = source == target ||
  100. !!OpenLayers.Projection.transforms[source] &&
  101. OpenLayers.Projection.transforms[source][target] ===
  102. OpenLayers.Projection.nullTransform;
  103. }
  104. }
  105. return equals;
  106. },
  107. /* Method: destroy
  108. * Destroy projection object.
  109. */
  110. destroy: function() {
  111. delete this.proj;
  112. delete this.projCode;
  113. },
  114. CLASS_NAME: "OpenLayers.Projection"
  115. });
  116. /**
  117. * Property: transforms
  118. * Transforms is an object, with from properties, each of which may
  119. * have a to property. This allows you to define projections without
  120. * requiring support for proj4js to be included.
  121. *
  122. * This object has keys which correspond to a 'source' projection object. The
  123. * keys should be strings, corresponding to the projection.getCode() value.
  124. * Each source projection object should have a set of destination projection
  125. * keys included in the object.
  126. *
  127. * Each value in the destination object should be a transformation function,
  128. * where the function is expected to be passed an object with a .x and a .y
  129. * property. The function should return the object, with the .x and .y
  130. * transformed according to the transformation function.
  131. *
  132. * Note - Properties on this object should not be set directly. To add a
  133. * transform method to this object, use the <addTransform> method. For an
  134. * example of usage, see the OpenLayers.Layer.SphericalMercator file.
  135. */
  136. OpenLayers.Projection.transforms = {};
  137. /**
  138. * APIMethod: addTransform
  139. * Set a custom transform method between two projections. Use this method in
  140. * cases where the proj4js lib is not available or where custom projections
  141. * need to be handled.
  142. *
  143. * Parameters:
  144. * from - {String} The code for the source projection
  145. * to - {String} the code for the destination projection
  146. * method - {Function} A function that takes a point as an argument and
  147. * transforms that point from the source to the destination projection
  148. * in place. The original point should be modified.
  149. */
  150. OpenLayers.Projection.addTransform = function(from, to, method) {
  151. if(!OpenLayers.Projection.transforms[from]) {
  152. OpenLayers.Projection.transforms[from] = {};
  153. }
  154. OpenLayers.Projection.transforms[from][to] = method;
  155. };
  156. /**
  157. * APIMethod: transform
  158. * Transform a point coordinate from one projection to another. Note that
  159. * the input point is transformed in place.
  160. *
  161. * Parameters:
  162. * point - {<OpenLayers.Geometry.Point> | Object} An object with x and y
  163. * properties representing coordinates in those dimensions.
  164. * source - {OpenLayers.Projection} Source map coordinate system
  165. * dest - {OpenLayers.Projection} Destination map coordinate system
  166. *
  167. * Returns:
  168. * point - {object} A transformed coordinate. The original point is modified.
  169. */
  170. OpenLayers.Projection.transform = function(point, source, dest) {
  171. if (source.proj && dest.proj) {
  172. point = Proj4js.transform(source.proj, dest.proj, point);
  173. } else if (source && dest &&
  174. OpenLayers.Projection.transforms[source.getCode()] &&
  175. OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
  176. OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
  177. }
  178. return point;
  179. };
  180. /**
  181. * APIFunction: nullTransform
  182. * A null transformation - useful for defining projection aliases when
  183. * proj4js is not available:
  184. *
  185. * (code)
  186. * OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857",
  187. * OpenLayers.Layer.SphericalMercator.projectForward);
  188. * OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:3857",
  189. * OpenLayers.Layer.SphericalMercator.projectInverse);
  190. * OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:900913",
  191. * OpenLayers.Projection.nullTransform);
  192. * OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:3857",
  193. * OpenLayers.Projection.nullTransform);
  194. * (end)
  195. */
  196. OpenLayers.Projection.nullTransform = function(point) {
  197. return point;
  198. };