Skip to content

Commit

Permalink
Refactor direction engines and unify structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwin Hochfelsner committed Feb 6, 2025
1 parent 361dcbb commit 3528239
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 249 deletions.
231 changes: 107 additions & 124 deletions app/assets/javascripts/index/directions/fossgis_osrm.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// OSRM engine
// Doesn't yet support hints

function FOSSGISOSRMEngine(id, vehicleType) {
var cachedHints = [];
(function () {
function FOSSGISOSRMEngine(id, vehicleType) {
let cachedHints = [];

return {
id: id,
creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
draggable: true,

_transformSteps: function (input_steps, line) {
var INSTRUCTION_TEMPLATE = {
function _processDirections(route) {
const INSTRUCTION_TEMPLATE = {
"continue": "javascripts.directions.instructions.continue",
"merge right": "javascripts.directions.instructions.merge_right",
"merge left": "javascripts.directions.instructions.merge_left",
Expand All @@ -37,7 +33,7 @@ function FOSSGISOSRMEngine(id, vehicleType) {
"depart": "javascripts.directions.instructions.start",
"arrive": "javascripts.directions.instructions.destination"
};
var ICON_MAP = {
const ICON_MAP = {
"continue": 0,
"merge right": 21,
"merge left": 20,
Expand All @@ -64,149 +60,136 @@ function FOSSGISOSRMEngine(id, vehicleType) {
"depart": 8,
"arrive": 14
};
var numToWord = function (num) {
function numToWord(num) {
return ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"][num - 1];
};
var transformed_steps = input_steps.map(function (step, idx) {
var maneuver_id;

}
function getManeuverId(maneuver) {
// special case handling
switch (step.maneuver.type) {
switch (maneuver.type) {
case "on ramp":
case "off ramp":
case "merge":
case "end of road":
case "fork":
maneuver_id = step.maneuver.type + " " + (step.maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
break;
return maneuver.type + " " + (maneuver.modifier.indexOf("left") >= 0 ? "left" : "right");
case "depart":
case "arrive":
case "roundabout":
case "rotary":
case "exit roundabout":
case "exit rotary":
maneuver_id = step.maneuver.type;
break;
return maneuver.type;
case "roundabout turn":
case "turn":
maneuver_id = "turn " + step.maneuver.modifier;
break;
// for unknown types the fallback is turn
return "turn " + maneuver.modifier;
// for unknown types the fallback is turn
default:
maneuver_id = "turn " + step.maneuver.modifier;
break;
}
var template = INSTRUCTION_TEMPLATE[maneuver_id];

// convert lat,lng pairs to LatLng objects
var step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(function (a) { return L.latLng(a); });
// append step_geometry on line
Array.prototype.push.apply(line, step_geometry);

var instText = "<b>" + (idx + 1) + ".</b> ";
var destinations = "<b>" + step.destinations + "</b>";
var namedRoad = true;
var name;

if (step.name && step.ref) {
name = "<b>" + step.name + " (" + step.ref + ")</b>";
} else if (step.name) {
name = "<b>" + step.name + "</b>";
} else if (step.ref) {
name = "<b>" + step.ref + "</b>";
} else {
name = I18n.t("javascripts.directions.instructions.unnamed");
namedRoad = false;
return "turn " + maneuver.modifier;
}
}

const steps = route.legs.flatMap(
leg => leg.steps.map(function (step, idx) {
const maneuver_id = getManeuverId(step.maneuver);

let template = INSTRUCTION_TEMPLATE[maneuver_id];

if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
instText += I18n.t(template, { name: name });
} else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
if (step.maneuver.exit) {
if (step.maneuver.exit <= 10) {
instText += I18n.t(template + "_with_exit_ordinal", { exit: I18n.t("javascripts.directions.instructions.exit_counts." + numToWord(step.maneuver.exit)), name: name });
const step_geometry = L.PolylineUtil.decode(step.geometry, { precision: 5 }).map(L.latLng);

let instText = "<b>" + (idx + 1) + ".</b> ";
const destinations = "<b>" + step.destinations + "</b>";
let namedRoad = true;
let name;

if (step.name && step.ref) {
name = "<b>" + step.name + " (" + step.ref + ")</b>";
} else if (step.name) {
name = "<b>" + step.name + "</b>";
} else if (step.ref) {
name = "<b>" + step.ref + "</b>";
} else {
name = I18n.t("javascripts.directions.instructions.unnamed");
namedRoad = false;
}

if (step.maneuver.type.match(/^exit (rotary|roundabout)$/)) {
instText += I18n.t(template, { name: name });
} else if (step.maneuver.type.match(/^(rotary|roundabout)$/)) {
if (step.maneuver.exit) {
if (step.maneuver.exit <= 10) {
instText += I18n.t(template + "_with_exit_ordinal", { exit: I18n.t("javascripts.directions.instructions.exit_counts." + numToWord(step.maneuver.exit)), name: name });
} else {
instText += I18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
}
} else {
instText += I18n.t(template + "_with_exit", { exit: step.maneuver.exit, name: name });
instText += I18n.t(template + "_without_exit", { name: name });
}
} else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
const params = {};
if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
if (step.destinations) params.directions = destinations;
if (namedRoad) params.directions = name;
if (Object.keys(params).length > 0) {
template = template + "_with_" + Object.keys(params).join("_");
}
instText += I18n.t(template, params);
} else {
instText += I18n.t(template + "_without_exit", { name: name });
}
} else if (step.maneuver.type.match(/^(on ramp|off ramp)$/)) {
var params = {};
if (step.exits && step.maneuver.type.match(/^(off ramp)$/)) params.exit = step.exits;
if (step.destinations) params.directions = destinations;
if (namedRoad) params.directions = name;
if (Object.keys(params).length > 0) {
template = template + "_with_" + Object.keys(params).join("_");
}
instText += I18n.t(template, params);
} else {
instText += I18n.t(template + "_without_exit", { name: name });
}
return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
});

return transformed_steps;
},

getRoute: function (points, callback) {
var params = [
{ name: "overview", value: "false" },
{ name: "geometries", value: "polyline" },
{ name: "steps", value: true }
];


if (cachedHints.length === points.length) {
params.push({ name: "hints", value: cachedHints.join(";") });
} else {
// invalidate cache
cachedHints = [];
}
return [[step.maneuver.location[1], step.maneuver.location[0]], ICON_MAP[maneuver_id], instText, step.distance, step_geometry];
})
);

return {
line: steps.flatMap(step => step[4]),
steps,
distance: route.distance,
time: route.duration
};
}

var encoded_coords = points.map(function (p) {
return p.lng + "," + p.lat;
}).join(";");
return {
id: id,
creditline: "<a href=\"https://routing.openstreetmap.de/about.html\" target=\"_blank\">OSRM (FOSSGIS)</a>",
draggable: true,

var req_url = OSM.FOSSGIS_OSRM_URL + "routed-" + vehicleType + "/route/v1/driving/" + encoded_coords;
getRoute: function (points, callback) {
const data = [
{ name: "overview", value: "false" },
{ name: "geometries", value: "polyline" },
{ name: "steps", value: true }
];

var onResponse = function (data) {
if (data.code !== "Ok") {
return callback(true);
if (cachedHints.length === points.length) {
data.push({ name: "hints", value: cachedHints.join(";") });
} else {
// invalidate cache
cachedHints = [];
}

cachedHints = data.waypoints.map(function (wp) {
return wp.hint;
});
const req_path = "routed-" + vehicleType + "/route/v1/driving/" + points.map(p => p.lng + "," + p.lat).join(";");

var line = [];
var transformLeg = function (leg) {
return this._transformSteps(leg.steps, line);
};

var steps = [].concat.apply([], data.routes[0].legs.map(transformLeg.bind(this)));
return $.ajax({
url: OSM.FOSSGIS_OSRM_URL + req_path,
data,
dataType: "json",
success: function (response) {
if (response.code !== "Ok") {
return callback(true);
}

callback(false, {
line: line,
steps: steps,
distance: data.routes[0].distance,
time: data.routes[0].duration
cachedHints = response.waypoints.map(wp => wp.hint);
callback(false, _processDirections(response.routes[0]));
},
error: function () {
callback(true);
}
});
};

return $.ajax({
url: req_url,
data: params,
dataType: "json",
success: onResponse.bind(this),
error: function () {
callback(true);
}
});
}
};
}
}
};
}

OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_car", "car"), true);
OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_bike", "bike"), true);
OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_foot", "foot"), true);
OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_car", "car"), true);
OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_bike", "bike"), true);
OSM.Directions.addEngine(new FOSSGISOSRMEngine("fossgis_osrm_foot", "foot"), true);
}());
Loading

0 comments on commit 3528239

Please sign in to comment.