Implement maxspeed:conditional

This commit is contained in:
vshcherb 2013-09-12 01:24:09 +02:00
parent ccf955076d
commit 12974c9926
2 changed files with 73 additions and 27 deletions

View file

@ -7,10 +7,7 @@ import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
@ -28,6 +25,7 @@ import net.osmand.binary.OsmandOdb.RestrictionData;
import net.osmand.binary.OsmandOdb.RouteData;
import net.osmand.util.MapUtils;
import net.osmand.util.OpeningHoursParser;
import org.apache.commons.logging.Log;
import com.google.protobuf.CodedInputStream;
@ -38,6 +36,14 @@ public class BinaryMapRouteReaderAdapter {
protected static final Log LOG = PlatformUtil.getLog(BinaryMapRouteReaderAdapter.class);
private static final int SHIFT_COORDINATES = 4;
private static class RouteTypeCondition {
String condition = "";
OpeningHoursParser.OpeningHours hours = null;
int intValue;
float floatValue;
}
public static class RouteTypeRule {
private final static int ACCESS = 1;
private final static int ONEWAY = 2;
@ -52,6 +58,7 @@ public class BinaryMapRouteReaderAdapter {
private int intValue;
private float floatValue;
private int type;
private List<RouteTypeCondition> conditions = null;
public RouteTypeRule(String t, String v) {
this.t = t.intern();
@ -81,6 +88,10 @@ public class BinaryMapRouteReaderAdapter {
return type;
}
public boolean conditional() {
return conditions != null;
}
public int onewayDirection(){
if(type == ONEWAY){
return intValue;
@ -90,6 +101,15 @@ public class BinaryMapRouteReaderAdapter {
public float maxSpeed(){
if(type == MAXSPEED){
if(conditions != null) {
Calendar i = Calendar.getInstance();
i.setTimeInMillis(System.currentTimeMillis());
for(RouteTypeCondition c : conditions) {
if(c.hours != null && c.hours.isOpenedForTime(i)) {
return c.floatValue;
}
}
}
return floatValue;
}
return -1;
@ -131,9 +151,41 @@ public class BinaryMapRouteReaderAdapter {
type = HIGHWAY_TYPE;
} else if(t.startsWith("access") && v != null){
type = ACCESS;
} else if(t.equalsIgnoreCase("maxspeed:conditional") && v != null){
conditions = new ArrayList<RouteTypeCondition>();
String[] cts = v.split(";");
for(String c : cts) {
int ch = c.indexOf('@');
if(ch > 0) {
RouteTypeCondition cond = new RouteTypeCondition();
cond.floatValue = parseMaxSpeed(c.substring(ch));
cond.condition = c.substring(ch + 1).trim();
if(cond.condition.startsWith("(") && cond.condition.endsWith(")")) {
cond.condition = cond.condition.substring(1, cond.condition.length() - 1).trim();
}
cond.hours = OpeningHoursParser.parseOpenedHours(cond.condition);
}
}
type = MAXSPEED;
} else if(t.equalsIgnoreCase("maxspeed") && v != null){
type = MAXSPEED;
floatValue = -1;
floatValue = parseMaxSpeed(v);
} else if (t.equalsIgnoreCase("lanes") && v != null) {
intValue = -1;
int i = 0;
type = LANES;
while (i < v.length() && Character.isDigit(v.charAt(i))) {
i++;
}
if (i > 0) {
intValue = Integer.parseInt(v.substring(0, i));
}
}
}
private float parseMaxSpeed(String v) {
float floatValue = -1;
if(v.equals("none")) {
floatValue = RouteDataObject.NONE_MAX_SPEED;
} else {
@ -149,18 +201,7 @@ public class BinaryMapRouteReaderAdapter {
}
}
}
} else if (t.equalsIgnoreCase("lanes") && v != null) {
intValue = -1;
int i = 0;
type = LANES;
while (i < v.length() && Character.isDigit(v.charAt(i))) {
i++;
}
if (i > 0) {
intValue = Integer.parseInt(v.substring(0, i));
}
}
return floatValue;
}
}

View file

@ -144,14 +144,19 @@ public class RouteDataObject {
public float getMaximumSpeed(){
int sz = types.length;
float maxSpeed = 0;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
float maxSpeed = r.maxSpeed();
if (maxSpeed > 0) {
return maxSpeed;
float mx = r.maxSpeed();
if (mx > 0) {
maxSpeed = mx;
// conditional has priority
if(r.conditional()) {
break;
}
}
return 0;
}
return maxSpeed ;
}
public boolean loop(){