Is there a JavaScript library that can help with parsing coordinates in various formats provided by users?
I have a map site with a search box that can be used for searching place names, but looking through the stats I'm seeing a lot of people trying to use coordinates in various formats directly, much like they are able to do on Google Maps.
Typical variations of latitudes and longitudes I see are:
-43 02 42.845 S 172 38 33.790 E
n 19°49'2.30e 41°51'29.20
41°46'57.90"S
172° 1'20.39"E 43 02 42.845 S
42.897S 172.720E
42.31.303 172.23.379
19.817306°, 41.858111°
That's a lot of variations, which is only a sample. Then there are users trying various New Zealand formats based on NZTM and NZMG.
Ideally I'd like to find a library that someone may have started with some basic latitudes and longitudes then extend the functionality to support other formats I see coming through.
Anyone know of anything already out there?
To parse numeric coordinates from raw text input, there is npm package coordinate-parser by otto-dev. It feels robust for WGS84 latitude-longitude pairs and their minute and decimal representations.
const CoordinateParser = require('coordinate-parser')
const text = `60 40' 31.35" N, 23 50' 55.75" E`
const pos = new CoordinateParser(text)
const lat = pos.getLatitude() // 60.675374
const lng = pos.getLongitude() // 23.848819
While proper for WGS84, coordinate-parser does not handle UTM coordinates, such as the ones on NZTM or ETRS89 TM35FIN, for example N 6730758, E 327874. The parser treats the large numbers as milliseconds or densely formatted DDMMSS.xxx instead of metres, thus yielding invalid results.
For UTM coordinates there is utm-coordinate-parser. It recognises common easting-northing pairs such as 6730758 N 327874 E as well as x-y pairs x:327874 y:6730758. Disclaimer: I am the package author.
const utmp = require('utm-coordinate-parser')
const text = 'N 6730758; W -327874'
const pos = utmp.parse(text)
const x = pos.x // 327874
const y = pos.y // 6730758
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With