geo-coordinates-parser 1.6.5 → 1.7.0

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.
package/README.md CHANGED
@@ -72,6 +72,9 @@ Formats used for testing can be be accessed with:
72
72
  convert.formats
73
73
  ```
74
74
 
75
+ **Please note that decimal precision needs to be reasonable. The maximum decimal places allowed in DMS, for example, is three (3). DMS coordinates with four decimal places in seconds are measuring position at a precision of less than
76
+ 10mm, which is way beyond what even the best professional GPS devices can provide.**
77
+
75
78
  **Please add coordinate formats that throw an error in the Github Issues.**
76
79
 
77
80
  **CAUTION!!!**
@@ -2,13 +2,21 @@ export default converter;
2
2
  /**
3
3
  * Function for converting coordinates in a variety of formats to decimal coordinates
4
4
  * @param {string} coordsString The coordinates string to convert
5
- * @param {number} decimalPlaces The number of decimal places for converted coordinates; default is 5
6
- * @returns {object} { verbatimCoordinates, decimalCoordinates, decimalLatitude, decimalLongitude }
5
+ * @param {number} [decimalPlaces] The number of decimal places for converted coordinates; default is 5
6
+ * @returns {{verbatimCoordinates: string, decimalCoordinates: string, decimalLatitude: string, decimalLongitude: string, closeEnough: function(string): boolean, toCoordinateFormat: toCoordinateFormat}}
7
7
  */
8
- declare function converter(coordsString: string, decimalPlaces: number): object;
8
+ declare function converter(coordsString: string, decimalPlaces?: number | undefined): {
9
+ verbatimCoordinates: string;
10
+ decimalCoordinates: string;
11
+ decimalLatitude: string;
12
+ decimalLongitude: string;
13
+ closeEnough: (arg0: string) => boolean;
14
+ toCoordinateFormat: typeof toCoordinateFormat;
15
+ };
9
16
  declare namespace converter {
10
17
  export { to };
11
18
  }
19
+ import toCoordinateFormat from './toCoordinateFormat.js';
12
20
  declare const to: Readonly<{
13
21
  DMS: "DMS";
14
22
  DM: "DM";
@@ -10,8 +10,8 @@ const toCoordinateFormat_js_1 = __importDefault(require("./toCoordinateFormat.js
10
10
  /**
11
11
  * Function for converting coordinates in a variety of formats to decimal coordinates
12
12
  * @param {string} coordsString The coordinates string to convert
13
- * @param {number} decimalPlaces The number of decimal places for converted coordinates; default is 5
14
- * @returns {object} { verbatimCoordinates, decimalCoordinates, decimalLatitude, decimalLongitude }
13
+ * @param {number} [decimalPlaces] The number of decimal places for converted coordinates; default is 5
14
+ * @returns {{verbatimCoordinates: string, decimalCoordinates: string, decimalLatitude: string, decimalLongitude: string, closeEnough: function(string): boolean, toCoordinateFormat: toCoordinateFormat}}
15
15
  */
16
16
  function converter(coordsString, decimalPlaces) {
17
17
  //TODO add exact match to entered string, so that it can be used to filter out superflous text around it
@@ -25,7 +25,24 @@ function converter(coordsString, decimalPlaces) {
25
25
  let lngdir = "";
26
26
  let match = [];
27
27
  let matchSuccess = false;
28
- if (regex_js_1.dd_re.test(coordsString)) {
28
+ if (regex_js_1.dm_numbers.test(coordsString)) {
29
+ match = regex_js_1.dm_numbers.exec(coordsString);
30
+ matchSuccess = checkMatch(match);
31
+ if (matchSuccess) {
32
+ ddLat = Math.abs(match[1]) + match[2] / 60;
33
+ if (Number(match[1]) < 0) {
34
+ ddLat *= -1;
35
+ }
36
+ ddLng = Math.abs(match[3]) + match[4] / 60;
37
+ if (Number(match[3]) < 0) {
38
+ ddLng *= -1;
39
+ }
40
+ }
41
+ else {
42
+ throw new Error("invalid coordinate format");
43
+ }
44
+ }
45
+ else if (regex_js_1.dd_re.test(coordsString)) {
29
46
  match = regex_js_1.dd_re.exec(coordsString);
30
47
  matchSuccess = checkMatch(match);
31
48
  if (matchSuccess) {
@@ -141,20 +158,20 @@ function converter(coordsString, decimalPlaces) {
141
158
  if (matchSuccess) {
142
159
  ddLat = Math.abs(parseInt(match[2]));
143
160
  if (match[4]) {
144
- ddLat += match[4] / 60;
161
+ ddLat += match[4].replace(',', '.') / 60;
145
162
  }
146
163
  if (match[6]) {
147
- ddLat += match[6] / 3600;
164
+ ddLat += match[6].replace(',', '.') / 3600;
148
165
  }
149
166
  if (parseInt(match[2]) < 0) {
150
167
  ddLat = -1 * ddLat;
151
168
  }
152
169
  ddLng = Math.abs(parseInt(match[10]));
153
170
  if (match[12]) {
154
- ddLng += match[12] / 60;
171
+ ddLng += match[12].replace(',', '.') / 60;
155
172
  }
156
173
  if (match[14]) {
157
- ddLng += match[14] / 3600;
174
+ ddLng += match[14].replace(',', '.') / 3600;
158
175
  }
159
176
  if (parseInt(match[10]) < 0) {
160
177
  ddLng = -1 * ddLng;
@@ -1,3 +1,4 @@
1
+ export const dm_numbers: RegExp;
1
2
  export const dd_re: RegExp;
2
3
  export const dms_periods: RegExp;
3
4
  export const dms_abbr: RegExp;
package/dist/cjs/regex.js CHANGED
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
2
  //Coordinates pattern matching regex
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.coords_other = exports.dms_abbr = exports.dms_periods = exports.dd_re = void 0;
4
+ exports.coords_other = exports.dms_abbr = exports.dms_periods = exports.dd_re = exports.dm_numbers = void 0;
5
+ //DM as numbers only - see issue #15
6
+ const dm_numbers = /([+-]?[0-8]?[0-9])\s+([0-5]?[0-9]\.\d{3,})[\s,]{1,}([+-]?[0-1]?[0-9]?[0-9])\s+([0-5]?[0-9]\.\d{3,})/;
7
+ exports.dm_numbers = dm_numbers;
5
8
  //decimal degrees
6
9
  const 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;
7
10
  exports.dd_re = dd_re;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const converter_js_1 = __importDefault(require("../converter.js"));
7
- const testformats_js_1 = __importDefault(require("../testformats.js"));
7
+ const testformats_js_1 = __importDefault(require("./testformats.js"));
8
8
  const failFormats_js_1 = __importDefault(require("./failFormats.js"));
9
9
  let allPassed = true;
10
10
  //FORMATS THAT SHOULD BE CONVERTED
@@ -6,7 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const converter_js_1 = __importDefault(require("../converter.js"));
7
7
  //const test = '26°44S 29°46E'
8
8
  //const test = '00.00, 01.00'
9
- const test = `8°83S 35°67E`;
9
+ //const test = `45°5'46.8134"N, 18°30'36.7124"E`
10
+ //const test = `50 8.2914,-5 2.4447`
11
+ //const test = `8°83S 35°67E`
12
+ const test = `N 48° 30,6410', E 18° 57,4583'`;
10
13
  try {
11
14
  let converted = (0, converter_js_1.default)(test);
12
15
  console.log(converted);
@@ -342,6 +342,27 @@ const otherFormats = [
342
342
  verbatimLongitude: 'E 23.23.23',
343
343
  decimalLatitude: -27.759444,
344
344
  decimalLongitude: 23.38972222
345
+ },
346
+ {
347
+ verbatimCoordinates: '53 16.3863,4 52.8171',
348
+ verbatimLatitude: '53 16.3863',
349
+ verbatimLongitude: '4 52.8171',
350
+ decimalLatitude: 53.273105,
351
+ decimalLongitude: 4.88029
352
+ },
353
+ {
354
+ verbatimCoordinates: '50 8.2914,-5 2.4447',
355
+ verbatimLatitude: '50 8.2914',
356
+ verbatimLongitude: '-5 2.4447',
357
+ decimalLatitude: 50.13819,
358
+ decimalLongitude: -5.040745
359
+ },
360
+ {
361
+ verbatimCoordinates: `N 48° 30,6410', E 18° 57,4583'`,
362
+ verbatimLatitude: `N 48° 30,6410'`,
363
+ verbatimLongitude: `E 18° 57,4583'`,
364
+ decimalLatitude: 48.51068,
365
+ decimalLongitude: 18.95764
345
366
  }
346
367
  ];
