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