-
Notifications
You must be signed in to change notification settings - Fork 1
/
coords.js
143 lines (120 loc) · 3.76 KB
/
coords.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
function parseDD(line) {
const re = /^\D*?(-?[0-9]+(?:[.,][0-9]{1,20})?)[,\s]+(-?[0-9]+(?:[.,][0-9]{1,20})?)\D*$/;
const match = re.exec(line.trim());
if (!match) {
return false;
}
var lat = parseFloat(match[1].replace(',', '.'));
var lon = parseFloat(match[2].replace(',', '.'));
if (Math.abs(lat) > 90 || Math.abs(lon) > 180) {
return false;
}
return {'lat': lat, 'lon': lon};
}
function parseWSG84(line) {
const re = /^\D*([NS])\s*(\d{1,2}).\s*(\d{1,2}(?:[.,]\d{1,4})?)'?[,\s/\\]+([EW])\s*(\d{1,3}).\s*(\d{1,2}(?:[.,]\d{1,4})?)'?\D*$/;
const match = re.exec(line.trim());
if (!match) {
return false;
}
const latDeg = parseInt(match[2], 10);
const lonDeg = parseInt(match[5], 10);
if (latDeg > 90 || lonDeg > 180) {
return false;
}
const latMin = parseFloat(match[3].replace(',', '.'));
const lonMin = parseFloat(match[6].replace(',', '.'));
return {
'lat': match[1], 'lat_deg': latDeg, 'lat_min': latMin,
'lon': match[4], 'lon_deg': lonDeg, 'lon_min': lonMin
};
}
function checkGlue(glue) {
var newGlue = ', ';
if (typeof glue === 'undefined') {
newGlue = ', ';
} else {
newGlue = glue;
}
return newGlue;
}
/**
* @returns {string}
* @param {string} lat
* @param {number} latDeg
* @param {number} latMin
* @param {string} lon
* @param {number} lonDeg
* @param {number} lonMin
* @param {string} _glue
*/
function WGS84toDD(lat, latDeg, latMin, lon, lonDeg, lonMin, _glue) {
var glue = checkGlue(_glue);
var la = latDeg + (latMin / 60);
var lo = lonDeg + (lonMin / 60);
if (lat === 'S') la = -la;
if (lon === 'W') lo = -lo;
return la.toFixed(5) + glue + lo.toFixed(5);
}
/**
* @returns {string}
* @param {number} _lat
* @param {number} _lon
* @param {string} _glue symbols used to glue lat and lon together in result string
*/
function DDtoWGS84(_lat, _lon, _glue) {
var glue = checkGlue(_glue);
var latLetter = _lat >= 0 ? 'N' : 'S';
var lotLetter = _lon >= 0 ? 'E' : 'W';
var lat = Math.abs(_lat);
var lon = Math.abs(_lon);
var latDeg = Math.floor(lat);
var lonDeg = Math.floor(lon);
var latMin = (lat - latDeg) * 60;
var lonMin = (lon - lonDeg) * 60;
// \xB0 is °
return latLetter + ' ' + latDeg + '\xB0 ' + latMin.toFixed(3) + "'" + glue +
lotLetter + ' ' + lonDeg + '\xB0 ' + lonMin.toFixed(3) + "'";
}
function transformCoordinatesString(line, glue) {
var coordsFrom = line.trim();
var coordsTo = coordsFrom;
var res = parseWSG84(coordsFrom);
if (res) {
coordsTo = WGS84toDD(res.lat, res.lat_deg, res.lat_min,
res.lon, res.lon_deg, res.lon_min, glue);
} else if (parseDD(coordsFrom)) {
res = parseDD(coordsFrom);
coordsTo = DDtoWGS84(res.lat, res.lon, glue);
}
return coordsTo;
}
function transformCoordinatesInElem(e, glue) {
var coordsFrom = e.textContent.trim();
e.innerHTML = transformCoordinatesString(coordsFrom, glue);
}
function transformCoordinatesInElemValue(e, glue) {
var coordsFrom = e.value.trim();
e.value = transformCoordinatesString(coordsFrom, glue);
}
function transformCoordinatesInElemByIdValue(id, glue) {
var elem = document.getElementById(id);
var coordsFrom = elem.value.trim();
elem.value = transformCoordinatesString(coordsFrom, glue);
}
function transformCoordinatesInElemById(id, glue) {
var elem = document.getElementById(id);
transformCoordinatesInElem(elem, glue);
}
function transformCoordinatesInElemBySelector(selector, glue) {
document.querySelectorAll(selector).forEach(function (item) {
transformCoordinatesInElem(item, glue);
});
}
if (typeof module !== 'undefined' && module.exports) {
exports.parseDD = parseDD;
exports.parseWSG84 = parseWSG84;
exports.DDtoWGS84 = DDtoWGS84;
exports.WGS84toDD = WGS84toDD;
exports.transformCoordinatesString = transformCoordinatesString;
}