Merge pull request #7302 from osmandapp/conditionalRouting

Implement conditional restrictions
This commit is contained in:
vshcherb 2019-07-22 12:04:03 +02:00 committed by GitHub
commit dfe3576cac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 224 additions and 66 deletions

View file

@ -41,7 +41,8 @@ public class BinaryMapRouteReaderAdapter {
private static class RouteTypeCondition {
String condition = "";
OpeningHoursParser.OpeningHours hours = null;
float floatValue;
String value;
int ruleid;
}
public static class RouteTypeRule {
@ -85,7 +86,7 @@ public class BinaryMapRouteReaderAdapter {
public String getTag() {
return t;
}
public String getValue(){
return v;
}
@ -101,6 +102,14 @@ public class BinaryMapRouteReaderAdapter {
public boolean conditional() {
return conditions != null;
}
public String getNonConditionalTag() {
String tag = getTag();
if(tag != null && tag.endsWith(":conditional")) {
tag = tag.substring(0, tag.length() - ":conditional".length());
}
return tag;
}
public int onewayDirection(){
if(type == ONEWAY){
@ -109,17 +118,21 @@ public class BinaryMapRouteReaderAdapter {
return 0;
}
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;
}
public int conditionalValue(long time) {
if (conditional()) {
Calendar i = Calendar.getInstance();
i.setTimeInMillis(time);
for (RouteTypeCondition c : conditions) {
if (c.hours != null && c.hours.isOpenedForTime(i)) {
return c.ruleid;
}
}
}
return 0;
}
public float maxSpeed(){
if(type == MAXSPEED){
return floatValue;
}
return -1;
@ -159,16 +172,14 @@ public class BinaryMapRouteReaderAdapter {
type = ROUNDABOUT;
} else if(t.equalsIgnoreCase("highway") && v != null){
type = HIGHWAY_TYPE;
} else if(t.startsWith("access") && v != null){
type = ACCESS;
} else if(t.equalsIgnoreCase("maxspeed:conditional") && v != null){
} else if(t.endsWith(":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 = RouteDataObject.parseSpeed(c.substring(0, ch), 0);
cond.value = c.substring(0, ch).trim();
cond.condition = c.substring(ch + 1).trim();
if (cond.condition.startsWith("(")) {
cond.condition = cond.condition.substring(1, cond.condition.length()).trim();
@ -180,7 +191,18 @@ public class BinaryMapRouteReaderAdapter {
conditions.add(cond);
}
}
type = MAXSPEED;
// we don't set type for condtiional so they are not used directly
// if(t.startsWith("maxspeed")) {
// type = MAXSPEED;
// } else if(t.startsWith("oneway")) {
// type = ONEWAY;
// } else if(t.startsWith("lanes")) {
// type = LANES;
// } else if(t.startsWith("access")) {
// type = ACCESS;
// }
} else if(t.startsWith("access") && v != null){
type = ACCESS;
} else if(t.equalsIgnoreCase("maxspeed") && v != null){
type = MAXSPEED;
floatValue = RouteDataObject.parseSpeed(v, 0);
@ -265,6 +287,21 @@ public class BinaryMapRouteReaderAdapter {
}
}
public void completeRouteEncodingRules() {
for(int i = 0; i < routeEncodingRules.size(); i++) {
RouteTypeRule rtr = routeEncodingRules.get(i);
if(rtr != null && rtr.conditional()) {
String tag = rtr.getNonConditionalTag();
for(RouteTypeCondition c : rtr.conditions ) {
if(tag != null && c.value != null) {
c.ruleid = findOrCreateRouteType(tag, c.value);
}
}
}
}
}
public List<RouteSubregion> getSubregions(){
return subregions;
}
@ -336,14 +373,8 @@ public class BinaryMapRouteReaderAdapter {
rdo.types = new int[o.types.length];
for (int i = 0; i < o.types.length; i++) {
RouteTypeRule tp = o.region.routeEncodingRules.get(o.types[i]);
int ruleId = searchRouteEncodingRule(tp.getTag(), tp.getValue());
if(ruleId != -1) {
rdo.types[i] = ruleId;
} else {
ruleId = routeEncodingRules.size() ;
initRouteEncodingRule(ruleId, tp.getTag(), tp.getValue());
rdo.types[i] = ruleId;
}
int ruleId = findOrCreateRouteType(tp.getTag(), tp.getValue());
rdo.types[i] = ruleId;
}
}
if (o.pointTypes != null) {
@ -408,7 +439,16 @@ public class BinaryMapRouteReaderAdapter {
}
public int findOrCreateRouteType(String tag, String value) {
int ruleId = searchRouteEncodingRule(tag, value);
if(ruleId == -1) {
ruleId = routeEncodingRules.size() ;
initRouteEncodingRule(ruleId, tag, value);
}
return ruleId;
}
}
// Used in C++
@ -484,6 +524,7 @@ public class BinaryMapRouteReaderAdapter {
int tag = WireFormat.getTagFieldNumber(t);
switch (tag) {
case 0:
region.completeRouteEncodingRules();
return;
case OsmandOdb.OsmAndRoutingIndex.NAME_FIELD_NUMBER :
region.name = codedIS.readString();

View file

@ -186,17 +186,13 @@ public class RouteDataObject {
if(k == getPointsLength() - 1) {
height = endHeight;
} else {
int[] tps = getPointTypes(k);
if (tps != null) {
for (int id : tps) {
RouteTypeRule rt = region.quickGetEncodingRule(id);
if (rt.getTag().equals("osmand_ele_asc")) {
height = (prevHeight + Float.parseFloat(rt.getValue()));
break;
} else if (rt.getTag().equals("osmand_ele_desc")) {
height = (prevHeight - Float.parseFloat(rt.getValue()));
break;
}
String asc = getValue(k, "osmand_ele_asc");
if(asc != null && asc.length() > 0) {
height = (prevHeight + Float.parseFloat(asc));
} else {
String desc = getValue(k, "osmand_ele_desc");
if(desc != null && desc.length() > 0) {
height = (prevHeight - Float.parseFloat(desc));
}
}
}
@ -486,27 +482,49 @@ public class RouteDataObject {
public int[] getTypes() {
return types;
}
public void processConditionalTags(long conditionalTime) {
int sz = types.length;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if (r != null && r.conditional()) {
int vl = r.conditionalValue(conditionalTime);
if(vl != 0) {
RouteTypeRule rtr = region.quickGetEncodingRule(vl);
String nonCondTag = rtr.getTag();
for(int ks = 0; ks < types.length; ks++) {
RouteTypeRule toReplace = region.quickGetEncodingRule(types[ks]);
if(toReplace != null && toReplace.getTag().equals(nonCondTag)) {
types[ks] = vl;
break;
}
}
}
}
}
}
public float getMaximumSpeed(boolean direction){
public float getMaximumSpeed(boolean direction) {
int sz = types.length;
float maxSpeed = 0;
for (int i = 0; i < sz; i++) {
RouteTypeRule r = region.quickGetEncodingRule(types[i]);
if(r.isForward() != 0) {
if((r.isForward() == 1) != direction) {
continue;
}
}
float mx = r.maxSpeed();
if (mx > 0) {
maxSpeed = mx;
// conditional has priority
if(r.conditional()) {
break;
if (r.isForward() != 0) {
if ((r.isForward() == 1) != direction) {
continue;
} else {
// priority over default
maxSpeed = mx;
break;
}
} else {
maxSpeed = mx;
}
}
}
return maxSpeed ;
return maxSpeed;
}
public static float parseSpeed(String v, float def) {
@ -671,6 +689,34 @@ public class RouteDataObject {
return r.getValue();
}
}
if (nameIds != null) {
for (int i = 0; i < nameIds.length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(nameIds[i]);
if (r.getTag().equals(tag)) {
return names.get(nameIds[i]);
}
}
}
return null;
}
public String getValue(int pnt, String tag) {
if (pointTypes != null && pnt < pointTypes.length && pointTypes[pnt] != null) {
for (int i = 0; i < pointTypes[pnt].length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pointTypes[pnt][i]);
if (r.getTag().equals(tag)) {
return r.getValue();
}
}
}
if (pointNameTypes != null && pnt < pointNameTypes.length && pointNameTypes[pnt] != null) {
for (int i = 0; i < pointNameTypes[pnt].length; i++) {
RouteTypeRule r = region.quickGetEncodingRule(pointNameTypes[pnt][i]);
if (r.getTag().equals(tag)) {
return pointNames[pnt][i];
}
}
}
return null;
}

View file

@ -46,7 +46,10 @@ public class RoutingConfiguration {
// 1.5 Recalculate distance help
public float recalculateDistance = 20000f;
// 1.6 Time to calculate all access restrictions based on conditions
public long routeCalculationTime = 0;
public static class Builder {
// Design time storage
private String defaultRouter = "";
@ -66,6 +69,7 @@ public class RoutingConfiguration {
public RoutingConfiguration build(String router, int memoryLimitMB, Map<String, String> params) {
return build(router, null, memoryLimitMB, params);
}
public RoutingConfiguration build(String router, Double direction, int memoryLimitMB, Map<String, String> params) {
if (!routers.containsKey(router)) {
router = defaultRouter;
@ -96,7 +100,6 @@ public class RoutingConfiguration {
}
i.planRoadDirection = parseSilentInt(getAttribute(i.router, "planRoadDirection"), i.planRoadDirection);
// i.planRoadDirection = 1;
return i;
}

View file

@ -1,12 +1,6 @@
package net.osmand.router;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.iterator.TLongIterator;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.hash.TLongHashSet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
@ -18,6 +12,13 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.iterator.TLongIterator;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.hash.TLongHashSet;
import net.osmand.NativeLibrary;
import net.osmand.NativeLibrary.NativeRouteSearchResult;
import net.osmand.PlatformUtil;
@ -32,8 +33,6 @@ import net.osmand.router.BinaryRoutePlanner.RouteSegment;
import net.osmand.router.BinaryRoutePlanner.RouteSegmentVisitor;
import net.osmand.router.RoutePlannerFrontEnd.RouteCalculationMode;
import org.apache.commons.logging.Log;
public class RoutingContext {
@ -70,7 +69,7 @@ public class RoutingContext {
public boolean leftSideNavigation;
public List<RouteSegmentResult> previouslyCalculatedRoute;
public PrecalculatedRouteDirection precalculatedRouteDirection;
// 2. Routing memory cache (big objects)
TLongObjectHashMap<List<RoutingSubregionTile>> indexedSubregions = new TLongObjectHashMap<List<RoutingSubregionTile>>();
TLongObjectHashMap<List<RouteDataObject>> tileRoutes = new TLongObjectHashMap<List<RouteDataObject>>();
@ -110,8 +109,6 @@ public class RoutingContext {
public FinalRouteSegment finalRouteSegment;
RoutingContext(RoutingContext cp) {
this.config = cp.config;
this.map.putAll(cp.map);
@ -316,12 +313,15 @@ public class RoutingContext {
BinaryMapIndexReader reader = reverseMap.get(ts.subregion.routeReg);
ts.setLoadedNonNative();
List<RouteDataObject> res = reader.loadRouteIndexData(ts.subregion);
// System.out.println(ts.subregion.shiftToData + " " + res);
if(toLoad != null) {
toLoad.addAll(res);
} else {
for(RouteDataObject ro : res){
if(ro != null) {
if(config.routeCalculationTime != 0) {
ro.processConditionalTags(config.routeCalculationTime);
}
if(config.router.acceptLine(ro)) {
if(excludeNotAllowed != null && !excludeNotAllowed.contains(ro.getId())) {
ts.add(ro);
@ -617,7 +617,6 @@ public class RoutingContext {
int cnt = 4;
while (cnt-- >= 0) {
for (int i = 0; (usedMem1 < usedMem2) && (i < 1000); ++i) {
// AVIAN FIXME
runtime.runFinalization();
runtime.gc();
Thread.yield();
@ -709,9 +708,11 @@ public class RoutingContext {
ctx.timeToLoad += (System.nanoTime() - nanoTime);
if (res != null) {
for (RouteDataObject ro : res) {
boolean accept = ro != null;
if (ctx != null) {
if (ctx != null && ro != null) {
if(ctx.config.routeCalculationTime != 0) {
ro.processConditionalTags(ctx.config.routeCalculationTime);
}
accept = ctx.getRouter().acceptLine(ro);
}
if (accept) {

View file

@ -11,6 +11,7 @@
Thx - Hardy
-->
<string name="temporary_conditional_routing">Consider temporary limitations</string>
<string name="turn_on_profile_desc">Please turn on at least one application profile, to use this setting.</string>
<string name="rendering_attr_winter_road_name">Winter road</string>
<string name="rendering_attr_ice_road_name">Ice road</string>

View file

@ -33,6 +33,9 @@
android:key="snap_to_road"
android:summary="@string/snap_to_road_descr"
android:title="@string/snap_to_road"/>
<CheckBoxPreference
android:key="enable_time_conditional_routing"
android:title="@string/temporary_conditional_routing"/>
<Preference
android:key="show_routing_alarms"
android:summary="@string/show_warnings_descr"

View file

@ -139,6 +139,7 @@ public class CurrentPositionHelper {
}
RoutingConfiguration cfg = app.getRoutingConfig().build(p, 10,
new HashMap<String, String>());
cfg.routeCalculationTime = System.currentTimeMillis();
ctx = new RoutePlannerFrontEnd().buildRoutingContext(cfg, null, rs);
RoutingConfiguration defCfg = app.getRoutingConfig().build("geocoding", 10,
new HashMap<String, String>());

View file

@ -1222,8 +1222,9 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name
public final OsmandPreference<Boolean> FAST_ROUTE_MODE = new BooleanPreference("fast_route_mode", true).makeProfile();
// temporarily for new version
// dev version
public final CommonPreference<Boolean> DISABLE_COMPLEX_ROUTING = new BooleanPreference("disable_complex_routing", false).makeGlobal();
public final CommonPreference<Boolean> ENABLE_TIME_CONDITIONAL_ROUTING = new BooleanPreference("enable_time_conditional_routing", true).makeGlobal();
public final CommonPreference<Boolean> SHOW_TRAFFIC_WARNINGS = new BooleanPreference("show_traffic_warnings", false).makeProfile().cache();

View file

@ -209,6 +209,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
registerBooleanPreference(settings.ENABLE_TIME_CONDITIONAL_ROUTING, screen);
addTurnScreenOn((PreferenceGroup) screen.findPreference("turn_screen_on"));

View file

@ -41,6 +41,7 @@ import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.MuteSoundRoutin
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.OtherSettingsRoutingParameter;
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.RouteSimulationItem;
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.ShowAlongTheRouteItem;
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.TimeConditionalRoutingItem;
import net.osmand.plus.routing.RouteProvider;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.router.GeneralRouter;
@ -96,6 +97,8 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
items.add(createAvoidRoadsItem(optionsItem));
} else if (optionsItem instanceof GpxLocalRoutingParameter) {
items.add(createGpxRoutingItem(optionsItem));
} else if (optionsItem instanceof TimeConditionalRoutingItem) {
items.add(createTimeConditionalRoutingItem(optionsItem));
} else if (optionsItem instanceof OtherSettingsRoutingParameter) {
items.add(createOtherSettingsRoutingItem(optionsItem));
} else {
@ -158,6 +161,26 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
return muteSoundItem[0];
}
private BaseBottomSheetItem createTimeConditionalRoutingItem(final LocalRoutingParameter optionsItem) {
final BottomSheetItemWithCompoundButton[] timeConditionalRoutingItem = new BottomSheetItemWithCompoundButton[1];
timeConditionalRoutingItem[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder()
.setChecked(settings.ENABLE_TIME_CONDITIONAL_ROUTING.get())
.setIcon(getContentIcon((optionsItem.getActiveIconId())))
.setTitle(getString(R.string.temporary_conditional_routing))
.setLayoutId(R.layout.bottom_sheet_item_with_descr_and_switch_56dp)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean enabled = !settings.ENABLE_TIME_CONDITIONAL_ROUTING.get();
settings.ENABLE_TIME_CONDITIONAL_ROUTING.set(enabled);
timeConditionalRoutingItem[0].setChecked(enabled);
app.getRoutingHelper().recalculateRouteDueToSettingsChange();
}
})
.create();
return timeConditionalRoutingItem[0];
}
private BaseBottomSheetItem createShowAlongTheRouteItem(final LocalRoutingParameter optionsItem) {
return new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon((optionsItem.getActiveIconId())))
@ -455,6 +478,7 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
ShowAlongTheRouteItem.KEY,
GeneralRouter.ALLOW_PRIVATE,
GeneralRouter.USE_SHORTEST_WAY,
TimeConditionalRoutingItem.KEY,
DividerItem.KEY,
GpxLocalRoutingParameter.KEY,
OtherSettingsRoutingParameter.KEY,
@ -467,6 +491,7 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
GeneralRouter.ALLOW_MOTORWAYS,
AvoidRoadsRoutingParameter.KEY,
ShowAlongTheRouteItem.KEY,
TimeConditionalRoutingItem.KEY,
DividerItem.KEY,
GpxLocalRoutingParameter.KEY,
OtherSettingsRoutingParameter.KEY,
@ -477,6 +502,7 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
DividerItem.KEY,
AvoidRoadsRoutingParameter.KEY,
ShowAlongTheRouteItem.KEY,
TimeConditionalRoutingItem.KEY,
DividerItem.KEY,
GpxLocalRoutingParameter.KEY,
OtherSettingsRoutingParameter.KEY,
@ -487,12 +513,14 @@ public class RouteOptionsBottomSheet extends MenuBottomSheetDialogFragment {
AvoidPTTypesRoutingParameter.KEY,
// ShowAlongTheRouteItem.KEY,
// DividerItem.KEY,
TimeConditionalRoutingItem.KEY,
OtherSettingsRoutingParameter.KEY),
OTHER(MuteSoundRoutingParameter.KEY,
DividerItem.KEY,
AvoidRoadsRoutingParameter.KEY,
ShowAlongTheRouteItem.KEY,
TimeConditionalRoutingItem.KEY,
DividerItem.KEY,
GpxLocalRoutingParameter.KEY,
OtherSettingsRoutingParameter.KEY,

View file

@ -370,6 +370,8 @@ public class RoutingOptionsHelper {
return new AvoidRoadsRoutingParameter();
case GpxLocalRoutingParameter.KEY:
return new GpxLocalRoutingParameter();
case TimeConditionalRoutingItem.KEY:
return new TimeConditionalRoutingItem();
case OtherSettingsRoutingParameter.KEY:
return new OtherSettingsRoutingParameter();
default:
@ -413,6 +415,7 @@ public class RoutingOptionsHelper {
list.add(1, new VoiceGuidanceRoutingParameter());
list.add(2, new InterruptMusicRoutingParameter());
list.add(3, new AvoidRoadsRoutingParameter());
list.add(4, new TimeConditionalRoutingItem());
list.add(new GpxLocalRoutingParameter());
list.add(new OtherSettingsRoutingParameter());
return list;
@ -750,6 +753,33 @@ public class RoutingOptionsHelper {
}
}
public static class TimeConditionalRoutingItem extends LocalRoutingParameter {
public static final String KEY = "TimeConditionalRoutingItem";
public String getKey() {
return KEY;
}
public boolean canAddToRouteMenu() {
return false;
}
public TimeConditionalRoutingItem() {
super(null);
}
@Override
public int getActiveIconId() {
return R.drawable.ic_action_road_works_dark;
}
@Override
public int getDisabledIconId() {
return R.drawable.ic_action_road_works_dark;
}
}
public static class ShowAlongTheRouteItem extends LocalRoutingParameter {
public static final String KEY = "ShowAlongTheRouteItem";

View file

@ -709,7 +709,6 @@ public class RouteProvider {
if (maxSpeed > 0) {
paramsR.put(GeneralRouter.MAX_SPEED, String.valueOf(maxSpeed));
}
float mb = (1 << 20);
Runtime rt = Runtime.getRuntime();
// make visible
@ -718,6 +717,9 @@ public class RouteProvider {
RoutingConfiguration cf = config.build( params.mode.getRoutingProfile(), params.start.hasBearing() ?
params.start.getBearing() / 180d * Math.PI : null,
memoryLimit, paramsR);
if(settings.ENABLE_TIME_CONDITIONAL_ROUTING.get()) {
cf.routeCalculationTime = System.currentTimeMillis();
}
return cf;
}