ianengelbrecht / geo-coordinates-parser

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.
MIT License
38 stars 9 forks source link

'invalid coordinates format' error with short longitude like '48.437316, -124' #3

Closed ccallendar closed 2 years ago

ccallendar commented 2 years ago

Hi, I'm noticing the error invalid coordinates format when I try to parse a string like 48.437316, -124. Looking at the regular expressions I guess the above string doesn't match the DD regex because it has less than 3 decimal digits. Any suggestions?

Thanks.

ianengelbrecht commented 2 years ago

Thanks Chris, yes the parser will throw that as an error. The assumption I built in is that decimal coordinates without decimal places are treated as incorrect (except two decimal places, which is then treated as degrees and minutes as these are often written as decimals, very confusing!). In your example, there is a very high level of precision on the latitude, which is not matched on the longitude. The likelihood of having a coordinate that is exactly -124.000000 is very small, and likely represents a transcription error or something like that. Same for something like 0,0 (see issue #2). So, the solution is either to use the parser to throw these kinds of values as errors that need to be checked and handle them accordingly, or if you're happy to accept coordinates like this as correct, put your own regex in place to catch them, either before they get to the parser, or in the catch statement after the parser. Something like /^-?\d+(\.\d{1,2})?[,;\s]+-?\d+(\.\d{1,2})?$/.test(coords) should work (but I haven't tested it!).

ccallendar commented 2 years ago

Thanks for the reply! That sound reasonable, I'll give that regex a try. And thanks for your effort making this package!