Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1
L.Proj = {};
2
 
3
L.Proj._isProj4Proj = function(a) {
4
        return typeof a['projName'] !== 'undefined';
5
}
6
 
7
L.Proj.Projection = L.Class.extend({
8
        initialize: function(a, def) {
9
                if (L.Proj._isProj4Proj(a)) {
10
                        this._proj = a;
11
                } else {
12
                        var code = a;
13
                        Proj4js.defs[code] = def;
14
                        this._proj = new Proj4js.Proj(code);
15
                }
16
        },
17
 
18
        project: function (latlng) {
19
                var point = new L.Point(latlng.lng, latlng.lat);
20
                return Proj4js.transform(Proj4js.WGS84, this._proj, point);
21
        },
22
 
23
        unproject: function (point, unbounded) {
24
                var point2 = Proj4js.transform(this._proj, Proj4js.WGS84, point.clone());
25
                return new L.LatLng(point2.y, point2.x, unbounded);
26
        }
27
});
28
 
29
L.Proj.CRS = L.Class.extend({
30
        includes: L.CRS,
31
 
32
        options: {
33
                transformation: new L.Transformation(1, 0, -1, 0)
34
        },
35
 
36
        initialize: function(a, b, c) {
37
                var code, proj, def, options;
38
 
39
                if (L.Proj._isProj4Proj(a)) {
40
                        proj = a;
41
                        code = proj.srsCode;
42
                        options = b || {};
43
 
44
                        this.projection = new L.Proj.Projection(proj);
45
                } else {
46
                        code = a;
47
                        def = b;
48
                        options = c || {};
49
                        this.projection = new L.Proj.Projection(code, def);
50
                }
51
 
52
                L.Util.setOptions(this, options);
53
                this.code = code;
54
                this.transformation = this.options.transformation;
55
 
56
                if (this.options.origin) {
57
                        this.transformation =
58
                                new L.Transformation(1, -this.options.origin[0],
59
                                        -1, this.options.origin[1]);
60
                }
61
 
62
                if (this.options.scales) {
63
                        this.scale = function(zoom) {
64
                                return this.options.scales[zoom];
65
                        }
66
                } else if (this.options.resolutions) {
67
                        this.scale = function(zoom) {
68
                                return 1 / this.options.resolutions[zoom];
69
                        }
70
                }
71
        }
72
});
73
 
74
L.Proj.CRS.TMS = L.Proj.CRS.extend({
75
        initialize: function(a, b, c, d) {
76
                if (L.Proj._isProj4Proj(a)) {
77
                        var proj = a,
78
                                projectedBounds = b,
79
                                options = c || {};
80
                        options.origin = [projectedBounds[0], projectedBounds[3]];
81
                        L.Proj.CRS.prototype.initialize(proj, options);
82
                } else {
83
                        var code = a,
84
                                def = b,
85
                                projectedBounds = c,
86
                                options = d || {};
87
                        options.origin = [projectedBounds[0], projectedBounds[3]];
88
                        L.Proj.CRS.prototype.initialize(code, def, options);
89
                }
90
 
91
                this.projectedBounds = projectedBounds;
92
        }
93
});
94
 
95
L.Proj.TileLayer = {};
96
 
97
L.Proj.TileLayer.TMS = L.TileLayer.extend({
98
        options: {
99
                tms: true,
100
                continuousWorld: true
101
        },
102
 
103
        initialize: function(urlTemplate, crs, options) {
104
                if (!(crs instanceof L.Proj.CRS.TMS)) {
105
                        throw new Error("CRS is not L.Proj.CRS.TMS.");
106
                }
107
 
108
                L.TileLayer.prototype.initialize.call(this, urlTemplate, options);
109
                this.crs = crs;
110
 
111
                // Verify grid alignment
112
                for (var i = this.options.minZoom; i < this.options.maxZoom; i++) {
113
                        var gridHeight = (this.crs.projectedBounds[3] - this.crs.projectedBounds[1]) /
114
                                this._projectedTileSize(i);
115
                        if (Math.abs(gridHeight - Math.round(gridHeight)) > 1e-3) {
116
                                throw new Error("Projected bounds does not match grid at zoom " + i);
117
                        }
118
                }
119
        },
120
 
121
        getTileUrl: function(tilePoint) {
122
                var gridHeight =
123
                        Math.round((this.crs.projectedBounds[3] - this.crs.projectedBounds[1]) /
124
                        this._projectedTileSize(this._map.getZoom()));
125
 
126
                // TODO: relies on some of TileLayer's internals
127
                return L.Util.template(this._url, L.Util.extend({
128
                        s: this._getSubdomain(tilePoint),
129
                        z: this._getZoomForUrl(),
130
                        x: tilePoint.x,
131
                        y: gridHeight - tilePoint.y - 1
132
                }, this.options));
133
        },
134
 
135
        _projectedTileSize: function(zoom) {
136
                return (this.options.tileSize / this.crs.scale(zoom));
137
        }
138
});
139
 
140
if (typeof module !== 'undefined') module.exports = L.Proj;
141
 
142
if (typeof L !== 'undefined' && typeof L.CRS !== 'undefined') {
143
        // This is left here for backwards compatibility
144
        L.CRS.proj4js = (function () {
145
                return function (code, def, transformation, options) {
146
                        options = options || {};
147
                        if (transformation) options.transformation = transformation;
148
 
149
                        return new L.Proj.CRS(code, def, options);
150
                };
151
        }());
152
}