~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/gis/openlayers/lib/OpenLayers/Layer/SphericalMercator.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @requires OpenLayers/Layer.js
 
3
 * @requires OpenLayers/Projection.js
 
4
 */
 
5
 
 
6
/**
 
7
 * Class: OpenLayers.Layer.SphericalMercator
 
8
 * A mixin for layers that wraps up the pieces neccesary to have a coordinate
 
9
 *     conversion for working with commercial APIs which use a spherical
 
10
 *     mercator projection.  Using this layer as a base layer, additional
 
11
 *     layers can be used as overlays if they are in the same projection.
 
12
 *
 
13
 * A layer is given properties of this object by setting the sphericalMercator
 
14
 *     property to true.
 
15
 *
 
16
 * More projection information:
 
17
 *  - http://spatialreference.org/ref/user/google-projection/
 
18
 *
 
19
 * Proj4 Text:
 
20
 *     +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0
 
21
 *     +k=1.0 +units=m +nadgrids=@null +no_defs
 
22
 *
 
23
 * WKT:
 
24
 *     900913=PROJCS["WGS84 / Simple Mercator", GEOGCS["WGS 84",
 
25
 *     DATUM["WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]], 
 
26
 *     PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], 
 
27
 *     AXIS["Longitude", EAST], AXIS["Latitude", NORTH]],
 
28
 *     PROJECTION["Mercator_1SP_Google"], 
 
29
 *     PARAMETER["latitude_of_origin", 0.0], PARAMETER["central_meridian", 0.0], 
 
30
 *     PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 0.0], 
 
31
 *     PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["x", EAST],
 
32
 *     AXIS["y", NORTH], AUTHORITY["EPSG","900913"]]
 
33
 */
 
34
OpenLayers.Layer.SphericalMercator = {
 
35
 
 
36
    /**
 
37
     * Method: getExtent
 
38
     * Get the map's extent.
 
39
     *
 
40
     * Returns:
 
41
     * {<OpenLayers.Bounds>} The map extent.
 
42
     */
 
43
    getExtent: function() {
 
44
        var extent = null;
 
45
        if (this.sphericalMercator) {
 
46
            extent = this.map.calculateBounds();
 
47
        } else {
 
48
            extent = OpenLayers.Layer.FixedZoomLevels.prototype.getExtent.apply(this);
 
49
        }
 
50
        return extent;
 
51
    },
 
52
 
 
53
    /** 
 
54
     * Method: initMercatorParameters 
 
55
     * Set up the mercator parameters on the layer: resolutions,
 
56
     *     projection, units.
 
57
     */
 
58
    initMercatorParameters: function() {
 
59
        // set up properties for Mercator - assume EPSG:900913
 
60
        this.RESOLUTIONS = [];
 
61
        var maxResolution = 156543.0339;
 
62
        for(var zoom=0; zoom<=this.MAX_ZOOM_LEVEL; ++zoom) {
 
63
            this.RESOLUTIONS[zoom] = maxResolution / Math.pow(2, zoom);
 
64
        }
 
65
        this.units = "m";
 
66
        this.projection = "EPSG:900913";
 
67
    },
 
68
 
 
69
    /**
 
70
     * APIMethod: forwardMercator
 
71
     * Given a lon,lat in EPSG:4326, return a point in Spherical Mercator.
 
72
     *
 
73
     * Parameters:
 
74
     * lon - {float} 
 
75
     * lat - {float}
 
76
     * 
 
77
     * Returns:
 
78
     * {<OpenLayers.LonLat>} The coordinates transformed to Mercator.
 
79
     */
 
80
    forwardMercator: function(lon, lat) {
 
81
        var x = lon * 20037508.34 / 180;
 
82
        var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
 
83
 
 
84
        y = y * 20037508.34 / 180;
 
85
        
 
86
        return new OpenLayers.LonLat(x, y);
 
87
    },
 
88
 
 
89
    /**
 
90
     * APIMethod: inverseMercator
 
91
     * Given a x,y in Spherical Mercator, return a point in EPSG:4326.
 
92
     *
 
93
     * Parameters:
 
94
     * x - {float} A map x in Spherical Mercator.
 
95
     * y - {float} A map y in Spherical Mercator.
 
96
     * 
 
97
     * Returns:
 
98
     * {<OpenLayers.LonLat>} The coordinates transformed to EPSG:4326.
 
99
     */
 
100
    inverseMercator: function(x, y) {
 
101
 
 
102
        var lon = (x / 20037508.34) * 180;
 
103
        var lat = (y / 20037508.34) * 180;
 
104
 
 
105
        lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
 
106
        
 
107
        return new OpenLayers.LonLat(lon, lat);
 
108
    },
 
109
 
 
110
    /**
 
111
     * Method: projectForward 
 
112
     * Given an object with x and y properties in EPSG:4326, modify the x,y
 
113
     * properties on the object to be the Spherical Mercator projected
 
114
     * coordinates.
 
115
     *
 
116
     * Parameters:
 
117
     * point - {Object} An object with x and y properties. 
 
118
     * 
 
119
     * Returns:
 
120
     * {Object} The point, with the x and y properties transformed to spherical
 
121
     * mercator.
 
122
     */
 
123
    projectForward: function(point) {
 
124
        var lonlat = OpenLayers.Layer.SphericalMercator.forwardMercator(point.x, point.y);
 
125
        point.x = lonlat.lon;
 
126
        point.y = lonlat.lat;
 
127
        return point;
 
128
    },
 
129
    
 
130
    /**
 
131
     * Method: projectInverse
 
132
     * Given an object with x and y properties in Spherical Mercator, modify
 
133
     * the x,y properties on the object to be the unprojected coordinates.
 
134
     *
 
135
     * Parameters:
 
136
     * point - {Object} An object with x and y properties. 
 
137
     * 
 
138
     * Returns:
 
139
     * {Object} The point, with the x and y properties transformed from
 
140
     * spherical mercator to unprojected coordinates..
 
141
     */
 
142
    projectInverse: function(point) {
 
143
        var lonlat = OpenLayers.Layer.SphericalMercator.inverseMercator(point.x, point.y);
 
144
        point.x = lonlat.lon;
 
145
        point.y = lonlat.lat;
 
146
        return point;
 
147
    }
 
148
 
 
149
};
 
150
 
 
151
/**
 
152
 * Note: Two transforms declared
 
153
 * Transforms from EPSG:4326 to EPSG:900913 and from EPSG:900913 to EPSG:4326
 
154
 *     are set by this class.
 
155
 */
 
156
OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:900913",
 
157
    OpenLayers.Layer.SphericalMercator.projectForward);
 
158
OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:4326",
 
159
    OpenLayers.Layer.SphericalMercator.projectInverse);