GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 70b115...ddb6fc )
by Florian
01:08
created

Coordinates.fromString   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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