geo-coordinates-parser
Advanced tools
Comparing version 1.7.0 to 1.7.1
@@ -142,2 +142,3 @@ "use strict"; | ||
} | ||
//the compass directions | ||
if (match[1]) { | ||
@@ -180,2 +181,3 @@ latdir = match[1]; | ||
} | ||
//the compass directions | ||
if (match[1]) { | ||
@@ -205,4 +207,4 @@ latdir = match[1]; | ||
//if we have one direction we must have the other | ||
if ((latdir || lngdir) && (!latdir || !lngdir)) { | ||
throw new Error("invalid coordinates format"); | ||
if ((latdir && !lngdir) || (!latdir && lngdir)) { | ||
throw new Error("invalid coordinates value"); | ||
} | ||
@@ -285,16 +287,15 @@ //the directions can't be the same | ||
//no integer coords allowed | ||
//validation -- no integer coords | ||
if (/^\d+$/.test(verbatimLat) || /^\d+$/.test(verbatimLng)) { | ||
throw new Error('degree only coordinate/s provided'); | ||
} | ||
//some tidying up... | ||
// last bit of tidying up... | ||
if (isNaN(ddLat) && ddLat.includes(',')) { | ||
ddLat = ddLat.replace(',', '.'); | ||
} | ||
if (isNaN(ddLng) && ddLng.includes(',')) { | ||
ddLng = ddLng.replace(',', '.'); | ||
} | ||
//all done!! | ||
//just truncate the decimals appropriately | ||
ddLat = Number(Number(ddLat).toFixed(decimalPlaces)); | ||
if (isNaN(ddLng) && ddLng.includes(',')) { | ||
ddLng = ddLng.replace(',', '.'); | ||
} | ||
ddLng = Number(Number(ddLng).toFixed(decimalPlaces)); | ||
@@ -324,7 +325,7 @@ return Object.freeze({ | ||
filteredMatch.shift(); | ||
//check the array length is an even number else exit | ||
//check the array length is an even number | ||
if (filteredMatch.length % 2 > 0) { | ||
return false; | ||
} | ||
//regex for testing corresponding values match | ||
// regex for testing corresponding values match | ||
const numerictest = /^[-+]?\d+([\.,]\d+)?$/; //for testing numeric values | ||
@@ -380,2 +381,3 @@ const stringtest = /[eastsouthnorthwest]+/i; //for testing string values (north, south, etc) | ||
} | ||
// An enum for coordinates formats | ||
const to = Object.freeze({ | ||
@@ -382,0 +384,0 @@ DMS: 'DMS', |
"use strict"; | ||
// when adding new formats, make sure to add them to testformats.js, and not to testFormats.json | ||
// use makeTestFormatsJSON.js to make testFormats.json | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -3,0 +5,0 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; |
@@ -11,11 +11,11 @@ "use strict"; | ||
}; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
test.decimalCoordinates = '-25.76887,28.26939'; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.toCoordinateFormat('DD')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DD')); | ||
test.decimalCoordinates = '-25.815928, 28.070318'; | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); | ||
test.decimalCoordinates = '-25.000, 28.000'; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); |
@@ -239,4 +239,4 @@ "use strict"; | ||
]; | ||
// additional formats we've encountered | ||
const otherFormats = [ | ||
// additional formats we've encountered | ||
{ | ||
@@ -243,0 +243,0 @@ verbatimCoordinates: '10.432342S 10.6345345E', |
export default toCoordinateFormat; | ||
/** | ||
* Converts decimalCoordinates to other formats commonly used | ||
* Converts decimalCoordinates to commonly used string formats | ||
* Note that this will add degree and direction symbols to decimal coordinates | ||
* @param {string} format Either DMS or DM | ||
@@ -5,0 +6,0 @@ * @returns {string} |
@@ -5,3 +5,4 @@ "use strict"; | ||
/** | ||
* Converts decimalCoordinates to other formats commonly used | ||
* Converts decimalCoordinates to commonly used string formats | ||
* Note that this will add degree and direction symbols to decimal coordinates | ||
* @param {string} format Either DMS or DM | ||
@@ -14,16 +15,38 @@ * @returns {string} | ||
if (this.decimalCoordinates && this.decimalCoordinates.trim()) { | ||
const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim())); | ||
const decimalLatitude = Number(parts[0]); | ||
const decimalLongitude = Number(parts[1]); | ||
const absoluteLatitude = Math.abs(decimalLatitude); | ||
const absoluteLongitude = Math.abs(decimalLongitude); | ||
const latDir = decimalLatitude > 0 ? "N" : "S"; | ||
const longDir = decimalLongitude > 0 ? "E" : "W"; | ||
let result; | ||
if (format == 'DD') { | ||
return this.decimalCoordinates; | ||
result = `${absoluteLatitude}° ${latDir}, ${absoluteLongitude}° ${longDir}`; | ||
} | ||
const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim())); | ||
let convertedLat = convert(parts[0], format, true); | ||
let convertedLong = convert(parts[1], format, false); | ||
//some custom cleaning for DMS | ||
if (convertedLat.endsWith('.0"') && convertedLong.endsWith('.0"')) { | ||
convertedLat = convertedLat.replace(/\.0"$/, '"'); | ||
convertedLong = convertedLong.replace(/\.0"$/, '"'); | ||
//else we need some more things | ||
const degreesLatitude = Math.floor(absoluteLatitude); | ||
const degreesLongitude = Math.floor(absoluteLongitude); | ||
const minutesLatitudeNotTruncated = (absoluteLatitude - degreesLatitude) * 60; | ||
const minutesLongitudeNotTruncated = (absoluteLongitude - degreesLongitude) * 60; | ||
if (format == 'DM') { | ||
const dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
const dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`; | ||
} | ||
const latDirection = parts[0] >= 0 ? " N" : " S"; | ||
const longDirection = parts[1] >= 0 ? " E" : " W"; | ||
return `${convertedLat + latDirection}, ${convertedLong + longDirection}`; | ||
if (format == "DMS") { | ||
const latMinutes = Math.floor(minutesLatitudeNotTruncated); | ||
const longMinutes = Math.floor(minutesLongitudeNotTruncated); | ||
let latSeconds = ((minutesLatitudeNotTruncated - latMinutes) * 60).toFixed(1).padStart(4, '0'); | ||
let longSeconds = ((minutesLongitudeNotTruncated - longMinutes) * 60).toFixed(1).padStart(4, '0'); | ||
const latMinutesString = latMinutes.toString().padStart(2, '0'); | ||
const longMinutesString = longMinutes.toString().padStart(2, '0'); | ||
// if they both end in .0 we drop the .0 | ||
if (latSeconds.endsWith('.0"') && longSeconds.endsWith('.0"')) { | ||
latSeconds = latSeconds.replace(/\.0"$/, '"'); | ||
longSeconds = longSeconds.replace(/\.0"$/, '"'); | ||
} | ||
result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`; | ||
} | ||
return result; | ||
} | ||
@@ -34,17 +57,2 @@ else { | ||
} | ||
//assumes everything is valid... | ||
function convert(coord, format) { | ||
const absolute = Math.abs(coord); | ||
const degrees = Math.floor(absolute); | ||
const minutesNotTruncated = (absolute - degrees) * 60; | ||
if (format == 'DM') { | ||
let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
return `${degrees}° ${dmMins}'`; | ||
} | ||
//else | ||
let minutes = Math.floor(minutesNotTruncated); | ||
let seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).padStart(4, '0'); | ||
minutes = minutes.toString().padStart(2, '0'); | ||
return `${degrees}° ${minutes}' ${seconds}"`; | ||
} | ||
function round(num, places) { | ||
@@ -51,0 +59,0 @@ const d = Math.pow(10, places); |
@@ -137,2 +137,3 @@ //function for converting coordinates from a string to decimal and verbatim | ||
} | ||
//the compass directions | ||
if (match[1]) { | ||
@@ -175,2 +176,3 @@ latdir = match[1]; | ||
} | ||
//the compass directions | ||
if (match[1]) { | ||
@@ -200,4 +202,4 @@ latdir = match[1]; | ||
//if we have one direction we must have the other | ||
if ((latdir || lngdir) && (!latdir || !lngdir)) { | ||
throw new Error("invalid coordinates format"); | ||
if ((latdir && !lngdir) || (!latdir && lngdir)) { | ||
throw new Error("invalid coordinates value"); | ||
} | ||
@@ -280,16 +282,15 @@ //the directions can't be the same | ||
//no integer coords allowed | ||
//validation -- no integer coords | ||
if (/^\d+$/.test(verbatimLat) || /^\d+$/.test(verbatimLng)) { | ||
throw new Error('degree only coordinate/s provided'); | ||
} | ||
//some tidying up... | ||
// last bit of tidying up... | ||
if (isNaN(ddLat) && ddLat.includes(',')) { | ||
ddLat = ddLat.replace(',', '.'); | ||
} | ||
if (isNaN(ddLng) && ddLng.includes(',')) { | ||
ddLng = ddLng.replace(',', '.'); | ||
} | ||
//all done!! | ||
//just truncate the decimals appropriately | ||
ddLat = Number(Number(ddLat).toFixed(decimalPlaces)); | ||
if (isNaN(ddLng) && ddLng.includes(',')) { | ||
ddLng = ddLng.replace(',', '.'); | ||
} | ||
ddLng = Number(Number(ddLng).toFixed(decimalPlaces)); | ||
@@ -319,7 +320,7 @@ return Object.freeze({ | ||
filteredMatch.shift(); | ||
//check the array length is an even number else exit | ||
//check the array length is an even number | ||
if (filteredMatch.length % 2 > 0) { | ||
return false; | ||
} | ||
//regex for testing corresponding values match | ||
// regex for testing corresponding values match | ||
const numerictest = /^[-+]?\d+([\.,]\d+)?$/; //for testing numeric values | ||
@@ -375,2 +376,3 @@ const stringtest = /[eastsouthnorthwest]+/i; //for testing string values (north, south, etc) | ||
} | ||
// An enum for coordinates formats | ||
const to = Object.freeze({ | ||
@@ -377,0 +379,0 @@ DMS: 'DMS', |
@@ -0,1 +1,3 @@ | ||
// when adding new formats, make sure to add them to testformats.js, and not to testFormats.json | ||
// use makeTestFormatsJSON.js to make testFormats.json | ||
import convert from '../converter.js'; | ||
@@ -2,0 +4,0 @@ import testFormats from './testformats.js'; |
@@ -6,11 +6,11 @@ import toCoordinateFormat from '../toCoordinateFormat.js'; | ||
}; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
test.decimalCoordinates = '-25.76887,28.26939'; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.toCoordinateFormat('DD')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DD')); | ||
test.decimalCoordinates = '-25.815928, 28.070318'; | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); | ||
test.decimalCoordinates = '-25.000, 28.000'; | ||
console.log(test.toCoordinateFormat('DMS')); | ||
console.log(test.toCoordinateFormat('DM')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DMS')); | ||
console.log(test.decimalCoordinates, '==', test.toCoordinateFormat('DM')); |
@@ -237,4 +237,4 @@ //return an array of coordinate strings for testing | ||
]; | ||
// additional formats we've encountered | ||
const otherFormats = [ | ||
// additional formats we've encountered | ||
{ | ||
@@ -241,0 +241,0 @@ verbatimCoordinates: '10.432342S 10.6345345E', |
export default toCoordinateFormat; | ||
/** | ||
* Converts decimalCoordinates to other formats commonly used | ||
* Converts decimalCoordinates to commonly used string formats | ||
* Note that this will add degree and direction symbols to decimal coordinates | ||
* @param {string} format Either DMS or DM | ||
@@ -5,0 +6,0 @@ * @returns {string} |
//borrowed from https://www.codegrepper.com/code-examples/javascript/javascript+converting+latitude+longitude+to+gps+coordinates | ||
/** | ||
* Converts decimalCoordinates to other formats commonly used | ||
* Converts decimalCoordinates to commonly used string formats | ||
* Note that this will add degree and direction symbols to decimal coordinates | ||
* @param {string} format Either DMS or DM | ||
@@ -11,16 +12,38 @@ * @returns {string} | ||
if (this.decimalCoordinates && this.decimalCoordinates.trim()) { | ||
const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim())); | ||
const decimalLatitude = Number(parts[0]); | ||
const decimalLongitude = Number(parts[1]); | ||
const absoluteLatitude = Math.abs(decimalLatitude); | ||
const absoluteLongitude = Math.abs(decimalLongitude); | ||
const latDir = decimalLatitude > 0 ? "N" : "S"; | ||
const longDir = decimalLongitude > 0 ? "E" : "W"; | ||
let result; | ||
if (format == 'DD') { | ||
return this.decimalCoordinates; | ||
result = `${absoluteLatitude}° ${latDir}, ${absoluteLongitude}° ${longDir}`; | ||
} | ||
const parts = this.decimalCoordinates.split(',').map(x => Number(x.trim())); | ||
let convertedLat = convert(parts[0], format, true); | ||
let convertedLong = convert(parts[1], format, false); | ||
//some custom cleaning for DMS | ||
if (convertedLat.endsWith('.0"') && convertedLong.endsWith('.0"')) { | ||
convertedLat = convertedLat.replace(/\.0"$/, '"'); | ||
convertedLong = convertedLong.replace(/\.0"$/, '"'); | ||
//else we need some more things | ||
const degreesLatitude = Math.floor(absoluteLatitude); | ||
const degreesLongitude = Math.floor(absoluteLongitude); | ||
const minutesLatitudeNotTruncated = (absoluteLatitude - degreesLatitude) * 60; | ||
const minutesLongitudeNotTruncated = (absoluteLongitude - degreesLongitude) * 60; | ||
if (format == 'DM') { | ||
const dmMinsLatitude = round(minutesLatitudeNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
const dmMinsLongitude = round(minutesLongitudeNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
result = `${degreesLatitude}° ${dmMinsLatitude}' ${latDir}, ${degreesLongitude}° ${dmMinsLongitude}' ${longDir}`; | ||
} | ||
const latDirection = parts[0] >= 0 ? " N" : " S"; | ||
const longDirection = parts[1] >= 0 ? " E" : " W"; | ||
return `${convertedLat + latDirection}, ${convertedLong + longDirection}`; | ||
if (format == "DMS") { | ||
const latMinutes = Math.floor(minutesLatitudeNotTruncated); | ||
const longMinutes = Math.floor(minutesLongitudeNotTruncated); | ||
let latSeconds = ((minutesLatitudeNotTruncated - latMinutes) * 60).toFixed(1).padStart(4, '0'); | ||
let longSeconds = ((minutesLongitudeNotTruncated - longMinutes) * 60).toFixed(1).padStart(4, '0'); | ||
const latMinutesString = latMinutes.toString().padStart(2, '0'); | ||
const longMinutesString = longMinutes.toString().padStart(2, '0'); | ||
// if they both end in .0 we drop the .0 | ||
if (latSeconds.endsWith('.0"') && longSeconds.endsWith('.0"')) { | ||
latSeconds = latSeconds.replace(/\.0"$/, '"'); | ||
longSeconds = longSeconds.replace(/\.0"$/, '"'); | ||
} | ||
result = `${degreesLatitude}° ${latMinutesString}' ${latSeconds}" ${latDir}, ${degreesLongitude}° ${longMinutesString}' ${longSeconds}" ${longDir}`; | ||
} | ||
return result; | ||
} | ||
@@ -31,17 +54,2 @@ else { | ||
} | ||
//assumes everything is valid... | ||
function convert(coord, format) { | ||
const absolute = Math.abs(coord); | ||
const degrees = Math.floor(absolute); | ||
const minutesNotTruncated = (absolute - degrees) * 60; | ||
if (format == 'DM') { | ||
let dmMins = round(minutesNotTruncated, 3).toFixed(3).padStart(6, '0'); | ||
return `${degrees}° ${dmMins}'`; | ||
} | ||
//else | ||
let minutes = Math.floor(minutesNotTruncated); | ||
let seconds = ((minutesNotTruncated - minutes) * 60).toFixed(1).padStart(4, '0'); | ||
minutes = minutes.toString().padStart(2, '0'); | ||
return `${degrees}° ${minutes}' ${seconds}"`; | ||
} | ||
function round(num, places) { | ||
@@ -48,0 +56,0 @@ const d = Math.pow(10, places); |
{ | ||
"name": "geo-coordinates-parser", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"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,0 +5,0 @@ "type": "module", |
112047
51
2857