1
|
|
|
/*jslint |
2
|
|
|
nomen: false, |
3
|
|
|
indent: 4 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
/*global |
7
|
|
|
GeographicLib, google |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
var Coordinates = {}; |
11
|
|
|
Coordinates.m_format = "DM"; |
12
|
|
|
Coordinates.m_geod = GeographicLib.Geodesic.WGS84; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
Coordinates.setFormat = function (format) { |
16
|
|
|
'use strict'; |
17
|
|
|
|
18
|
|
|
if (format === "DM" || format === "DMS" || format === "D") { |
19
|
|
|
this.m_format = format; |
20
|
|
|
} |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
Coordinates.validLat = function (lat) { |
25
|
|
|
'use strict'; |
26
|
|
|
|
27
|
|
|
return lat !== null && lat !== undefined && !isNaN(lat) && -90.0 <= lat && lat <= 90.0; |
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
Coordinates.validLng = function (lng) { |
32
|
|
|
'use strict'; |
33
|
|
|
|
34
|
|
|
return lng !== null && lng !== undefined && !isNaN(lng) && -180.0 <= lng && lng <= 180.0; |
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
Coordinates.valid = function (lat, lng) { |
39
|
|
|
'use strict'; |
40
|
|
|
|
41
|
|
|
return this.validLat(lat) && this.validLng(lng); |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
Coordinates.toLatLng = function (lat, lng) { |
46
|
|
|
'use strict'; |
47
|
|
|
|
48
|
|
|
if (this.valid(lat, lng)) { |
49
|
|
|
return new google.maps.LatLng(lat, lng); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return null; |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
Coordinates.sanitize = function (s) { |
57
|
|
|
'use strict'; |
58
|
|
|
|
59
|
|
|
var sanitized = "", |
60
|
|
|
commas = 0, |
61
|
|
|
periods = 0, |
62
|
|
|
i; |
63
|
|
|
|
64
|
|
|
for (i = 0; i < s.length; i = i + 1) { |
65
|
|
|
if ((s[i] === 'o') || (s[i] === 'O')) { |
66
|
|
|
// map 'O'/'o' to 'E' (German 'Ost' = 'East') |
67
|
|
|
sanitized += 'E'; |
68
|
|
|
} else if (s[i].match(/[a-z0-9\-]/i)) { |
69
|
|
|
sanitized += s[i].toUpperCase(); |
70
|
|
|
} else if (s[i] === '.') { |
71
|
|
|
periods += 1; |
72
|
|
|
sanitized += s[i]; |
73
|
|
|
} else if (s[i] === ',') { |
74
|
|
|
commas += 1; |
75
|
|
|
sanitized += s[i]; |
76
|
|
|
} else { |
77
|
|
|
sanitized += ' '; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// try to map commas to spaces or periods |
82
|
|
|
if ((commas === 1) && ((periods === 0) || (periods >= 2))) { |
83
|
|
|
return sanitized.replace(/,/g, ' '); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ((commas >= 1) && (periods === 0)) { |
87
|
|
|
return sanitized.replace(/,/g, '.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return sanitized; |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
Coordinates.create = function (h1, d1, m1, s1, h2, d2, m2, s2) { |
95
|
|
|
'use strict'; |
96
|
|
|
|
97
|
|
|
var c1, c2, lat, lng; |
98
|
|
|
|
99
|
|
|
if (h1 !== '+' && d1 < 0) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
if (m1 < 0 || m1 >= 60) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
if (s1 < 0 || s1 >= 60) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (h2 !== '+' && d2 < 0) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
if (m2 < 0 || m2 >= 60) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
if (s2 < 0 || s2 >= 60) { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
c1 = d1 + (m1 / 60.0) + (s1 / 3600.0); |
120
|
|
|
c2 = d2 + (m2 / 60.0) + (s2 / 3600.0); |
121
|
|
|
|
122
|
|
|
if (h1 === '+' && h2 === '+') { |
123
|
|
|
lat = c1; |
124
|
|
|
lng = c2; |
125
|
|
|
} else if ((h1 === 'N' || h1 === 'S') && (h2 === 'E' || h2 === 'W')) { |
126
|
|
|
lat = c1; |
127
|
|
|
lng = c2; |
128
|
|
|
if (h1 === 'S') { |
129
|
|
|
lat = -lat; |
130
|
|
|
} |
131
|
|
|
if (h2 === 'W') { |
132
|
|
|
lng = -lng; |
133
|
|
|
} |
134
|
|
|
} else if ((h2 === 'N' || h2 === 'S') && (h1 === 'E' || h1 === 'W')) { |
135
|
|
|
lat = c2; |
136
|
|
|
lng = c1; |
137
|
|
|
if (h2 === 'S') { |
138
|
|
|
lat = -lat; |
139
|
|
|
} |
140
|
|
|
if (h1 === 'W') { |
141
|
|
|
lng = -lng; |
142
|
|
|
} |
143
|
|
|
} else { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return Coordinates.toLatLng(lat, lng); |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
Coordinates.fromString = function (coordsString) { |
152
|
|
|
'use strict'; |
153
|
|
|
|
154
|
|
|
var s = Coordinates.sanitize(coordsString), |
155
|
|
|
i, |
156
|
|
|
p, |
157
|
|
|
m, |
158
|
|
|
c, |
159
|
|
|
patterns = [ |
160
|
|
|
// DM / H D M |
161
|
|
|
[/^\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*$/, 1, 2, 3, 0, 4, 5, 6, 0], |
162
|
|
|
// DM / D H M |
163
|
|
|
[/^\s*(\d+)\s*([NEWS])\s*(\d+\.?\d*)\s+(\d+)\s*([NEWS])\s*(\d+\.?\d*)\s*$/, 2, 1, 3, 0, 5, 4, 6, 0], |
164
|
|
|
// DM / D M H |
165
|
|
|
[/^\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*$/, 3, 1, 2, 0, 6, 4, 5, 0], |
166
|
|
|
// DM / D M |
167
|
|
|
[/^\s*(\d+)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+\.?\d*)\s*$/, 'N', 1, 2, 0, 'E', 3, 4, 0], |
168
|
|
|
// DMS / H D M S |
169
|
|
|
[/^\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*$/, 1, 2, 3, 4, 5, 6, 7, 8], |
170
|
|
|
// DMS / D H M S |
171
|
|
|
[/^\s*(\d+)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s+(\d+)\s*([NEWS])\s*(\d+)\s+(\d+\.?\d*)\s*$/, 2, 1, 3, 4, 6, 5, 7, 8], |
172
|
|
|
// DMS / D M S H |
173
|
|
|
[/^\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*([NEWS])\s*$/, 4, 1, 2, 3, 6, 5, 6, 7], |
174
|
|
|
// DMS / D M S |
175
|
|
|
[/^\s*(\d+)\s+(\d+)\s+(\d+\.?\d*)\s+(\d+)\s+(\d+)\s+(\d+\.?\d*)\s*$/, 'N', 1, 2, 3, 'E', 4, 5, 6], |
176
|
|
|
// D / H D |
177
|
|
|
[/^\s*([NEWS])\s*(\d+\.?\d*)\s*([NEWS])\s*(\d+\.?\d*)\s*$/, 1, 2, 0, 0, 3, 4, 0, 0], |
178
|
|
|
// D / D H |
179
|
|
|
[/^\s*(\d+\.?\d*)\s*([NEWS])\s*(\d+\.?\d*)\s*([NEWS])\s*$/, 2, 1, 0, 0, 4, 3, 0, 0], |
180
|
|
|
// D / D |
181
|
|
|
[/^\s*(-?\d+\.?\d*)\s+(-?\d+\.?\d*)\s*$/, '+', 1, 0, 0, '+', 2, 0, 0] |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
function mm(match, index) { |
185
|
|
|
var mi; |
186
|
|
|
if (typeof(index) === 'number') { |
187
|
|
|
if (index > 0) { |
188
|
|
|
mi = match[index]; |
189
|
|
|
if (mi === '+' || mi === 'N' || mi === 'E' || mi === 'W' || mi === 'S') { |
190
|
|
|
return mi; |
191
|
|
|
} |
192
|
|
|
return parseFloat(mi); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
return index; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
for (i = 0; i < patterns.length; i = i + 1) { |
199
|
|
|
p = patterns[i]; |
200
|
|
|
m = s.match(p[0]); |
201
|
|
|
if (m) { |
202
|
|
|
c = Coordinates.create( |
203
|
|
|
mm(m, p[1]), |
204
|
|
|
mm(m, p[2]), |
205
|
|
|
mm(m, p[3]), |
206
|
|
|
mm(m, p[4]), |
207
|
|
|
mm(m, p[5]), |
208
|
|
|
mm(m, p[6]), |
209
|
|
|
mm(m, p[7]), |
210
|
|
|
mm(m, p[8]) |
211
|
|
|
); |
212
|
|
|
if (c) { |
213
|
|
|
return c; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
return null; |
218
|
|
|
}; |
219
|
|
|
|
220
|
|
|
Coordinates.NS = function (lat) { |
221
|
|
|
'use strict'; |
222
|
|
|
|
223
|
|
|
if (lat >= 0) { |
224
|
|
|
return 'N'; |
225
|
|
|
} |
226
|
|
|
return 'S'; |
227
|
|
|
}; |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
Coordinates.EW = function (lng) { |
231
|
|
|
'use strict'; |
232
|
|
|
|
233
|
|
|
if (lng >= 0) { |
234
|
|
|
return 'E'; |
235
|
|
|
} |
236
|
|
|
return 'W'; |
237
|
|
|
}; |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
Coordinates.toStringDM = function (coords) { |
241
|
|
|
'use strict'; |
242
|
|
|
|
243
|
|
|
var lat = Math.abs(coords.lat()), |
244
|
|
|
lat_deg, |
245
|
|
|
lat_min, |
246
|
|
|
lat_mmin, |
247
|
|
|
lng = Math.abs(coords.lng()), |
248
|
|
|
lng_deg, |
249
|
|
|
lng_min, |
250
|
|
|
lng_mmin, |
251
|
|
|
s; |
252
|
|
|
|
253
|
|
|
lat_deg = Math.floor(lat); |
254
|
|
|
lat = lat - lat_deg; |
255
|
|
|
lat_min = Math.floor(lat * 60); |
256
|
|
|
lat = lat * 60 - lat_min; |
257
|
|
|
lat_mmin = Math.floor(Math.round(lat * 1000)); |
258
|
|
|
while (lat_mmin >= 1000) { |
259
|
|
|
lat_mmin -= 1000; |
260
|
|
|
lat_min += 1; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
lng_deg = Math.floor(lng); |
264
|
|
|
lng = lng - lng_deg; |
265
|
|
|
lng_min = Math.floor(lng * 60); |
266
|
|
|
lng = lng * 60 - lng_min; |
267
|
|
|
lng_mmin = Math.floor(Math.round(lng * 1000)); |
268
|
|
|
while (lng_mmin >= 1000) { |
269
|
|
|
lng_mmin -= 1000; |
270
|
|
|
lng_min += 1; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
s = this.NS(coords.lat()) + |
274
|
|
|
" " + |
275
|
|
|
this.zeropad(lat_deg, 2) + |
276
|
|
|
" " + |
277
|
|
|
this.zeropad(lat_min, 2) + |
278
|
|
|
"." + |
279
|
|
|
this.zeropad(lat_mmin, 3) + |
280
|
|
|
" " + |
281
|
|
|
this.EW(coords.lng()) + |
282
|
|
|
" " + |
283
|
|
|
this.zeropad(lng_deg, 3) + |
284
|
|
|
" " + |
285
|
|
|
this.zeropad(lng_min, 2) + |
286
|
|
|
"." + |
287
|
|
|
this.zeropad(lng_mmin, 3); |
288
|
|
|
return s; |
289
|
|
|
}; |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
Coordinates.toStringDMS = function (coords) { |
293
|
|
|
'use strict'; |
294
|
|
|
|
295
|
|
|
var lat = Math.abs(coords.lat()), |
296
|
|
|
lat_deg, |
297
|
|
|
lat_min, |
298
|
|
|
lat_sec, |
299
|
|
|
lng = Math.abs(coords.lng()), |
300
|
|
|
lng_deg, |
301
|
|
|
lng_min, |
302
|
|
|
lng_sec, |
303
|
|
|
s; |
304
|
|
|
|
305
|
|
|
lat_deg = Math.floor(lat); |
306
|
|
|
lat = lat - lat_deg; |
307
|
|
|
lat_min = Math.floor(lat * 60); |
308
|
|
|
lat = lat * 60 - lat_min; |
309
|
|
|
lat_sec = lat * 60.0; |
310
|
|
|
|
311
|
|
|
lng_deg = Math.floor(lng); |
312
|
|
|
lng = lng - lng_deg; |
313
|
|
|
lng_min = Math.floor(lng * 60); |
314
|
|
|
lng = lng * 60 - lng_min; |
315
|
|
|
lng_sec = lng * 60.0; |
316
|
|
|
|
317
|
|
|
s = this.NS(coords.lat()) + |
318
|
|
|
" " + |
319
|
|
|
this.zeropad(lat_deg, 2) + |
320
|
|
|
" " + |
321
|
|
|
this.zeropad(lat_min, 2) + |
322
|
|
|
" " + |
323
|
|
|
this.zeropad(lat_sec.toFixed(2), 5) + |
324
|
|
|
" " + |
325
|
|
|
this.EW(coords.lng()) + |
326
|
|
|
" " + |
327
|
|
|
this.zeropad(lng_deg, 3) + |
328
|
|
|
" " + |
329
|
|
|
this.zeropad(lng_min, 2) + |
330
|
|
|
" " + |
331
|
|
|
this.zeropad(lng_sec.toFixed(2), 5); |
332
|
|
|
|
333
|
|
|
return s; |
334
|
|
|
}; |
335
|
|
|
|
336
|
|
|
|
337
|
|
|
Coordinates.toStringD = function (coords) { |
338
|
|
|
'use strict'; |
339
|
|
|
|
340
|
|
|
var lat = Math.abs(coords.lat()), |
341
|
|
|
lng = Math.abs(coords.lng()); |
342
|
|
|
|
343
|
|
|
return this.NS(coords.lat()) + |
344
|
|
|
" " + |
345
|
|
|
lat.toFixed(6) + |
346
|
|
|
" " + |
347
|
|
|
this.EW(coords.lng()) + |
348
|
|
|
" " + |
349
|
|
|
lng.toFixed(6); |
350
|
|
|
}; |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
Coordinates.toString = function (coords) { |
354
|
|
|
'use strict'; |
355
|
|
|
|
356
|
|
|
if (this.m_format === "DM") { |
357
|
|
|
return this.toStringDM(coords); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if (this.m_format === "DMS") { |
361
|
|
|
return this.toStringDMS(coords); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
if (this.m_format === "D") { |
365
|
|
|
return this.toStringD(coords); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return this.toStringDM(coords); |
369
|
|
|
}; |
370
|
|
|
|
371
|
|
|
|
372
|
|
|
Coordinates.dist_angle_geodesic = function (startpos, endpos) { |
373
|
|
|
'use strict'; |
374
|
|
|
|
375
|
|
|
var t = this.m_geod.Inverse(startpos.lat(), startpos.lng(), endpos.lat(), endpos.lng()), |
376
|
|
|
a = t.azi1; |
377
|
|
|
if (a < 0) { |
378
|
|
|
a += 360.0; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return {dist: t.s12, angle: a}; |
382
|
|
|
}; |
383
|
|
|
|
384
|
|
|
|
385
|
|
|
Coordinates.projection_geodesic = function (startpos, angle, distance) { |
386
|
|
|
'use strict'; |
387
|
|
|
|
388
|
|
|
var t = this.m_geod.Direct(startpos.lat(), startpos.lng(), angle, distance); |
389
|
|
|
return new google.maps.LatLng(t.lat2, t.lon2); |
390
|
|
|
}; |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
Coordinates.zeropad = function (num, width) { |
394
|
|
|
'use strict'; |
395
|
|
|
|
396
|
|
|
var s = String(num); |
397
|
|
|
while (s.length < width) { |
398
|
|
|
s = "0" + s; |
399
|
|
|
} |
400
|
|
|
return s; |
401
|
|
|
}; |
402
|
|
|
|