geo-coordinates-parser 1.6.6 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,10 @@
1
1
  import convert from '../converter.js';
2
2
  //const test = '26°44S 29°46E'
3
3
  //const test = '00.00, 01.00'
4
- const test = `26°05240'S, 27°51448'E`;
4
+ //const test = `45°5'46.8134"N, 18°30'36.7124"E`
5
+ //const test = `50 8.2914,-5 2.4447`
6
+ //const test = `8°83S 35°67E`
7
+ const test = `N 48° 30,6410', E 18° 57,4583'`;
5
8
  try {
6
9
  let converted = convert(test);
7
10
  console.log(converted);
@@ -234,8 +234,8 @@ const coordsRegexFormats = [
234
234
  decimalLongitude: 22.75
235
235
  }
236
236
  ];
237
+ // additional formats we've encountered
237
238
  const otherFormats = [
238
- // additional formats we've encountered
239
239
  {
240
240
  verbatimCoordinates: '10.432342S 10.6345345E',
241
241
  verbatimLatitude: '10.432342S',
@@ -340,6 +340,27 @@ const otherFormats = [
340
340
  verbatimLongitude: 'E 23.23.23',
341
341
  decimalLatitude: -27.759444,
342
342
  decimalLongitude: 23.38972222
343
+ },
344
+ {
345
+ verbatimCoordinates: '53 16.3863,4 52.8171',
346
+ verbatimLatitude: '53 16.3863',
347
+ verbatimLongitude: '4 52.8171',
348
+ decimalLatitude: 53.273105,
349
+ decimalLongitude: 4.88029
350
+ },
351
+ {
352
+ verbatimCoordinates: '50 8.2914,-5 2.4447',
353
+ verbatimLatitude: '50 8.2914',
354
+ verbatimLongitude: '-5 2.4447',
355
+ decimalLatitude: 50.13819,
356
+ decimalLongitude: -5.040745
357
+ },
358
+ {
359
+ verbatimCoordinates: `N 48° 30,6410', E 18° 57,4583'`,
360
+ verbatimLatitude: `N 48° 30,6410'`,
361
+ verbatimLongitude: `E 18° 57,4583'`,
362
+ decimalLatitude: 48.51068,
363
+ decimalLongitude: 18.95764
343
364
  }
344
365
  ];
345
366
  function getAllTestFormats() {
@@ -1,6 +1,7 @@
1
1
  export default toCoordinateFormat;
2
2
  /**
3
- * Converts decimalCoordinates to other formats commonly used
3
+ * Converts decimalCoordinates to commonly used string formats
4
+ * Note that this will add degree and direction symbols to decimal coordinates
4
5
  * @param {string} format Either DMS or DM
5
6
  * @returns {string}
6
7
  */
@@ -1,6 +1,7 @@
1
1
  //borrowed from https://www.codegrepper.com/code-examples/javascript/javascript+converting+latitude+longitude+to+gps+coordinates
2
2
  /**
3
- * Converts decimalCoordinates to other formats commonly used
3
+ * Converts decimalCoordinates to commonly used string formats
4
+ * Note that this will add degree and direction symbols to decimal coordinates
4
5
  * @param {string} format Either DMS or DM
5
6
  * @returns {string}
6
7
  */
@@ -8,40 +9,47 @@ function toCoordinateFormat(format) {
8
9
  if (!['DMS', 'DM', 'DD'].includes(format))
9
10
  throw new Error('invalid format specified');
10
11
  if (this.decimalCoordinates && this.decimalCoordinates.trim()) {
12
+ const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim()));
13
+ const decimalLatitude = Number(parts[0]);
14
+ const decimalLongitude = Number(parts[1]);
15
+ const absoluteLatitude = Math.abs(decimalLatitude);
16
+ const absoluteLongitude = Math.abs(decimalLongitude);
17
+ const latDir = decimalLatitude > 0 ? "N" : "S";
18
+ const longDir = decimalLongitude > 0 ? "E" : "W";
19
+ let result;
11
20
  if (format == 'DD') {
12
- return this.decimalCoordinates;
21
+ result = `${absoluteLatitude}° ${latDir}, ${absoluteLongitude}° ${longDir}`;
13
22
  }
14
- const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim()));
15
- let convertedLat = convert(parts[0], format, true);
16
- let convertedLong = convert(parts[1], format, false);
17
- //some custom cleaning for DMS
18
- if (convertedLat.endsWith('.0"') && convertedLong.endsWith('.0"')) {
19
- convertedLat = convertedLat.replace(/\.0"$/, '"');
20
- convertedLong = convertedLong.replace(/\.0"$/, '"');
23
+ //else we need some more things
24
+ const degreesLatitude = Math.floor(absoluteLatitude);
25
+ const degreesLongitude = Math.floor(absoluteLongitude);
26
+ const minutesLatitudeNotTruncated = (absoluteLatitude - degreesLatitude) * 60;
27
+ const minutesLongitudeNotTruncated = (absoluteLongitude - degreesLongitude) * 60;
28
+ if (format == 'DM') {
29
+ const dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
30
+ const dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
31
+ result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`;
32
+ }
33
+ if (format == "DMS") {
34
+ const latMinutes = Math.floor(minutesLatitudeNotTruncated);
35
+ const longMinutes = Math.floor(minutesLongitudeNotTruncated);
36
+ let latSeconds = ((minutesLatitudeNotTruncated - latMinutes) * 60).toFixed(1).padStart(4, '0');
37
+ let longSeconds = ((minutesLongitudeNotTruncated - longMinutes) * 60).toFixed(1).padStart(4, '0');
38
+ const latMinutesString = latMinutes.toString().padStart(2, '0');
39
+ const longMinutesString = longMinutes.toString().padStart(2, '0');
40
+ // if they both end in .0 we drop the .0
41
+ if (latSeconds.endsWith('.0"') && longSeconds.endsWith('.0"')) {
42
+ latSeconds = latSeconds.replace(/\.0"$/, '"');
43
+ longSeconds = longSeconds.replace(/\.0"$/, '"');
44
+ }
45
+ result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`;
21
46
  }
22
- const latDirection = parts[0] >= 0 ? " N" : " S";
23
- const longDirection = parts[1] >= 0 ? " E" : " W";
24
- return `${convertedLat + latDirection}, ${convertedLong + longDirection}`;
47
+ return result;
25
48
  }
26
49
  else {
27
50
  throw new Error('no decimal coordinates to convert');
28
51
  }
29
52
  }
30
- //assumes everything is valid...
31
- function convert(coord, format) {
32
- const absolute = Math.abs(coord);
33
- const degrees = Math.floor(absolute);
34
- const minutesNotTruncated = (absolute - degrees) * 60;
35
- if (format == 'DM') {
36
- let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0');
37
- return `${degrees}° ${dmMins}'`;
38
- }
39
- //else
40
- let minutes = Math.floor(minutesNotTruncated);
41
- let seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).padStart(4, '0');
42
- minutes = minutes.toString().padStart(2, '0');
43
- return `${degrees}° ${minutes}' ${seconds}"`;
44
- }
45
53
  function round(num, places) {
46
54
  const d = Math.pow(10, places);
47
55
  return Math.round((num + Number.EPSILON) * d) / d;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.6.6",
3
+ "version": "1.7.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
  "type": "module",
6
6
  "main": "./dist/cjs/merge.js",
@@ -12,6 +12,7 @@
12
12
  }
13
13
  },
14
14
  "scripts": {
15
+ "prepack": "npm run build",
15
16
  "build": "rd /s /q dist && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && fixup.bat",
16
17
  "test": "node test.js"
17
18
  },