geo-coordinates-parser
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -242,3 +242,4 @@ //function for converting coordinates from a string to decimal and verbatim | ||
decimalLongitude: Number(ddLng), | ||
decimalCoordinates: `${ddLat},${ddLng}` | ||
decimalCoordinates: `${ddLat},${ddLng}`, | ||
closeEnough: coordsCloseEnough | ||
} | ||
@@ -296,4 +297,27 @@ } | ||
//functions for coordinate validation | ||
//as decimal arithmetic is not straightforward, we approximate | ||
function decimalsCloseEnough(dec1, dec2){ | ||
var diff = Math.abs(dec1 - dec2) | ||
return diff <= 0.0000011 | ||
} | ||
function coordsCloseEnough(coordsToTest) { | ||
if (coordsToTest.includes(',')){ | ||
var coords = coordsToTest.split(',') | ||
if(Number(coords[0]) == NaN || Number(coords[1]) == NaN) { | ||
throw new Error("coords are not valid decimals") | ||
} | ||
else { | ||
return decimalsCloseEnough(this.decimalLatitude, Number(coords[0])) && decimalsCloseEnough(this.decimalLongitude, coords[1]) //this here will be the converted coordinates object | ||
} | ||
} | ||
else { | ||
throw new Error("coords being tested must be separated by a comma") | ||
} | ||
} | ||
//Coordinates pattern matching regex | ||
var dd_re = /(NORTH|SOUTH|[NS])?[\s]*([+-]?[0-8]?[0-9](?:[\.,]\d{3,}))([•º°]?)[\s]*(NORTH|SOUTH|[NS])?[\s]*[,/]?[\s]*(EAST|WEST|[EW])?[\s]*([+-]?[0-1]?[0-9]?[0-9](?:[\.,]\d{3,}))([•º°]?)[\s]*(EAST|WEST|[EW])?/i; | ||
@@ -300,0 +324,0 @@ //degrees minutes seconds with '.' as separator - gives array with 15 values |
{ | ||
"name": "geo-coordinates-parser", | ||
"version": "1.0.0", | ||
"version": "1.0.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 @@ "main": "converter.js", |
# Geo Coordinate Parser | ||
A Javascript function for reading a variety of coordinate formats and converting to decimal numbers. Builds on other efforts for convenience by returning the verbatim coordinates and the decimal coordinates all in one object. | ||
A Javascript function for reading a variety of coordinate formats and converting to decimal numbers. Builds on other efforts for convenience by returning the verbatim coordinates and the decimal coordinates all in one object. Also includes a function to test existing decimal coordinates against those from the converter. | ||
@@ -9,13 +9,17 @@ ### Usage | ||
let coords = parseCoordsString('40° 26.7717, -79° 56.93172'); | ||
let converted = parseCoordsString('40° 26.7717, -79° 56.93172'); | ||
coords.decimalLatitude; // 40.446195 ✓ | ||
coords.decimalLongitude; // -79.948862 ✓ | ||
coords.verbatimLatitude; // '40° 26.7717' ✓ | ||
coords.verbatimLongitude; // '-79° 56.93172' ✓ | ||
converted.decimalLatitude; // 40.446195 ✓ | ||
converted.decimalLongitude; // -79.948862 ✓ | ||
converted.verbatimLatitude; // '40° 26.7717' ✓ | ||
converted.verbatimLongitude; // '-79° 56.93172' ✓ | ||
``` | ||
The returned object includes properties verbatimCoordinates, verbatimLatitude, verbatimLongitude, decimalLatitude, decimalLatitude, and decimalCoordinates. | ||
**Please add coordinate formats that throw an error in the Issues.** | ||
Sometimes we may want to validate existing decimal coordinates against those returned from the converter to find errors. Because we're working with decimal numbers we must settle for values that are close enough (in this case the same up to six decimal places). | ||
```js | ||
converted.closeEnough(yourDecimalCoordinatesToTest) //must be a string separated by , | ||
``` | ||
### Supported formats | ||
@@ -28,2 +32,4 @@ | ||
**Please add coordinate formats that throw an error in the Issues.** | ||
### License | ||
@@ -30,0 +36,0 @@ MIT Licence |
@@ -11,5 +11,7 @@ const convert = require('./converter') | ||
var converted = convert(t.verbatimCoordinates) | ||
var testDecimalCoordsString = `${t.decimalLatitude},${t.decimalLongitude}` | ||
//check the calculation is correct | ||
if(!decimalsCloseEnough(converted.decimalLatitude, t.decimalLatitude) || !decimalsCloseEnough(converted.decimalLongitude, t.decimalLongitude)) { | ||
if(!converted.closeEnough(testDecimalCoordsString)) { | ||
console.log("Error in decimal conversion") | ||
@@ -51,2 +53,4 @@ console.log(t.verbatimCoordinates) | ||
//as decimal arithmetic is not straightforward, we approximate | ||
@@ -53,0 +57,0 @@ function decimalsCloseEnough(dec1, dec2){ |
23957
8
593
38