geo-coordinates-parser 1.5.4 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
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\s+/g, ' ').trim(); //just to tidy up whitespaces
19
+ coordsString = coordsString.replace(/\s+/g, ' ').trim(); //just to tidy up whitespaces
19
20
 
20
21
  var ddLat = null;
21
22
  var ddLng = null;
@@ -78,7 +79,7 @@ function converter(coordsString, decimalPlaces) {
78
79
  }
79
80
 
80
81
  if (match[6]){
81
- ddLat += match[6]/3600;
82
+ ddLat += match[6].replace(',', '.')/3600;
82
83
  }
83
84
 
84
85
  if (parseInt(match[2]) < 0) {
@@ -92,7 +93,7 @@ function converter(coordsString, decimalPlaces) {
92
93
  }
93
94
 
94
95
  if (match[13]) {
95
- ddLng += match[13]/3600;
96
+ ddLng += match[13].replace(',', '.')/3600;
96
97
  }
97
98
 
98
99
  if (parseInt(match[9]) < 0) {
@@ -107,11 +108,10 @@ function converter(coordsString, decimalPlaces) {
107
108
  else if (match[7]) {
108
109
  latdir = match[7];
109
110
  lngdir = match[14];
110
- }
111
-
111
+ }
112
112
  }
113
113
  else {
114
- throw new Error("invalid DMS coordinates format")
114
+ throw new Error("invalid DMS coordinates format")
115
115
  }
116
116
  }
117
117
  else if (dms_abbr.test(coordsString)) {
@@ -122,16 +122,10 @@ function converter(coordsString, decimalPlaces) {
122
122
  ddLat = Math.abs(parseInt(match[2]));
123
123
  if (match[4]) {
124
124
  ddLat += match[4]/60;
125
- if(!match[3]) {
126
- match[3] = ' ';
127
- }
128
125
  }
129
126
 
130
127
  if (match[6]) {
131
128
  ddLat += match[6]/3600;
132
- if(!match[5]) {
133
- match[5] = ' ';
134
- }
135
129
  }
136
130
 
137
131
  if (parseInt(match[2]) < 0) {
@@ -142,16 +136,10 @@ function converter(coordsString, decimalPlaces) {
142
136
 
143
137
  if (match[12]) {
144
138
  ddLng += match[12]/60;
145
- if(!match[11]) {
146
- match[11] = ' ';
147
- }
148
139
  }
149
140
 
150
141
  if (match[14]) {
151
142
  ddLng += match[14]/3600;
152
- if(!match[13]) {
153
- match[13] = ' ';
154
- }
155
143
  }
156
144
 
157
145
  if (parseInt(match[10]) < 0) {
@@ -180,16 +168,10 @@ function converter(coordsString, decimalPlaces) {
180
168
  ddLat = Math.abs(parseInt(match[2]));
181
169
  if (match[4]){
182
170
  ddLat += match[4]/60;
183
- if(!match[3]) {
184
- match[3] = ' ';
185
- }
186
171
  }
187
172
 
188
173
  if (match[6]) {
189
174
  ddLat += match[6]/3600;
190
- if(!match[5]) {
191
- match[5] = ' ';
192
- }
193
175
  }
194
176
 
195
177
  if (parseInt(match[2]) < 0) {
@@ -199,16 +181,10 @@ function converter(coordsString, decimalPlaces) {
199
181
  ddLng = Math.abs(parseInt(match[10]));
200
182
  if (match[12]) {
201
183
  ddLng += match[12]/60;
202
- if(!match[11]) {
203
- match[11] = ' ';
204
- }
205
184
  }
206
185
 
207
186
  if (match[14]) {
208
187
  ddLng += match[14]/3600;
209
- if(!match[13]) {
210
- match[13] = ' ';
211
- }
212
188
  }
213
189
 
214
190
  if (parseInt(match[10]) < 0) {
@@ -229,18 +205,30 @@ function converter(coordsString, decimalPlaces) {
229
205
  }
230
206
  }
231
207
 
232
- //check longitude value - it can be wrong!
233
- if (Math.abs(ddLng) >= 180) {
234
- throw new Error("invalid longitude value")
235
- }
236
-
237
- //just to be safe check latitude also...
238
- if (Math.abs(ddLat) >= 90) {
239
- throw new Error("invalid latitude value")
240
- }
241
-
242
208
  if (matchSuccess){
243
-
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
+
244
232
  //make sure the signs and cardinal directions match
245
233
  var patt = /S|SOUTH/i;
246
234
  if (patt.test(latdir)) {
@@ -256,7 +244,6 @@ function converter(coordsString, decimalPlaces) {
256
244
  }
257
245
  }
258
246
 
259
-
260
247
  //we need to get the verbatim coords from the string
261
248
  //we can't split down the middle because if there are decimals they may have different numbers on each side
262
249
  //so we need to find the separating character, or if none, use the match values to split down the middle
@@ -359,7 +346,7 @@ function checkMatch(match) { //test if the matched groups arrays are 'balanced'.
359
346
  }
360
347
 
361
348
  //regex for testing corresponding values match
362
- var numerictest = /^[-+]?\d+([\.,]{1}\d+)?$/; //for testing numeric values
349
+ var numerictest = /^[-+]?\d+([\.,]\d+)?$/; //for testing numeric values
363
350
  var stringtest = /[eastsouthnorthwest]+/i; //for testing string values (north, south, etc)
364
351
 
365
352
 
@@ -414,13 +401,13 @@ function coordsCloseEnough(coordsToTest) {
414
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;
415
402
 
416
403
  //degrees minutes seconds with '.' as separator - gives array with 15 values
417
- var dms_periods = /(NORTH|SOUTH|[NS])?[\ \t]*([+-]?[0-8]?[0-9])[\ \t]*(\.)[\ \t]*([0-5]?[0-9])[\ \t]*(\.)?[\ \t]*((?:[0-5]?[0-9])(?:\.\d{1,3})?)?\s*(NORTH|SOUTH|[NS])?(?:[\ \t]*[,/;][\ \t]*|[\ \t]*)(EAST|WEST|[EW])?[\ \t]*([+-]?[0-1]?[0-9]?[0-9])[\ \t]*(\.)[\ \t]*([0-5]?[0-9])[\ \t]*(\.)?[\ \t]*((?:[0-5]?[0-9])(?:\.\d{1,3})?)?\s*(EAST|WEST|[EW])?/i;
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;
418
405
 
419
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
420
- var dms_abbr = /(NORTH|SOUTH|[NS])?[\ \t]*([+-]?[0-8]?[0-9])[\ \t]*(D(?:EG)?(?:REES)?)[\ \t]*([0-5]?[0-9])[\ \t]*(M(?:IN)?(?:UTES)?)[\ \t]*((?:[0-5]?[0-9])(?:\.\d{1,3})?)?(S(?:EC)?(?:ONDS)?)?[\ \t]*(NORTH|SOUTH|[NS])?(?:[\ \t]*[,/;][\ \t]*|[\ \t]*)(EAST|WEST|[EW])?[\ \t]*([+-]?[0-1]?[0-9]?[0-9])[\ \t]*(D(?:EG)?(?:REES)?)[\ \t]*([0-5]?[0-9])[\ \t]*(M(?:IN)?(?:UTES)?)[\ \t]*((?:[0-5]?[0-9])(?:\.\d{1,3})?)?(S(?:EC)?(?:ONDS)?)[\ \t]*(EAST|WEST|[EW])?/i;
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;
421
408
 
422
409
  //everything else - gives array of 17 values
423
- var coords_other = /(NORTH|SOUTH|[NS])?[\ \t]*([+-]?[0-8]?[0-9])[\ \t]*([•º°\.:]|D(?:EG)?(?:REES)?)?[\ \t]*,?([0-5]?[0-9](?:\.\d{1,})?)?[\ \t]*(['′´’\.:]|M(?:IN)?(?:UTES)?)?[\ \t]*,?((?:[0-5]?[0-9])(?:\.\d{1,3})?)?[\ \t]*(''|′′|’’|´´|["″”\.])?[\ \t]*(NORTH|SOUTH|[NS])?(?:\s*[,/;]\s*|\s*)(EAST|WEST|[EW])?[\ \t]*([+-]?[0-1]?[0-9]?[0-9])[\ \t]*([•º°\.:]|D(?:EG)?(?:REES)?)?[\ \t]*,?([0-5]?[0-9](?:\.\d{1,})?)?[\ \t]*(['′´’\.:]|M(?:IN)?(?:UTES)?)?[\ \t]*,?((?:[0-5]?[0-9])(?:\.\d{1,3})?)?[\ \t]*(''|′′|´´|’’|["″”\.])?[\ \t]*(EAST|WEST|[EW])?/i;
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;
424
411
 
425
412
  const to = Object.freeze({
426
413
  DMS: 'DMS',
package/failFormats.js CHANGED
@@ -7,6 +7,11 @@ const failingFormats = [
7
7
  '50°4\'17.698"east, 23°4\'17.698"south', //directions wrong way round
8
8
  'E23.34355,S25.324234', // directions wrong way round
9
9
  '23°45\'12.2\'\'S 18.33\'56.7\'\'E', //symbols don't match
10
+ 'S 27.45.34 23.23.23', //missing direction on right side
11
+ 'S 27.45.34 S 23.23.23', //invalid direction on right side
12
+ 'S 90°4\'17.698" S 23°4\'17.698"',
13
+ '27.45.34 S S 23.23.23', //invalid direction on right side
14
+ '27.45.34 23.23.23 E' //no dir on one side
10
15
  ]
11
16
 
12
17
  module.exports = failingFormats
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.5.4",
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
@@ -1,5 +1,5 @@
1
1
  const convert = require('./converter')
2
- const test = '27.45.34 S 23.23.23 E'
2
+ const test = '27.45.34 S S 23.23.23'
3
3
 
4
4
  try{
5
5
  let converted = convert(test)
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,27 @@ 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
289
+ },
290
+ {
291
+ verbatimCoordinates: '27.15.45.2S 18.32.53.4E',
292
+ verbatimLatitude: '27.15.45.2S',
293
+ verbatimLongitude: '18.32.53.4E',
294
+ decimalLatitude: -27.262556,
295
+ decimalLongitude: 18.548167
296
+ },
297
+ {
298
+ verbatimCoordinates: '27.15.45,2S 18.32.53,4E',
299
+ verbatimLatitude: '27.15.45,2S',
300
+ verbatimLongitude: '18.32.53,4E',
301
+ decimalLatitude: -27.262556,
302
+ decimalLongitude: 18.548167
275
303
  },
276
304
  {
277
305
  verbatimCoordinates: 'S23.43563 ° E22.45634 °', //decimals with spaces before the symbol!!