Created
November 17, 2014 11:12
-
-
Save huacnlee/713c79f934bb3ce5bf5a to your computer and use it in GitHub Desktop.
用于将 GPS 的坐标格式转成浮点数, 如 DMSToDecimal.convert("N 30d17m27.786712646484375s") => 30.282034
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.DatatypeConverter; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
// 用于将 GPS 的坐标格式转成浮点数 | |
// 如 DMSToDecimal.convert("N 30d17m27.786712646484375s") => 30.282034 | |
public class DMSToDecimal { | |
private static final String REGEXP = "(N|E|W|S)([\\.\\d\\s]+)d([\\.\\d\\s]+)m([\\.\\d\\s]+)s"; | |
private static double dmsToDecimal(String hemisphereOUmeridien,double degres,double minutes,double secondes){ | |
double LatOrLon=0; | |
double signe=1.0; | |
if((hemisphereOUmeridien.equals("W"))||(hemisphereOUmeridien.equals("S"))) {signe=-1.0;} | |
LatOrLon = signe*(Math.floor(degres) + Math.floor(minutes)/60.0 + secondes/3600.0); | |
return(LatOrLon); | |
} | |
// N 30d17m27.786712646484375s -> 30.282034 | |
public static double convert(String input) { | |
Pattern p = Pattern.compile(REGEXP); | |
Matcher match = p.matcher(input); | |
double decimal = 0.0; | |
if (match.matches() == true) { | |
String h = match.group(1); | |
double d = Double.parseDouble(match.group(2)); | |
double m = Double.parseDouble(match.group(3)); | |
double s = Double.parseDouble(match.group(4)); | |
decimal = dmsToDecimal(h, d, m, s); | |
} | |
return decimal; | |
} | |
public static void main(String[] args) { | |
System.out.format("%f%n", convert("N 30d17m30.567626953125s")); | |
System.out.format("%f%n", convert("E120d7m40.1599999999999966s")); | |
System.out.format("%f%n", convert("E 119d4m47.017822265625s")); | |
System.out.format("%f%n", convert("N 30d17m21.359710693359375s")); | |
System.out.format("%f%n", convert("E120d2m42.0499999999999972s")); | |
System.out.format("%f%n", convert("N0d0m0s")); | |
System.out.format("%f%n", convert("S 30d16m55.322113037109375s")); | |
System.out.format("%f%n", convert("W 120d3m40.1385498046875s")); | |
// System.out.format("%f%n",a); | |
// System.out.format("%f%n",DMSToDecimal("E",120,3,38.984985)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment