geo-coordinates-parser 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- package/converter.js +93 -49
- package/failFormats.js +2 -4
- package/package.json +1 -1
- package/testformats.js +8 -1
package/converter.js
CHANGED
@@ -38,12 +38,24 @@ function converter(coordsString, decimalPlaces) {
|
|
38
38
|
if(ddLng.includes(',')) {
|
39
39
|
ddLng = ddLng.replace(',', '.');
|
40
40
|
}
|
41
|
+
|
42
|
+
//validation, we don't want things like 23.00000
|
43
|
+
|
44
|
+
//some more validation: no zero coords or degrees only
|
45
|
+
if (Number(Math.round(ddLat)) == Number(ddLat)) {
|
46
|
+
throw new Error('degree only coordinate provided')
|
47
|
+
}
|
48
|
+
|
49
|
+
if (Number(Math.round(ddLng)) == Number(ddLng)) {
|
50
|
+
throw new Error('degree only coordinate provided')
|
51
|
+
}
|
41
52
|
|
42
53
|
//get directions
|
43
|
-
if(match[1]){
|
54
|
+
if(match[1]) {
|
44
55
|
latdir = match[1];
|
45
56
|
lngdir = match[5];
|
46
|
-
}
|
57
|
+
}
|
58
|
+
else if (match[4]) {
|
47
59
|
latdir = match[4];
|
48
60
|
lngdir = match[8];
|
49
61
|
}
|
@@ -58,25 +70,41 @@ function converter(coordsString, decimalPlaces) {
|
|
58
70
|
match = dms_periods.exec(coordsString);
|
59
71
|
matchSuccess = checkMatch(match);
|
60
72
|
if (matchSuccess){
|
73
|
+
|
61
74
|
ddLat = Math.abs(parseInt(match[2]));
|
62
|
-
|
75
|
+
|
76
|
+
if (match[4]) {
|
63
77
|
ddLat += match[4]/60;
|
64
|
-
|
78
|
+
}
|
79
|
+
|
80
|
+
if (match[6]){
|
65
81
|
ddLat += match[6]/3600;
|
66
|
-
|
82
|
+
}
|
83
|
+
|
84
|
+
if (parseInt(match[2]) < 0) {
|
67
85
|
ddLat = -1 * ddLat;
|
86
|
+
}
|
87
|
+
|
68
88
|
ddLng = Math.abs(parseInt(match[9]));
|
69
|
-
|
89
|
+
|
90
|
+
if (match[11]) {
|
70
91
|
ddLng += match[11]/60;
|
71
|
-
|
92
|
+
}
|
93
|
+
|
94
|
+
if (match[13]) {
|
72
95
|
ddLng += match[13]/3600;
|
73
|
-
|
96
|
+
}
|
97
|
+
|
98
|
+
if (parseInt(match[9]) < 0) {
|
74
99
|
ddLng = -1 * ddLng;
|
75
|
-
|
76
|
-
|
100
|
+
}
|
101
|
+
|
102
|
+
//the compass directions
|
103
|
+
if(match[1]) {
|
77
104
|
latdir = match[1];
|
78
105
|
lngdir = match[8];
|
79
|
-
}
|
106
|
+
}
|
107
|
+
else if (match[7]) {
|
80
108
|
latdir = match[7];
|
81
109
|
lngdir = match[14];
|
82
110
|
}
|
@@ -89,39 +117,52 @@ function converter(coordsString, decimalPlaces) {
|
|
89
117
|
else if (dms_abbr.test(coordsString)) {
|
90
118
|
match = dms_abbr.exec(coordsString);
|
91
119
|
matchSuccess = checkMatch(match);
|
92
|
-
|
120
|
+
|
121
|
+
if (matchSuccess) {
|
93
122
|
ddLat = Math.abs(parseInt(match[2]));
|
94
|
-
if (match[4]){
|
123
|
+
if (match[4]) {
|
95
124
|
ddLat += match[4]/60;
|
96
|
-
if(!match[3])
|
125
|
+
if(!match[3]) {
|
97
126
|
match[3] = ' ';
|
127
|
+
}
|
98
128
|
}
|
129
|
+
|
99
130
|
if (match[6]) {
|
100
131
|
ddLat += match[6]/3600;
|
101
|
-
if(!match[5])
|
132
|
+
if(!match[5]) {
|
102
133
|
match[5] = ' ';
|
134
|
+
}
|
103
135
|
}
|
104
|
-
|
136
|
+
|
137
|
+
if (parseInt(match[2]) < 0) {
|
105
138
|
ddLat = -1 * ddLat;
|
139
|
+
}
|
140
|
+
|
106
141
|
ddLng = Math.abs(parseInt(match[10]));
|
107
|
-
|
142
|
+
|
143
|
+
if (match[12]) {
|
108
144
|
ddLng += match[12]/60;
|
109
|
-
if(!match[11])
|
145
|
+
if(!match[11]) {
|
110
146
|
match[11] = ' ';
|
147
|
+
}
|
111
148
|
}
|
112
|
-
|
149
|
+
|
150
|
+
if (match[14]) {
|
113
151
|
ddLng += match[14]/3600;
|
114
|
-
if(!match[13])
|
152
|
+
if(!match[13]) {
|
115
153
|
match[13] = ' ';
|
154
|
+
}
|
116
155
|
}
|
117
|
-
|
156
|
+
|
157
|
+
if (parseInt(match[10]) < 0) {
|
118
158
|
ddLng = -1 * ddLng;
|
159
|
+
}
|
119
160
|
|
120
|
-
if(match[1]){
|
161
|
+
if(match[1]) {
|
121
162
|
latdir = match[1];
|
122
163
|
lngdir = match[9];
|
123
164
|
}
|
124
|
-
else if (match[8]){
|
165
|
+
else if (match[8]) {
|
125
166
|
latdir = match[8];
|
126
167
|
lngdir = match[16];
|
127
168
|
}
|
@@ -134,39 +175,50 @@ function converter(coordsString, decimalPlaces) {
|
|
134
175
|
else if (coords_other.test(coordsString)) {
|
135
176
|
match = coords_other.exec(coordsString);
|
136
177
|
matchSuccess = checkMatch(match);
|
137
|
-
|
178
|
+
|
179
|
+
if (matchSuccess) {
|
138
180
|
ddLat = Math.abs(parseInt(match[2]));
|
139
181
|
if (match[4]){
|
140
182
|
ddLat += match[4]/60;
|
141
|
-
if(!match[3])
|
183
|
+
if(!match[3]) {
|
142
184
|
match[3] = ' ';
|
185
|
+
}
|
143
186
|
}
|
187
|
+
|
144
188
|
if (match[6]) {
|
145
189
|
ddLat += match[6]/3600;
|
146
|
-
if(!match[5])
|
190
|
+
if(!match[5]) {
|
147
191
|
match[5] = ' ';
|
192
|
+
}
|
148
193
|
}
|
149
|
-
|
194
|
+
|
195
|
+
if (parseInt(match[2]) < 0) {
|
150
196
|
ddLat = -1 * ddLat;
|
197
|
+
}
|
151
198
|
|
152
199
|
ddLng = Math.abs(parseInt(match[10]));
|
153
|
-
if (match[12]){
|
200
|
+
if (match[12]) {
|
154
201
|
ddLng += match[12]/60;
|
155
|
-
if(!match[11])
|
202
|
+
if(!match[11]) {
|
156
203
|
match[11] = ' ';
|
204
|
+
}
|
157
205
|
}
|
158
|
-
|
206
|
+
|
207
|
+
if (match[14]) {
|
159
208
|
ddLng += match[14]/3600;
|
160
|
-
if(!match[13])
|
209
|
+
if(!match[13]) {
|
161
210
|
match[13] = ' ';
|
211
|
+
}
|
162
212
|
}
|
163
|
-
|
213
|
+
|
214
|
+
if (parseInt(match[10]) < 0) {
|
164
215
|
ddLng = -1 * ddLng;
|
165
|
-
|
166
|
-
|
216
|
+
}
|
217
|
+
|
218
|
+
if(match[1]) {
|
167
219
|
latdir = match[1];
|
168
220
|
lngdir = match[9];
|
169
|
-
} else if (match[8]){
|
221
|
+
} else if (match[8]) {
|
170
222
|
latdir = match[8];
|
171
223
|
lngdir = match[16];
|
172
224
|
}
|
@@ -177,23 +229,15 @@ function converter(coordsString, decimalPlaces) {
|
|
177
229
|
}
|
178
230
|
}
|
179
231
|
|
180
|
-
if (ddLat == 0.0 || ddLng == 0.0) {
|
181
|
-
throw new Error('zero coordinate/s provided')
|
182
|
-
}
|
183
|
-
|
184
|
-
//some more validation: no zero coords or degrees only
|
185
|
-
if (Number(Math.round(ddLat)) == Number(ddLat)) {
|
186
|
-
throw new Error('degree only coordinate provided')
|
187
|
-
}
|
188
|
-
|
189
|
-
if (Number(Math.round(ddLng)) == Number(ddLng)) {
|
190
|
-
throw new Error('degree only coordinate provided')
|
191
|
-
}
|
192
|
-
|
193
232
|
//check longitude value - it can be wrong!
|
194
|
-
if (Math.abs(ddLng) >=180) {
|
233
|
+
if (Math.abs(ddLng) >= 180) {
|
195
234
|
throw new Error("invalid longitude value")
|
196
235
|
}
|
236
|
+
|
237
|
+
//just to be safe check latitude also...
|
238
|
+
if (Math.abs(ddLng) >= 90) {
|
239
|
+
throw new Error("invalid latitude value")
|
240
|
+
}
|
197
241
|
|
198
242
|
if (matchSuccess){
|
199
243
|
|
package/failFormats.js
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
//TODO These formats should throw...
|
2
2
|
|
3
3
|
const failingFormats = [
|
4
|
-
'N 52d12m42s E 000d0m0s', //degree only coordinate and zero coordinate
|
5
|
-
'N49 0.000 E02 33.314', //degree only coordinate
|
6
|
-
'0,0', //another zero coordinate, numbers only
|
7
|
-
'0, 24.34532', //zero coordinate on one size only
|
8
4
|
'50°4\'17.698"south, 24.34532', //different formats on each side
|
5
|
+
'90°4\'17.698"south, 23°4\'17.698"east',
|
6
|
+
'50°4\'17.698"east, 23°4\'17.698"south',
|
9
7
|
]
|
10
8
|
|
11
9
|
module.exports = failingFormats
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "geo-coordinates-parser",
|
3
|
-
"version": "1.5.
|
3
|
+
"version": "1.5.1",
|
4
4
|
"description": "A Javascript function for reading a variety of coordinate formats and converting to decimal numbers. Builds on other efforts by returning the verbatim coordinates and the decimal coordinates all in one object.",
|
5
5
|
"main": "merge.js",
|
6
6
|
"scripts": {
|
package/testformats.js
CHANGED
@@ -225,7 +225,14 @@ var coordsRegexFormats = [
|
|
225
225
|
verbatimLongitude: '-74.123256',
|
226
226
|
decimalLatitude: 40.123256,
|
227
227
|
decimalLongitude: -74.123256
|
228
|
-
}
|
228
|
+
},
|
229
|
+
{
|
230
|
+
verbatimCoordinates: '18°24S 22°45E', //this is read as degrees and minutes
|
231
|
+
verbatimLatitude: '18°24S',
|
232
|
+
verbatimLongitude: '22°45E',
|
233
|
+
decimalLatitude: -18.4,
|
234
|
+
decimalLongitude: 22.75
|
235
|
+
}
|
229
236
|
]
|
230
237
|
|
231
238
|
var otherFormats = [
|