Fix route object calc

This commit is contained in:
Victor Shcherb 2015-05-09 23:53:16 +02:00
parent a4adbfeba1
commit eaf140dded
2 changed files with 44 additions and 52 deletions

View file

@ -187,63 +187,33 @@ public class RouteDataObject {
} }
public static float parseLength(String v, float def) { public static float parseLength(String v, float def) {
// Supported formats: float f = 0;
// 10 m // 14'10" 14 - inches, 10 feet
// 10m
// 14'10"
// 14.5'
// 15ft
// Even values with any more characters after these formats are rejected, e.g. (14'10"x) or (10 metres).
int i = Algorithms.findFirstNumberEndIndex(v); int i = Algorithms.findFirstNumberEndIndex(v);
if (i > 0) { if (i > 0) {
float f = Float.parseFloat(v.substring(0, i)); f += Float.parseFloat(v.substring(0, i));
if (i == v.Length()) { String pref = v.substring(i, v.length()).trim();
// Value with no unit means meters. Done. float add = 0;
return f; for(int ik = 0; ik < pref.length(); ik++) {
} else { if(Character.isDigit(pref.charAt(ik)) || pref.charAt(ik) == '.' || pref.charAt(ik) == '-') {
if (v.charAt(i) == " ") { int first = Algorithms.findFirstNumberEndIndex(pref.substring(ik));
i++; if(first != -1) {
add = parseLength(pref.substring(ik), 0);
pref = pref.substring(0, ik);
} }
if (v.charAt(i) == "m") { break;
// Meters, no conversion needed, done.
if (i == v.Length()) {
return f;
} else {
return def;
} }
} }
if (v.charAt(i) == "'") { if(pref.contains("km")) {
// Convert feet to meters. f *= 1000;
}
if(pref.contains("\"")) {
f *= 0.0254;
} else if (pref.contains("\'") || pref.contains("ft")) {
// foot to meters
f *= 0.3048; f *= 0.3048;
i++;
} else if (v.charAt(i) == "f" && i < v.Length() && v.charAt(i+1) != "t") {
// 'ft' is a discouraged unit per the wiki, but we know what it means.
// Convert feet to meters.
f *= 0.3048;
// When 'ft' is used, no inches are expected.
if (i == v.Length()) {
return f;
} else {
return def;
}
}
if (i < v.Length()) {
String w = v.substring(i);
int j = Algorithms.findFirstNumberEndIndex(w);
if (j > 0 && w.charAt(0) == "\"") {
float g = Float.parseFloat(w.substring(0, j))
// Convert inches to meters.
f += g * 0.0254;
if (i + j == v.Length()) {
return f;
} else {
return def;
}
}
} else {
return f;
}
} }
return f + add;
} }
return def; return def;
} }
@ -413,6 +383,27 @@ public class RouteDataObject {
return Math.abs(px - x) * 0.011d + Math.abs(py - y) * 0.01863d; return Math.abs(px - x) * 0.011d + Math.abs(py - y) * 0.01863d;
} }
private static void assertTrueLength(String vl, float exp){
float dest = parseLength(vl, 0);
if(exp != dest) {
System.err.println("FAIL " + vl + " " + dest);
} else {
System.out.println("OK " + vl);
}
}
public static void main(String[] args) {
assertTrueLength("10 km", 10000);
assertTrueLength("0.01 km", 10);
assertTrueLength("0.01 km 10 m", 20);
assertTrueLength("10 m", 10);
assertTrueLength("10m", 10);
assertTrueLength("3.4 m", 3.4f);
assertTrueLength("14'10\"", 4.5212f);
assertTrueLength("14.5'", 4.4196f);
assertTrueLength("15ft", 4.572f);
}
@Override @Override
public String toString() { public String toString() {

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.osmand.nauticalPlugin" package="net.osmand.nauticalPlugin"
android:installLocation="auto"
android:versionCode="6" android:versionCode="6"
android:versionName="1.0" > android:versionName="1.0" >