geo-coordinates-parser 1.5.6 → 1.5.7
Sign up to get free protection for your applications and to get access to all the features.
- package/converter.js +30 -51
- package/package.json +1 -1
- package/testFormats.json +35 -0
- package/testIndividual.js +1 -1
- package/testformats.js +14 -0
package/converter.js
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
//function for converting coordinates from a string to decimal and verbatim
|
2
|
+
//this is just a comment
|
2
3
|
|
3
4
|
const toCoordinateFormat = require('./toCoordinateFormat.js')
|
4
5
|
|
@@ -15,7 +16,7 @@ function converter(coordsString, decimalPlaces) {
|
|
15
16
|
decimalPlaces = 5
|
16
17
|
}
|
17
18
|
|
18
|
-
coordsString = coordsString.replace(/\s
|
19
|
+
coordsString = coordsString.replace(/\s+/g, ' ').trim(); //just to tidy up whitespaces
|
19
20
|
|
20
21
|
var ddLat = null;
|
21
22
|
var ddLng = null;
|
@@ -107,16 +108,7 @@ function converter(coordsString, decimalPlaces) {
|
|
107
108
|
else if (match[7]) {
|
108
109
|
latdir = match[7];
|
109
110
|
lngdir = match[14];
|
110
|
-
}
|
111
|
-
else { //we have to catch an edge case where we have no direction indicators
|
112
|
-
throw new Error("invalid DMS coordinates format")
|
113
|
-
}
|
114
|
-
|
115
|
-
//we have to catch another edge case here, same or missing direction indicators
|
116
|
-
if(!latdir || !lngdir) {
|
117
|
-
throw new Error("invalid DMS coordinates format")
|
118
|
-
}
|
119
|
-
|
111
|
+
}
|
120
112
|
}
|
121
113
|
else {
|
122
114
|
throw new Error("invalid DMS coordinates format")
|
@@ -130,16 +122,10 @@ function converter(coordsString, decimalPlaces) {
|
|
130
122
|
ddLat = Math.abs(parseInt(match[2]));
|
131
123
|
if (match[4]) {
|
132
124
|
ddLat += match[4]/60;
|
133
|
-
if(!match[3]) {
|
134
|
-
match[3] = ' ';
|
135
|
-
}
|
136
125
|
}
|
137
126
|
|
138
127
|
if (match[6]) {
|
139
128
|
ddLat += match[6]/3600;
|
140
|
-
if(!match[5]) {
|
141
|
-
match[5] = ' ';
|
142
|
-
}
|
143
129
|
}
|
144
130
|
|
145
131
|
if (parseInt(match[2]) < 0) {
|
@@ -150,16 +136,10 @@ function converter(coordsString, decimalPlaces) {
|
|
150
136
|
|
151
137
|
if (match[12]) {
|
152
138
|
ddLng += match[12]/60;
|
153
|
-
if(!match[11]) {
|
154
|
-
match[11] = ' ';
|
155
|
-
}
|
156
139
|
}
|
157
140
|
|
158
141
|
if (match[14]) {
|
159
142
|
ddLng += match[14]/3600;
|
160
|
-
if(!match[13]) {
|
161
|
-
match[13] = ' ';
|
162
|
-
}
|
163
143
|
}
|
164
144
|
|
165
145
|
if (parseInt(match[10]) < 0) {
|
@@ -188,16 +168,10 @@ function converter(coordsString, decimalPlaces) {
|
|
188
168
|
ddLat = Math.abs(parseInt(match[2]));
|
189
169
|
if (match[4]){
|
190
170
|
ddLat += match[4]/60;
|
191
|
-
if(!match[3]) {
|
192
|
-
match[3] = ' ';
|
193
|
-
}
|
194
171
|
}
|
195
172
|
|
196
173
|
if (match[6]) {
|
197
174
|
ddLat += match[6]/3600;
|
198
|
-
if(!match[5]) {
|
199
|
-
match[5] = ' ';
|
200
|
-
}
|
201
175
|
}
|
202
176
|
|
203
177
|
if (parseInt(match[2]) < 0) {
|
@@ -207,16 +181,10 @@ function converter(coordsString, decimalPlaces) {
|
|
207
181
|
ddLng = Math.abs(parseInt(match[10]));
|
208
182
|
if (match[12]) {
|
209
183
|
ddLng += match[12]/60;
|
210
|
-
if(!match[11]) {
|
211
|
-
match[11] = ' ';
|
212
|
-
}
|
213
184
|
}
|
214
185
|
|
215
186
|
if (match[14]) {
|
216
187
|
ddLng += match[14]/3600;
|
217
|
-
if(!match[13]) {
|
218
|
-
match[13] = ' ';
|
219
|
-
}
|
220
188
|
}
|
221
189
|
|
222
190
|
if (parseInt(match[10]) < 0) {
|
@@ -237,18 +205,30 @@ function converter(coordsString, decimalPlaces) {
|
|
237
205
|
}
|
238
206
|
}
|
239
207
|
|
240
|
-
//check longitude value - it can be wrong!
|
241
|
-
if (Math.abs(ddLng) >= 180) {
|
242
|
-
throw new Error("invalid longitude value")
|
243
|
-
}
|
244
|
-
|
245
|
-
//just to be safe check latitude also...
|
246
|
-
if (Math.abs(ddLat) >= 90) {
|
247
|
-
throw new Error("invalid latitude value")
|
248
|
-
}
|
249
|
-
|
250
208
|
if (matchSuccess){
|
251
|
-
|
209
|
+
|
210
|
+
//more validation....
|
211
|
+
|
212
|
+
//check longitude value - it can be wrong!
|
213
|
+
if (Math.abs(ddLng) >= 180) {
|
214
|
+
throw new Error("invalid longitude value")
|
215
|
+
}
|
216
|
+
|
217
|
+
//just to be safe check latitude also...
|
218
|
+
if (Math.abs(ddLat) >= 90) {
|
219
|
+
throw new Error("invalid latitude value")
|
220
|
+
}
|
221
|
+
|
222
|
+
//if we have one direction we must have the other
|
223
|
+
if((latdir || lngdir) && (!latdir || !lngdir)) {
|
224
|
+
throw new Error("invalid coordinates format")
|
225
|
+
}
|
226
|
+
|
227
|
+
//the directions can't be the same
|
228
|
+
if(latdir && latdir == lngdir) {
|
229
|
+
throw new Error("invalid coordinates format")
|
230
|
+
}
|
231
|
+
|
252
232
|
//make sure the signs and cardinal directions match
|
253
233
|
var patt = /S|SOUTH/i;
|
254
234
|
if (patt.test(latdir)) {
|
@@ -264,7 +244,6 @@ function converter(coordsString, decimalPlaces) {
|
|
264
244
|
}
|
265
245
|
}
|
266
246
|
|
267
|
-
|
268
247
|
//we need to get the verbatim coords from the string
|
269
248
|
//we can't split down the middle because if there are decimals they may have different numbers on each side
|
270
249
|
//so we need to find the separating character, or if none, use the match values to split down the middle
|
@@ -367,7 +346,7 @@ function checkMatch(match) { //test if the matched groups arrays are 'balanced'.
|
|
367
346
|
}
|
368
347
|
|
369
348
|
//regex for testing corresponding values match
|
370
|
-
var numerictest = /^[-+]?\d+([\.,]
|
349
|
+
var numerictest = /^[-+]?\d+([\.,]\d+)?$/; //for testing numeric values
|
371
350
|
var stringtest = /[eastsouthnorthwest]+/i; //for testing string values (north, south, etc)
|
372
351
|
|
373
352
|
|
@@ -422,13 +401,13 @@ function coordsCloseEnough(coordsToTest) {
|
|
422
401
|
var dd_re = /(NORTH|SOUTH|[NS])?[\s]*([+-]?[0-8]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(NORTH|SOUTH|[NS])?[\s]*[,/;]?[\s]*(EAST|WEST|[EW])?[\s]*([+-]?[0-1]?[0-9]?[0-9](?:[\.,]\d{3,}))[\s]*([•º°]?)[\s]*(EAST|WEST|[EW])?/i;
|
423
402
|
|
424
403
|
//degrees minutes seconds with '.' as separator - gives array with 15 values
|
425
|
-
var dms_periods = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)
|
404
|
+
var dms_periods = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(\.)\s*([0-5]?[0-9])\s*(\.)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(EAST|WEST|[EW])?/i;
|
426
405
|
|
427
406
|
//degrees minutes seconds with words 'degrees, minutes, seconds' as separators (needed because the s of seconds messes with the S of SOUTH) - gives array of 17 values
|
428
|
-
var dms_abbr = /(NORTH|SOUTH|[NS])
|
407
|
+
var dms_abbr = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*(D(?:EG)?(?:REES)?)\s*([0-5]?[0-9])\s*(M(?:IN)?(?:UTES)?)\s*((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(S(?:EC)?(?:ONDS)?)\s*(EAST|WEST|[EW])?/i;
|
429
408
|
|
430
409
|
//everything else - gives array of 17 values
|
431
|
-
var coords_other = /(NORTH|SOUTH|[NS])
|
410
|
+
var coords_other = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|’’|´´|["″”\.])?\s*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?\s*([+-]?[0-1]?[0-9]?[0-9])\s*([•º°\.:]|D(?:EG)?(?:REES)?)?\s*,?([0-5]?[0-9](?:[\.,]\d{1,})?)?\s*(['′´’\.:]|M(?:IN)?(?:UTES)?)?\s*,?((?:[0-5]?[0-9])(?:[\.,]\d{1,3})?)?\s*(''|′′|´´|’’|["″”\.])?\s*(EAST|WEST|[EW])?/i;
|
432
411
|
|
433
412
|
const to = Object.freeze({
|
434
413
|
DMS: 'DMS',
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "geo-coordinates-parser",
|
3
|
-
"version": "1.5.
|
3
|
+
"version": "1.5.7",
|
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.json
CHANGED
@@ -132,6 +132,13 @@
|
|
132
132
|
"decimalLatitude": 40.1230555555,
|
133
133
|
"decimalLongitude": -74.12305555555555
|
134
134
|
},
|
135
|
+
{
|
136
|
+
"verbatimCoordinates": "40°7’23\"S 74°7’23\"E",
|
137
|
+
"verbatimLatitude": "40°7’23\"S",
|
138
|
+
"verbatimLongitude": "74°7’23\"E",
|
139
|
+
"decimalLatitude": -40.1230555555,
|
140
|
+
"decimalLongitude": 74.12305555555555
|
141
|
+
},
|
135
142
|
{
|
136
143
|
"verbatimCoordinates": "40°7’23\" -74°7’23\"",
|
137
144
|
"verbatimLatitude": "40°7’23\"",
|
@@ -293,6 +300,27 @@
|
|
293
300
|
"decimalLatitude": -27.2625,
|
294
301
|
"decimalLongitude": 18.548055
|
295
302
|
},
|
303
|
+
{
|
304
|
+
"verbatimCoordinates": "-27.15.45 18.32.53",
|
305
|
+
"verbatimLatitude": "-27.15.45",
|
306
|
+
"verbatimLongitude": "18.32.53",
|
307
|
+
"decimalLatitude": -27.2625,
|
308
|
+
"decimalLongitude": 18.548055
|
309
|
+
},
|
310
|
+
{
|
311
|
+
"verbatimCoordinates": "27.15.45.2S 18.32.53.4E",
|
312
|
+
"verbatimLatitude": "27.15.45.2S",
|
313
|
+
"verbatimLongitude": "18.32.53.4E",
|
314
|
+
"decimalLatitude": -27.262556,
|
315
|
+
"decimalLongitude": 18.548167
|
316
|
+
},
|
317
|
+
{
|
318
|
+
"verbatimCoordinates": "27.15.45,2S 18.32.53,4E",
|
319
|
+
"verbatimLatitude": "27.15.45,2S",
|
320
|
+
"verbatimLongitude": "18.32.53,4E",
|
321
|
+
"decimalLatitude": -27.262556,
|
322
|
+
"decimalLongitude": 18.548167
|
323
|
+
},
|
296
324
|
{
|
297
325
|
"verbatimCoordinates": "S23.43563 ° E22.45634 °",
|
298
326
|
"verbatimLatitude": "S23.43563 °",
|
@@ -313,5 +341,12 @@
|
|
313
341
|
"verbatimLongitude": "23.23.23 E",
|
314
342
|
"decimalLatitude": -27.759444,
|
315
343
|
"decimalLongitude": 23.38972222
|
344
|
+
},
|
345
|
+
{
|
346
|
+
"verbatimCoordinates": "S 27.45.34 E 23.23.23",
|
347
|
+
"verbatimLatitude": "S 27.45.34",
|
348
|
+
"verbatimLongitude": "E 23.23.23",
|
349
|
+
"decimalLatitude": -27.759444,
|
350
|
+
"decimalLongitude": 23.38972222
|
316
351
|
}
|
317
352
|
]
|
package/testIndividual.js
CHANGED
package/testformats.js
CHANGED
@@ -103,6 +103,13 @@ var coordsParserFormats = [
|
|
103
103
|
decimalLatitude: 40.1230555555,
|
104
104
|
decimalLongitude: -74.12305555555555
|
105
105
|
},
|
106
|
+
{
|
107
|
+
verbatimCoordinates: '40°7’23"S 74°7’23"E',
|
108
|
+
verbatimLatitude: '40°7’23"S',
|
109
|
+
verbatimLongitude: '74°7’23"E',
|
110
|
+
decimalLatitude: -40.1230555555,
|
111
|
+
decimalLongitude: 74.12305555555555
|
112
|
+
},
|
106
113
|
{
|
107
114
|
verbatimCoordinates: '40°7’23" -74°7’23"',
|
108
115
|
verbatimLatitude: '40°7’23"',
|
@@ -272,6 +279,13 @@ var otherFormats = [
|
|
272
279
|
verbatimLongitude: '18.32.53E',
|
273
280
|
decimalLatitude: -27.2625,
|
274
281
|
decimalLongitude: 18.548055
|
282
|
+
},
|
283
|
+
{
|
284
|
+
verbatimCoordinates: '-27.15.45 18.32.53',
|
285
|
+
verbatimLatitude: '-27.15.45',
|
286
|
+
verbatimLongitude: '18.32.53',
|
287
|
+
decimalLatitude: -27.2625,
|
288
|
+
decimalLongitude: 18.548055
|
275
289
|
},
|
276
290
|
{
|
277
291
|
verbatimCoordinates: '27.15.45.2S 18.32.53.4E',
|