geo-coordinates-parser 1.7.0 → 1.7.2

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,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,51 @@ 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
+ let dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
30
+ let dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0');
31
+ if (dmMinsLatitude.endsWith('.000') && dmMinsLongitude.endsWith('.000')) {
32
+ dmMinsLatitude = dmMinsLatitude.replace(/\.000$/, '');
33
+ dmMinsLongitude = dmMinsLongitude.replace(/\.000$/, '');
34
+ }
35
+ result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`;
36
+ }
37
+ if (format == "DMS") {
38
+ const latMinutes = Math.floor(minutesLatitudeNotTruncated);
39
+ const longMinutes = Math.floor(minutesLongitudeNotTruncated);
40
+ let latSeconds = ((minutesLatitudeNotTruncated - latMinutes) * 60).toFixed(1).padStart(4, '0');
41
+ let longSeconds = ((minutesLongitudeNotTruncated - longMinutes) * 60).toFixed(1).padStart(4, '0');
42
+ const latMinutesString = latMinutes.toString().padStart(2, '0');
43
+ const longMinutesString = longMinutes.toString().padStart(2, '0');
44
+ // if they both end in .0 we drop the .0
45
+ if (latSeconds.endsWith('.0') && longSeconds.endsWith('.0')) {
46
+ latSeconds = latSeconds.replace(/\.0$/, '');
47
+ longSeconds = longSeconds.replace(/\.0$/, '');
48
+ }
49
+ result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`;
21
50
  }
22
- const latDirection = parts[0] >= 0 ? " N" : " S";
23
- const longDirection = parts[1] >= 0 ? " E" : " W";
24
- return `${convertedLat + latDirection}, ${convertedLong + longDirection}`;
51
+ return result;
25
52
  }
26
53
  else {
27
54
  throw new Error('no decimal coordinates to convert');
28
55
  }
29
56
  }
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
57
  function round(num, places) {
46
58
  const d = Math.pow(10, places);
47
59
  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.7.0",
3
+ "version": "1.7.2",
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",