~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Projection.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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. */
 
4
 
 
5
/**
 
6
 * @requires OpenLayers/Util.js
 
7
 */
 
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
    /**
 
18
     * Property: proj
 
19
     * {Object} Proj4js.Proj instance.
 
20
     */
 
21
    proj: null,
 
22
    
 
23
    /**
 
24
     * Property: projCode
 
25
     * {String}
 
26
     */
 
27
    projCode: null,
 
28
 
 
29
    /**
 
30
     * Constructor: OpenLayers.Projection
 
31
     * This class offers several methods for interacting with a wrapped 
 
32
     *     pro4js projection object. 
 
33
     *
 
34
     * Parameters:
 
35
     * options - {Object} An optional object with properties to set on the
 
36
     *     format
 
37
     *
 
38
     * Returns:
 
39
     * {<OpenLayers.Projection>} A projection object.
 
40
     */
 
41
    initialize: function(projCode, options) {
 
42
        OpenLayers.Util.extend(this, options);
 
43
        this.projCode = projCode;
 
44
        if (window.Proj4js) {
 
45
            this.proj = new Proj4js.Proj(projCode);
 
46
        }
 
47
    },
 
48
    
 
49
    /**
 
50
     * APIMethod: getCode
 
51
     * Get the string SRS code.
 
52
     *
 
53
     * Returns:
 
54
     * {String} The SRS code.
 
55
     */
 
56
    getCode: function() {
 
57
        return this.proj ? this.proj.srsCode : this.projCode;
 
58
    },
 
59
   
 
60
    /**
 
61
     * APIMethod: getUnits
 
62
     * Get the units string for the projection -- returns null if 
 
63
     *     proj4js is not available.
 
64
     *
 
65
     * Returns:
 
66
     * {String} The units abbreviation.
 
67
     */
 
68
    getUnits: function() {
 
69
        return this.proj ? this.proj.units : null;
 
70
    },
 
71
 
 
72
    /**
 
73
     * Method: toString
 
74
     * Convert projection to string (getCode wrapper).
 
75
     *
 
76
     * Returns:
 
77
     * {String} The projection code.
 
78
     */
 
79
    toString: function() {
 
80
        return this.getCode();
 
81
    },
 
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
        if (projection && projection.getCode) {
 
93
            return this.getCode() == projection.getCode();
 
94
        } else {
 
95
            return false;
 
96
        }    
 
97
    },
 
98
 
 
99
    /* Method: destroy
 
100
     * Destroy projection object.
 
101
     */
 
102
    destroy: function() {
 
103
        delete this.proj;
 
104
        delete this.projCode;
 
105
    },
 
106
    
 
107
    CLASS_NAME: "OpenLayers.Projection" 
 
108
});     
 
109
 
 
110
/**
 
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.
 
115
 *
 
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. 
 
120
 * 
 
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.
 
125
 *
 
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.
 
129
 */
 
130
OpenLayers.Projection.transforms = {};
 
131
 
 
132
/**
 
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.
 
137
 *
 
138
 * Parameters:
 
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.
 
144
 */
 
145
OpenLayers.Projection.addTransform = function(from, to, method) {
 
146
    if(!OpenLayers.Projection.transforms[from]) {
 
147
        OpenLayers.Projection.transforms[from] = {};
 
148
    }
 
149
    OpenLayers.Projection.transforms[from][to] = method;
 
150
};
 
151
 
 
152
/**
 
153
 * APIMethod: transform
 
154
 * Transform a point coordinate from one projection to another.  Note that
 
155
 *     the input point is transformed in place.
 
156
 * 
 
157
 * Parameters:
 
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
 
162
 *
 
163
 * Returns:
 
164
 * point - {object} A transformed coordinate.  The original point is modified.
 
165
 */
 
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); 
 
173
    }
 
174
    return point;
 
175
};