geo-coordinates-parser
Advanced tools
Comparing version 1.0.1 to 1.1.0
//function for converting coordinates from a string to decimal and verbatim | ||
function converter(coordsString) { | ||
module.exports = function(coordsString) { | ||
coordsString = coordsString.replace(/\s\s+/g, ' '); //just to tidy up whitespaces | ||
@@ -185,16 +184,52 @@ | ||
//we need to get the verbatim coords from the string | ||
//the conversion worked, so we should be able to split down the middle | ||
var middle = Math.round(coordsString.length/2) | ||
var verbatimLat = coordsString.substr(0, middle).trim() | ||
var verbatimLng = coordsString.substr(middle).trim() | ||
//remove any separators if they're in there | ||
var seps = "[,/]" | ||
var reg = new RegExp(seps) | ||
//verbatimLat | ||
if(reg.test(verbatimLat[verbatimLat.length - 1])) { | ||
verbatimLat = verbatimLat.substr(0, verbatimLat.length - 1).trim() | ||
//we can't split down the middle because if there are decimals they may have different numbers on each side | ||
//so we need to find the separating character, or if none, use the match values to split down the middle | ||
var verbatimLat | ||
var verbatimLng | ||
var sepChars = /[,/\u0020]/g //comma, forward slash and spacebar | ||
var seps = coordsString.match(sepChars) | ||
if (seps == null) { | ||
//split down the middle | ||
var middle = Math.floor(coordsString.length/2) | ||
verbatimLat = coordsString.substring(0, middle).trim() | ||
verbatimLng = coordsString.substring(middle).trim() | ||
} | ||
//verbatimLong | ||
if(reg.test(verbatimLng[0])) { | ||
verbatimLng = verbatimLng.substr(1).trim() | ||
else { //if length is odd then find the index of the middle value | ||
//get the middle index | ||
var middle | ||
//easy for odd numbers | ||
if (seps.length % 2 == 1) { | ||
middle = Math.floor(seps.length / 2) | ||
} | ||
else { | ||
middle = (seps.length / 2) - 1 | ||
} | ||
//walk through seps until we get to the middle | ||
var splitIndex = 0; | ||
//it might be only one value | ||
if (middle == 0){ | ||
splitIndex = coordsString.indexOf(seps[0]) | ||
verbatimLat = coordsString.substring(0, splitIndex).trim() | ||
verbatimLng = coordsString.substring(splitIndex + 1).trim() | ||
} | ||
else { | ||
var currSepIndex = 0 | ||
var startSearchIndex = 0 | ||
while (currSepIndex <= middle){ | ||
splitIndex = coordsString.indexOf(seps[currSepIndex], startSearchIndex) | ||
startSearchIndex = splitIndex + 1 | ||
currSepIndex++ | ||
} | ||
verbatimLat = coordsString.substring(0, splitIndex).trim() | ||
verbatimLng = coordsString.substring(splitIndex + 1).trim() | ||
} | ||
} | ||
@@ -329,1 +364,8 @@ | ||
var coords_other = /(NORTH|SOUTH|[NS])?[\ \t]*([+-]?[0-8]?[0-9])[\ \t]*([•º°\.:]|D(?:EG)?(?:REES)?)?[\ \t]*,?([0-5]?[0-9](?:\.\d{1,})?)?[\ \t]*(['′´’\.:]|M(?:IN)?(?:UTES)?)?[\ \t]*,?((?:[0-5]?[0-9])(?:\.\d{1,3})?)?[\ \t]*(''|′′|’’|´´|["″”\.])?[\ \t]*(NORTH|SOUTH|[NS])?(?:[\ \t]*[,/][\ \t]*|[\ \t]*)(EAST|WEST|[EW])?[\ \t]*([+-]?[0-1]?[0-9]?[0-9])[\ \t]*([•º°\.:]|D(?:EG)?(?:REES)?)?[\ \t]*,?([0-5]?[0-9](?:\.\d{1,})?)?[\ \t]*(['′´’\.:]|M(?:IN)?(?:UTES)?)?[\ \t]*,?((?:[0-5]?[0-9])(?:\.\d{1,3})?)?[\ \t]*(''|′′|´´|’’|["″”\.])?[\ \t]*(EAST|WEST|[EW])?/i; | ||
var testFormats = require('./testformats').map(format => format.verbatimCoordinates) | ||
converter.formats = testFormats | ||
module.exports = converter |
{ | ||
"name": "geo-coordinates-parser", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"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", |
@@ -7,5 +7,5 @@ # Geo Coordinate Parser | ||
```js | ||
parseCoordsString = require('geo-coordinate-parser'); | ||
const convert = require('geo-coordinate-parser'); | ||
let converted = parseCoordsString('40° 26.7717, -79° 56.93172'); | ||
let converted = convert('40° 26.7717, -79° 56.93172'); | ||
@@ -30,6 +30,13 @@ converted.decimalLatitude; // 40.446195 ✓ | ||
- 27deg 15min 45.2sec S 18deg 32min 53.7sec E | ||
- 40° 26.7717 -79° 56.93172 | ||
- 18.24S 22.45E // read as degrees and minutes | ||
**Please add coordinate formats that throw an error in the Issues.** | ||
Formats used to testing can be be accessed with: | ||
```js | ||
covert.formats | ||
``` | ||
**Note to users: Please add coordinate formats that throw an error in the Github Issues.** | ||
### License | ||
@@ -36,0 +43,0 @@ MIT Licence |
@@ -241,2 +241,10 @@ //return an array of coordinate strings for testing | ||
decimalLongitude: 28.2344 | ||
}, | ||
{ | ||
verbatimCoordinates: '40° 26.7717 -79° 56.93172', | ||
verbatimLatitude: '40° 26.7717', | ||
verbatimLongitude: '-79° 56.93172', | ||
decimalLatitude: 40.446195, | ||
decimalLongitude: -79.948862 | ||
} | ||
@@ -243,0 +251,0 @@ ] |
25545
629
45