-
Notifications
You must be signed in to change notification settings - Fork 6
/
projection.js
273 lines (250 loc) · 7.53 KB
/
projection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
define([
"dojo/_base/lang", // extend
"dojo/_base/array", // forEach
"dojo/_base/kernel", // global
"./Map",
"./Placemark",
"./FeatureContainer",
"./util/_base",
"./util/bbox"
], function(lang, array, kernel, Map, Placemark, FeatureContainer, u, bbox) {
// module object
var proj = {},
proj4js,
// registry of direct transform functions
transforms = {},
// registry of projection instances, used if global Proj4js is defined
projInstances = {}
;
// tranform function for the djeo wrapper for Proj4js
var djeoProj4jsTransform = function(fromProj, toProj, point) {
return proj4js.transform(fromProj, toProj, point);
};
// tranform function for the original Proj4js
var proj4jsTransform = function(fromProj, toProj, point) {
return Proj4js.transform(fromProj, toProj, point);
};
// direct transform function
var directTransform = function(fromProj, toProj, point) {
return transforms[fromProj][toProj](point);
};
// transform a single point (depth == 1)
// transform an array of points (depth == 2)
// transform an array of arrays of points (depth == 3)
// transfrom an array of arrays of arrays of points (depth == 4)
var transform = function(depth, /* Array */entities, /* Array */_entities, fromProj, toProj, transformFunction, bb) {
if (depth == 1) {
var p = transformFunction(fromProj, toProj, {x: entities[0], y: entities[1]});
_entities.push(p.x, p.y);
bbox.extend(bb, [p.x, p.y]);
}
else {
array.forEach(entities, function(entity){
var _entity = [];
transform(depth-1, entity, _entity, fromProj, toProj, transformFunction, bb);
_entities.push(_entity);
});
}
}
proj.transform = function(fromProj, toProj, coords, coordsType) {
// _ prefix means "projected"
var _coords = coords,
pi = projInstances
;
// get transforFunction, fromProj, toProj
// try different ways
var transformFunction;
// djeo wrapper for Proj4js
if (proj4js) {
if (!pi[fromProj]) pi[fromProj] = new proj4js.Proj(fromProj);
if (!pi[toProj]) pi[toProj] = new proj4js.Proj(toProj);
if (pi[fromProj].readyToUse && pi[fromProj].readyToUse) {
fromProj = pi[fromProj];
toProj = pi[toProj];
transformFunction = djeoProj4jsTransform;
}
}
// original Proj4js
else if (kernel.global.Proj4js) {
if (!pi[fromProj]) pi[fromProj] = new Proj4js.Proj(fromProj);
if (!pi[toProj]) pi[toProj] = new Proj4js.Proj(toProj);
if (pi[fromProj].readyToUse && pi[fromProj].readyToUse) {
fromProj = pi[fromProj];
toProj = pi[toProj];
transformFunction = proj4jsTransform;
}
}
if (!transformFunction && transforms[fromProj] && transforms[fromProj][toProj]) {
// try direct transform function
transformFunction = directTransform;
}
if (transformFunction) {
if (coordsType) {
_coords = [];
var _bbox = [Infinity,Infinity,-Infinity,-Infinity];
var depth = 0;
switch (coordsType) {
case "Point":
depth = 1;
break;
case "LineString":
depth = 2;
break;
case "Polygon":
depth = 3;
break;
case "MultiLineString":
depth = 3;
break;
case "MultiPolygon":
depth = 4;
break;
}
if (depth) transform(
depth,
coords,
_coords,
fromProj,
toProj,
transformFunction,
_bbox
);
}
else {
var p1 = transformFunction(fromProj, toProj, {x: coords[0], y: coords[1]});
var p2 = transformFunction(fromProj, toProj, {x: coords[2], y: coords[3]});
_coords = [p1.x, p1.y, p2.x, p2.y];
}
}
return _coords;
}
proj.addTransform = function(fromProj, toProj, transformFunc) {
if (!transforms[fromProj]) transforms[fromProj] = {};
transforms[fromProj][toProj] = transformFunc;
}
proj.setProj4js = function(_proj4js){
if (!proj4js) {
proj4js = _proj4js;
}
}
var p = Placemark.prototype;
// patch getCoords method of the Placemark class
var getCoords = p.getCoords; // original getCoords
p.getCoords = function() {
var coords = this._coords;
if (!coords) {
// calling original getCoords
coords = getCoords.call(this);
if (coords) {
var projection = this.getProjection();
// compare projections of coords and the map
var mapProjection = this.map.projection;
if (projection && mapProjection && projection !== mapProjection) {
coords = proj.transform(projection, mapProjection, coords, this.getCoordsType());
this._coords = coords;
}
}
}
return coords;
};
p = Map.prototype;
// patch _get_center method of the Map class
var _get_center = p._get_center; // original _get_center
p._get_center = function() {
var center = _get_center.call(this),
appProjection = this.appProjection || this.dataProjection || this.projection
;
if (this.projection && appProjection != this.projection) {
center = proj.transform(this.projection, appProjection, center, "Point");
}
return center;
}
// patch _get_extent method of the Map class
var _get_extent = p._get_extent; // original _get_extent
p._get_extent = function() {
var extent = _get_extent.call(this),
appProjection = this.appProjection || this.dataProjection || this.projection
;
if (this.projection && appProjection != this.projection) {
extent = proj.transform(this.projection, appProjection, extent);
}
return extent;
}
// patch containerPixelToCoords method of the Map class
var containerPixelToCoords = p.containerPixelToCoords; // original _get_extent
p.containerPixelToCoords = function(x, y) {
var coords = containerPixelToCoords.call(this, x, y),
appProjection = this.appProjection || this.dataProjection || this.projection
;
if (this.projection && appProjection != this.projection) {
coords = proj.transform(this.projection, appProjection, coords, "Point");
}
return coords;
}
// patch the Placemark class
lang.extend(Placemark, {
getProjection: function() {
return this.projection || this.parent.getProjection();
},
getBbox: function() {
// summary:
// Returns the feature bounding box in the current map projection
var bb = this._bbox;
if (!bb) {
// this.bbox is normally supplied as input parameter when a feature is created
if (this.bbox) {
// check if we can use this.bbox, i.e. this.bbox is in the map projection
var projection = this.getProjection();
// compare projections of this.bbox and the map
var mapProjection = this.map.projection;
if (projection && mapProjection && projection !== mapProjection) {
// discard this.bbox
bb = null;
}
}
}
if (!bb) bb = bbox.get(this);
return bb;
}
});
// patch the FeatureContainer class
lang.extend(FeatureContainer, {
getProjection: function() {
var projection = this.projection || this._projection;
if (!projection) {
projection = this.parent.getProjection();
// store the projection for future calls
this._projection = projection;
}
return projection;
}
});
// patch the Map class
lang.extend(Map, {
getProjection: function() {
return this.dataProjection || this.projection;
},
getCoords: function(coords, type) {
var appProjection = this.appProjection || this.dataProjection || this.projection;
if (this.projection && appProjection != this.projection) {
coords = proj.transform(appProjection, this.projection, coords, type || "Point");
}
return coords;
}
});
// transformations for Spherical Mercator projection
// see http://mercator.myzen.co.uk for derivation of formulas
proj.addTransform("EPSG:4326", "EPSG:3857", function(point){
return {
x: u.earthRadius * point.x * Math.PI / 180,
y: u.earthRadius * Math.log(Math.tan(Math.PI/4 + point.y*Math.PI/360))
};
});
proj.addTransform("EPSG:3857", "EPSG:4326", function(point){
return {
x: 180*point.x / (Math.PI * u.earthRadius),
y: 360/Math.PI * Math.atan(Math.exp(point.y / u.earthRadius)) - 90
};
});
return proj;
});