347
368
  function getAllTestFormats() {
@@ -1,6 +1,7 @@
1
1
  export default toCoordinateFormat;
2
2
  /**
3
3
  * Converts decimalCoordinates to other formats commonly used
4
- * @param {*} format Either DMS or DM
4
+ * @param {string} format Either DMS or DM
5
+ * @returns {string}
5
6
  */
6
- declare function toCoordinateFormat(format: any): any;
7
+ declare function toCoordinateFormat(format: string): string;
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  /**
5
5
  * Converts decimalCoordinates to other formats commonly used
6
- * @param {*} format Either DMS or DM
6
+ * @param {string} format Either DMS or DM
7
+ * @returns {string}
7
8
  */
8
9
  function toCoordinateFormat(format) {
9
10
  if (!['DMS', 'DM', 'DD'].includes(format))
@@ -2,13 +2,21 @@ export default converter;
2
2
  /**
3
3
  * Function for converting coordinates in a variety of formats to decimal coordinates
4
4
  * @param {string} coordsString The coordinates string to convert
5
- * @param {number} decimalPlaces The number of decimal places for converted coordinates; default is 5
6
- * @returns {object} { verbatimCoordinates, decimalCoordinates, decimalLatitude, decimalLongitude }
5
+ * @param {number} [decimalPlaces] The number of decimal places for converted coordinates; default is 5
6
+ * @returns {{verbatimCoordinates: string, decimalCoordinates: string, decimalLatitude: string, decimalLongitude: string, closeEnough: function(string): boolean, toCoordinateFormat: toCoordinateFormat}}
7
7
  */
8
- declare function converter(coordsString: string, decimalPlaces: number): object;
8
+ declare function converter(coordsString: string, decimalPlaces?: number | undefined): {
9
+ verbatimCoordinates: string;
10
+ decimalCoordinates: string;
11
+ decimalLatitude: string;
12
+ decimalLongitude: string;
13
+ closeEnough: (arg0: string) => boolean;
14
+ toCoordinateFormat: typeof toCoordinateFormat;
15
+ };
9
16
  declare namespace converter {
10
17
  export { to };
11
18
  }
19
+ import toCoordinateFormat from './toCoordinateFormat.js';
12
20
  declare const to: Readonly<{
13
21
  DMS: "DMS";
14
22
  DM: "DM";
@@ -1,12 +1,12 @@
1
1
  //function for converting coordinates from a string to decimal and verbatim
2
2
  //this is just a comment
3
- import { dd_re, dms_periods, dms_abbr, coords_other } from './regex.js';
3
+ import { dm_numbers, dd_re, dms_periods, dms_abbr, coords_other } from './regex.js';
4
4
  import toCoordinateFormat from './toCoordinateFormat.js';
5
5
  /**
6
6
  * Function for converting coordinates in a variety of formats to decimal coordinates
7
7
  * @param {string} coordsString The coordinates string to convert
8
- * @param {number} decimalPlaces The number of decimal places for converted coordinates; default is 5
9
- * @returns {object} { verbatimCoordinates, decimalCoordinates, decimalLatitude, decimalLongitude }
8
+ * @param {number} [decimalPlaces] The number of decimal places for converted coordinates; default is 5
9
+ * @returns {{verbatimCoordinates: string, decimalCoordinates: string, decimalLatitude: string, decimalLongitude: string, closeEnough: function(string): boolean, toCoordinateFormat: toCoordinateFormat}}
10
10
  */
11
11
  function converter(coordsString, decimalPlaces) {
12
12
  //TODO add exact match to entered string, so that it can be used to filter out superflous text around it
@@ -20,7 +20,24 @@ function converter(coordsString, decimalPlaces) {
20
20
  let lngdir = "";
21
21
  let match = [];
22
22
  let matchSuccess = false;
23
- if (dd_re.test(coordsString)) {
23
+ if (dm_numbers.test(coordsString)) {
24
+ match = dm_numbers.exec(coordsString);
25
+ matchSuccess = checkMatch(match);
26
+ if (matchSuccess) {
27
+ ddLat = Math.abs(match[1]) + match[2] / 60;
28
+ if (Number(match[1]) < 0) {
29
+ ddLat *= -1;
30
+ }
31
+ ddLng = Math.abs(match[3]) + match[4] / 60;
32
+ if (Number(match[3]) < 0) {
33
+ ddLng *= -1;
34
+ }
35
+ }
36
+ else {
37
+ throw new Error("invalid coordinate format");
38
+ }
39
+ }
40
+ else if (dd_re.test(coordsString)) {
24
41
  match = dd_re.exec(coordsString);
25
42
  matchSuccess = checkMatch(match);
26
43
  if (matchSuccess) {
@@ -136,20 +153,20 @@ function converter(coordsString, decimalPlaces) {
136
153
  if (matchSuccess) {
137
154
  ddLat = Math.abs(parseInt(match[2]));
138
155
  if (match[4]) {
139
- ddLat += match[4] / 60;
156
+ ddLat += match[4].replace(',', '.') / 60;
140
157
  }
141
158
  if (match[6]) {
142
- ddLat += match[6] / 3600;
159
+ ddLat += match[6].replace(',', '.') / 3600;
143
160
  }
144
161
  if (parseInt(match[2]) < 0) {
145
162
  ddLat = -1 * ddLat;
146
163
  }
147
164
  ddLng = Math.abs(parseInt(match[10]));
148
165
  if (match[12]) {
149
- ddLng += match[12] / 60;
166
+ ddLng += match[12].replace(',', '.') / 60;
150
167
  }
151
168
  if (match[14]) {
152
- ddLng += match[14] / 3600;
169
+ ddLng += match[14].replace(',', '.') / 3600;
153
170
  }
154
171
  if (parseInt(match[10]) < 0) {
155
172
  ddLng = -1 * ddLng;
@@ -1,3 +1,4 @@
1
+ export const dm_numbers: RegExp;
1
2
  export const dd_re: RegExp;
2
3
  export const dms_periods: RegExp;
3
4
  export const dms_abbr: RegExp;
package/dist/mjs/regex.js CHANGED
@@ -1,4 +1,6 @@
1
1
  //Coordinates pattern matching regex
2
+ //DM as numbers only - see issue #15
3
+ const dm_numbers = /([+-]?[0-8]?[0-9])\s+([0-5]?[0-9]\.\d{3,})[\s,]{1,}([+-]?[0-1]?[0-9]?[0-9])\s+([0-5]?[0-9]\.\d{3,})/;
2
4
  //decimal degrees
3
5
  const 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;
4
6
  //degrees minutes seconds with '.' as separator - gives array with 15 values
@@ -7,4 +9,4 @@ const dms_periods = /(NORTH|SOUTH|[NS])?\s*([+-]?[0-8]?[0-9])\s*(\.)\s*([0-5]?[0
7
9
  const 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;
8
10
  //everything else - gives array of 17 values
9
11
  const 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;
10
- export { dd_re, dms_periods, dms_abbr, coords_other };
12
+ export { dm_numbers, dd_re, dms_periods, dms_abbr, coords_other };
@@ -1,5 +1,5 @@
1
1
  import convert from '../converter.js';
2
- import testFormats from '../testformats.js';
2
+ import testFormats from './testformats.js';
3
3
  import failingFormats from './failFormats.js';
4
4
  let allPassed = true;
5
5
  //FORMATS THAT SHOULD BE CONVERTED
@@ -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 = `8°83S 35°67E`;
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);
@@ -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
3
  * Converts decimalCoordinates to other formats commonly used
4
- * @param {*} format Either DMS or DM
4
+ * @param {string} format Either DMS or DM
5
+ * @returns {string}
5
6
  */
6
- declare function toCoordinateFormat(format: any): any;
7
+ declare function toCoordinateFormat(format: string): string;
@@ -1,7 +1,8 @@
1
1
  //borrowed from https://www.codegrepper.com/code-examples/javascript/javascript+converting+latitude+longitude+to+gps+coordinates
2
2
  /**
3
3
  * Converts decimalCoordinates to other formats commonly used
4
- * @param {*} format Either DMS or DM
4
+ * @param {string} format Either DMS or DM
5
+ * @returns {string}
5
6
  */
6
7
  function toCoordinateFormat(format) {
7
8
  if (!['DMS', 'DM', 'DD'].includes(format))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geo-coordinates-parser",
3
- "version": "1.6.5",
3
+ "version": "1.7.0",
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
  },