19

Looking for a java utility. It is even better if you can tell me how to do it using geotools library.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • possible duplicate of [How do I convert these coordinates to coordinates readable by Google Maps?](http://stackoverflow.com/questions/995738/how-do-i-convert-these-coordinates-to-coordinates-readable-by-google-maps) – Cute Bear Jan 15 '14 at 15:59

4 Answers4

25

By "Decimal coordinates" do you mean latitude and longitude (also known as the decimal degree)? If so, what you're trying to do is pretty easy:

Given a DMS (Degrees, Minutes, Seconds) coordinate such as W87°43′41″, it's trivial to convert it to a number of decimal degrees using the following method: Calculate the total number of seconds, 43′41″ = (43*60 + 41) = 2621 seconds. The fractional part is total number of seconds divided by 3600. 2621 / 3600 = ~0.728056 Add fractional degrees to whole degrees to produce the final result: 87 + 0.728056 = 87.728056

Since it is a West longitude coordinate, negate the result. The final result is -87.728056.

From Wikipedia. Here's a Javascript widget that does the same thing.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
23

It depends on your source format. If it's already split up into degrees (d), minutes (m), and seconds (s), your algorithm is:

(assuming d is can be positive or negative)

dd = Math.signum(d) * (Math.abs(d) + (m / 60.0) + (s / 3600.0));

If it's smooshed together into a string, the usual format is:

"ddd.mmss"

So parse out using a regular expression or String.substring() to get m and s.

Converting back is:

d = (int)dd;  // Truncate the decimals
t1 = (dd - d) * 60;
m = (int)t1;
s = (t1 - m) * 60;
CodeSlinger
  • 429
  • 3
  • 8
10
**begin  23°26’49”**
degrees = 23
minutes = 26
seconds = 49
decimal = ((minutes * 60)+seconds) / (60*60))
answer = degrees + decimal
**finish  23.44694444**
RussWill
  • 201
  • 3
  • 3
0

TLDR

fun getNormal(value: String): Double {
    val degree = value.substringBefore("°").toDouble()
    val minute = value.substringAfter("°").substringBefore("'").toDouble()
    val second = value.substringAfter("'").trim().toDouble()
    return sign(degree) * (abs(degree) + (minute / 60.0) + (second / 3600.0))
}

getNormal("34° 31' 24.924") returns 34.52359

Emad Razavi
  • 1,903
  • 2
  • 17
  • 24