forked from ProjectionWizard/projectionwizard.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDvsDMSconverter.js
196 lines (161 loc) · 3.72 KB
/
DDvsDMSconverter.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
/*
* PROJECTION WIZARD v2.0
* Map Projection Selection Tool
*
* Author: Bojan Savric
* Date: June, 2019
*
*/
/*DECIMAL DEGREES TO DEGREES, MINUTES, SECONDS*/
/*Coverting decimal degrees (number) in to the DMS form (string)*/
function dd2dms(alfa) {
var deg, min, sec;
//parsing DD to DMS
deg = parseInt(alfa);
min = parseInt((alfa - deg) * 60.0);
sec = Math.round(((alfa - deg) * 60.0 - min) * 60.0);
if (sec == 60.0) {
sec = 0.0;
min += 1.;
}
if (min == 60.0) {
min = 0.0;
deg += 1.;
}
//Combining a string
return deg + "º " + ("0" + min).slice(-2) + "' " + ("0" + sec).slice(-2) + "'' ";
}
/*Coverter for latitude*/
function dd2dmsLAT(phi) {
var letter;
//defining a hemisphere
if (phi < 0) {
letter = "S";
phi *= -1;
} else
letter = "N";
//returning the final string
return dd2dms(phi) + letter;
}
/*Coverter for latitude*/
function dd2dmLAT(phi) {
var letter;
//defining a hemisphere
if (phi < 0) {
letter = "S";
phi *= -1;
} else
letter = "N";
//returning the final string
return dd2dms(phi) + letter;
}
/*Coverter for longitude*/
function dd2dmLON(lam) {
var letter;
//defining a hemisphere
if (lam < 0) {
letter = "W";
lam *= -1;
} else
letter = "E";
return dd2dms(lam) + letter;
}
/*Coverter for longitude*/
function dd2dmsLON(lam) {
var letter;
//defining a hemisphere
if (lam < 0) {
letter = "W";
lam *= -1;
} else
letter = "E";
return dd2dms(lam) + letter;
}
/*DEGREES, MINUTES, SECONDS TO DECIMAL DEGREES*/
/*Coverting DMS form (string) in to the decimal degrees (number)*/
function dms2dd(string) {
var dec, min, sec;
//Getting indecies
var indexDEC = string.indexOf("º"),
indexSEC = string.indexOf("''"),
indexMIN = string.indexOf("'");
if (indexMIN == indexSEC)
indexMIN = -1;
//Parsing the string
if (indexDEC < 0) dec = 0.0;
else dec = parseFloat(string.slice(0, indexDEC));
if (indexMIN < 0) min = 0.0;
else min = parseFloat(string.slice(indexDEC + 1, indexMIN));
if (indexSEC < 0) sec = 0.0;
else if (indexMIN < 0) sec = parseFloat(string.slice(indexDEC + 1, indexSEC));
else sec = parseFloat(string.slice(indexMIN + 1, indexSEC));
//Returning the values
if ((indexDEC < 0) && (indexMIN < 0) && (indexSEC < 0))
return parseFloat(string);
else
return dec + min / 60.0 + sec / 3600.0;
}
/*Coverter for latitude*/
function dms2ddLAT(string) {
//searching seconds
var index = string.indexOf("''") + 2;
if (index < 2) {
//searching minutes
index = string.indexOf("'") + 1;
if (index < 1) {
//searching degrees
index = string.indexOf("º") + 1;
if (index < 1) {
//searching hemisphere letter
index = string.indexOf("N") + 1;
if (index < 1) {
//searching hemisphere letter
index = string.indexOf("S") + 1;
if (index < 1) {
//only number passed
index = string.length;
}
}
}
}
}
// Getting decimal degrees
var decdeg = dms2dd(string.slice(0, index));
// Applying correct sign
if (string.search(" S") > 0) {
decdeg *= -1;
}
return decdeg;
}
/*Coverter for longitude*/
function dms2ddLON(string) {
//searching seconds
var index = string.indexOf("''") + 2;
if (index < 2) {
//searching minutes
index = string.indexOf("'") + 1;
if (index < 1) {
//searching degrees
index = string.indexOf("º") + 1;
if (index < 1) {
//searching hemisphere letter
index = string.indexOf("E") + 1;
if (index < 1) {
//searching hemisphere letter
index = string.indexOf("W") + 1;
if (index < 1) {
//only number passed
index = string.length;
}
}
}
}
}
// Getting decimal degrees
var decdeg = dms2dd(string.slice(0, index));
// Applying correct sign
if (string.search(" W") > 0) {
decdeg *= -1;
}
return decdeg;
}