I’m using a GPS web service that is retrieving information in the following format (numbers changed up a bit for privacy reasons but format is unchanged):
X: 32 14 08.47S
Y: 140 17 12.82E
What I need to do is convert these to decimal co-ordinates (xx.xxxxxxxxx, xx.xxxxxxxxx). Are there any simple snippets of Java code that can do this task? If not, I’m happy to look at resources that explain how to achieve this in a different language.
If the degrees, minutes, and seconds are guaranteed to be separated a single space you could do something as simple as
That will leave you with
You could then
Double.parseDouble()the ones aftertokens[0]to get the numeric degrees, minutes, and seconds which you would then combine to get the decimal degrees. Of course fortokens[3]you’d have to strip off the final N/S/E/W character before doing the parse.Another more elegant possibility might be to take advantage of the fact that instances of
MessageFormatand its subclasses can be use for parsing a string of a given format as well as formatting such a string.