1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under a modified BSD license.
2
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
3
* for the full text of the license. */
6
* @requires OpenLayers/Util.js
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.
15
OpenLayers.Projection = OpenLayers.Class({
19
* {Object} Proj4js.Proj instance.
30
* Constructor: OpenLayers.Projection
31
* This class offers several methods for interacting with a wrapped
32
* pro4js projection object.
35
* options - {Object} An optional object with properties to set on the
39
* {<OpenLayers.Projection>} A projection object.
41
initialize: function(projCode, options) {
42
OpenLayers.Util.extend(this, options);
43
this.projCode = projCode;
45
this.proj = new Proj4js.Proj(projCode);
51
* Get the string SRS code.
54
* {String} The SRS code.
57
return this.proj ? this.proj.srsCode : this.projCode;
62
* Get the units string for the projection -- returns null if
63
* proj4js is not available.
66
* {String} The units abbreviation.
68
getUnits: function() {
69
return this.proj ? this.proj.units : null;
74
* Convert projection to string (getCode wrapper).
77
* {String} The projection code.
79
toString: function() {
80
return this.getCode();
85
* Test equality of two projection instances. Determines equality based
86
* soley on the projection code.
89
* {Boolean} The two projections are equivalent.
91
equals: function(projection) {
92
if (projection && projection.getCode) {
93
return this.getCode() == projection.getCode();
100
* Destroy projection object.
102
destroy: function() {
104
delete this.projCode;
107
CLASS_NAME: "OpenLayers.Projection"
111
* Property: transforms
112
* Transforms is an object, with from properties, each of which may
113
* have a to property. This allows you to define projections without
114
* requiring support for proj4js to be included.
116
* This object has keys which correspond to a 'source' projection object. The
117
* keys should be strings, corresponding to the projection.getCode() value.
118
* Each source projection object should have a set of destination projection
119
* keys included in the object.
121
* Each value in the destination object should be a transformation function,
122
* where the function is expected to be passed an object with a .x and a .y
123
* property. The function should return the object, with the .x and .y
124
* transformed according to the transformation function.
126
* Note - Properties on this object should not be set directly. To add a
127
* transform method to this object, use the <addTransform> method. For an
128
* example of usage, see the OpenLayers.Layer.SphericalMercator file.
130
OpenLayers.Projection.transforms = {};
133
* APIMethod: addTransform
134
* Set a custom transform method between two projections. Use this method in
135
* cases where the proj4js lib is not available or where custom projections
136
* need to be handled.
139
* from - {String} The code for the source projection
140
* to - {String} the code for the destination projection
141
* method - {Function} A function that takes a point as an argument and
142
* transforms that point from the source to the destination projection
143
* in place. The original point should be modified.
145
OpenLayers.Projection.addTransform = function(from, to, method) {
146
if(!OpenLayers.Projection.transforms[from]) {
147
OpenLayers.Projection.transforms[from] = {};
149
OpenLayers.Projection.transforms[from][to] = method;
153
* APIMethod: transform
154
* Transform a point coordinate from one projection to another. Note that
155
* the input point is transformed in place.
158
* point - {{OpenLayers.Geometry.Point> | Object} An object with x and y
159
* properties representing coordinates in those dimensions.
160
* sourceProj - {OpenLayers.Projection} Source map coordinate system
161
* destProj - {OpenLayers.Projection} Destination map coordinate system
164
* point - {object} A transformed coordinate. The original point is modified.
166
OpenLayers.Projection.transform = function(point, source, dest) {
167
if (source.proj && dest.proj) {
168
point = Proj4js.transform(source.proj, dest.proj, point);
169
} else if (source && dest &&
170
OpenLayers.Projection.transforms[source.getCode()] &&
171
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
172
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);