Refactor MQ/OSRM engines a la @karussell patch

This commit is contained in:
Richard Fairhurst 2014-03-08 13:37:27 +00:00
parent dd4406ed41
commit a997690897
5 changed files with 139 additions and 110 deletions

View file

@ -0,0 +1,81 @@
// see:
// http://developer.mapquest.com/web/products/open/directions-service
// http://open.mapquestapi.com/directions/
// https://github.com/apmon/openstreetmap-website/blob/21edc353a4558006f0ce23f5ec3930be6a7d4c8b/app/controllers/routing_controller.rb#L153
MapQuestEngine = function(vehicleName, vehicleParam, locale) {
this.vehicleName = vehicleName;
this.vehicleParam = vehicleParam;
this.locale = locale;
if (!locale)
this.locale = "en";
};
MapQuestEngine.prototype.createConfig = function() {
var that = this;
return {
name: "javascripts.directions.engines.mapquest_"+this.vehicleName.toLowerCase(),
creditline: 'Directions courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">',
draggable: false,
_hints: {},
MQ_SPRITE_MAP: {
0: 1, // straight
1: 2, // slight right
2: 3, // right
3: 4, // sharp right
4: 5, // reverse
5: 6, // sharp left
6: 7, // left
7: 8, // slight left
8: 5, // right U-turn
9: 5, // left U-turn
10: 2, // right merge
11: 8, // left merge
12: 2, // right on-ramp
13: 8, // left on-ramp
14: 2, // right off-ramp
15: 8, // left off-ramp
16: 2, // right fork
17: 8, // left fork
18: 1 // straight fork
},
getRoute: function(isFinal,points) {
var url="http://open.mapquestapi.com/directions/v2/route?key=Fmjtd%7Cluur290anu%2Crl%3Do5-908a0y";
var from=points[0]; var to=points[points.length-1];
url+="&from="+from.join(',');
url+="&to="+to.join(',');
url+="&"+that.vehicleParam;
//url+="&locale=" + I18n.currentLocale(); //Doesn't actually work. MapQuest requires full locale e.g. "de_DE", but I18n only provides language, e.g. "de"
url+="&manMaps=false";
url+="&shapeFormat=raw&generalize=0";
this.requestJSONP(url+"&callback=");
},
gotRoute: function(router,data) {
// *** what if no route?
var poly=[];
var shape=data.route.shape.shapePoints;
for (var i=0; i<shape.length; i+=2) {
poly.push(L.latLng(shape[i],shape[i+1]));
}
router.setPolyline(poly);
// data.shape.maneuverIndexes links turns to polyline positions
// data.legs[0].maneuvers is list of turns
var steps=[];
var mq=data.route.legs[0].maneuvers;
for (var i=0; i<mq.length; i++) {
var s=mq[i];
var d=(i==mq.length-1) ? 15: this.MQ_SPRITE_MAP[s.turnType];
steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance*1000]);
}
router.setItinerary( { steps: steps });
return true;
}
};
};
OSM.RoutingEngines.list.push(new MapQuestEngine("Bicycle", "routeType=bicycle").createConfig());
OSM.RoutingEngines.list.push(new MapQuestEngine("Foot", "routeType=pedestrian").createConfig());
OSM.RoutingEngines.list.push(new MapQuestEngine("Car", "routeType=fastest").createConfig());
// can be: routeType=fastest|shortest|pedestrian|multimodal|bicycle

View file

@ -1,65 +0,0 @@
// see:
// http://developer.mapquest.com/web/products/open/directions-service
// http://open.mapquestapi.com/directions/
// https://github.com/apmon/openstreetmap-website/blob/21edc353a4558006f0ce23f5ec3930be6a7d4c8b/app/controllers/routing_controller.rb#L153
OSM.RoutingEngines.list.push({
name: "javascripts.directions.engines.mapquest_bike",
creditline: 'Directions courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">',
draggable: false,
_hints: {},
MQ_SPRITE_MAP: {
0: 1, // straight
1: 2, // slight right
2: 3, // right
3: 4, // sharp right
4: 5, // reverse
5: 6, // sharp left
6: 7, // left
7: 8, // slight left
8: 5, // right U-turn
9: 5, // left U-turn
10: 2, // right merge
11: 8, // left merge
12: 2, // right on-ramp
13: 8, // left on-ramp
14: 2, // right off-ramp
15: 8, // left off-ramp
16: 2, // right fork
17: 8, // left fork
18: 1 // straight fork
},
getRoute: function(isFinal,points) {
var url="http://open.mapquestapi.com/directions/v2/route?key=Fmjtd%7Cluur290anu%2Crl%3Do5-908a0y";
var from=points[0]; var to=points[points.length-1];
url+="&from="+from.join(',');
url+="&to="+to.join(',');
url+="&routeType=bicycle";
//url+="&locale=" + I18n.currentLocale(); //Doesn't actually work. MapQuest requires full locale e.g. "de_DE", but I18n only provides language, e.g. "de"
url+="&manMaps=false";
url+="&shapeFormat=raw&generalize=0";
this.requestJSONP(url+"&callback=");
},
gotRoute: function(router,data) {
// *** what if no route?
var poly=[];
var shape=data.route.shape.shapePoints;
for (var i=0; i<shape.length; i+=2) {
poly.push(L.latLng(shape[i],shape[i+1]));
}
router.setPolyline(poly);
// data.shape.maneuverIndexes links turns to polyline positions
// data.legs[0].maneuvers is list of turns
var steps=[];
var mq=data.route.legs[0].maneuvers;
for (var i=0; i<mq.length; i++) {
var s=mq[i];
var d=(i==mq.length-1) ? 15: this.MQ_SPRITE_MAP[s.turnType];
steps.push([L.latLng(s.startPoint.lat, s.startPoint.lng), d, s.narrative, s.distance*1000]);
}
router.setItinerary( { steps: steps });
return true;
}
});

View file

@ -0,0 +1,55 @@
// OSRM car engine
// *** need to clear hints at some point
OSRMEngine = function(vehicleName, baseURL, locale) {
this.vehicleName = vehicleName;
this.baseURL = baseURL;
this.locale = locale;
if (!locale)
this.locale = "en";
};
OSRMEngine.prototype.createConfig = function() {
var that = this;
return {
name: "javascripts.directions.engines.osrm_"+this.vehicleName.toLowerCase(),
creditline: 'Directions courtesy of <a href="http://project-osrm.org/" target="_blank">OSRM</a>',
draggable: true,
_hints: {},
getRoute: function(isFinal,points) {
var url=that.baseURL+"?z=14&output=json";
for (var i=0; i<points.length; i++) {
var pair=points[i].join(',');
url+="&loc="+pair;
if (this._hints[pair]) url+= "&hint="+this._hints[pair];
}
if (isFinal) url+="&instructions=true";
this.requestJSONP(url+"&jsonp=");
},
gotRoute: function(router,data) {
if (data.status==207) {
return false;
}
// Draw polyline
var line=L.PolylineUtil.decode(data.route_geometry);
for (i=0; i<line.length; i++) { line[i].lat/=10; line[i].lng/=10; }
router.setPolyline(line);
// *** store hints
// Assemble instructions
var steps=[];
for (i=0; i<data.route_instructions.length; i++) {
var s=data.route_instructions[i];
var instCodes=s[0].split('-');
var instText="<b>"+(i+1)+".</b> ";
instText+=TURN_INSTRUCTIONS[instCodes[0]];
if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; }
if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : I18n.t('javascripts.directions.instructions.unnamed'); }
steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]);
}
if (steps.length) router.setItinerary({ steps: steps });
return true;
}
};
};
OSM.RoutingEngines.list.push(new OSRMEngine("Car", "http://router.project-osrm.org/viaroute").createConfig());

View file

@ -1,43 +0,0 @@
// OSRM car engine
// *** this should all be shared from an OSRM library somewhere
// *** need to clear hints at some point
OSM.RoutingEngines.list.push({
name: "javascripts.directions.engines.osrm_car",
creditline: 'Directions courtesy of <a href="http://project-osrm.org/" target="_blank">OSRM</a>',
draggable: true,
_hints: {},
getRoute: function(isFinal,points) {
var url="http://router.project-osrm.org/viaroute?z=14&output=json";
for (var i=0; i<points.length; i++) {
var pair=points[i].join(',');
url+="&loc="+pair;
if (this._hints[pair]) url+= "&hint="+this._hints[pair];
}
if (isFinal) url+="&instructions=true";
this.requestJSONP(url+"&jsonp=");
},
gotRoute: function(router,data) {
if (data.status==207) {
return false;
}
// Draw polyline
var line=L.PolylineUtil.decode(data.route_geometry);
for (i=0; i<line.length; i++) { line[i].lat/=10; line[i].lng/=10; }
router.setPolyline(line);
// *** store hints
// Assemble instructions
var steps=[];
for (i=0; i<data.route_instructions.length; i++) {
var s=data.route_instructions[i];
var instCodes=s[0].split('-');
var instText="<b>"+(i+1)+".</b> ";
instText+=TURN_INSTRUCTIONS[instCodes[0]];
if (instCodes[1]) { instText+="exit "+instCodes[1]+" "; }
if (instCodes[0]!=15) { instText+=s[1] ? "<b>"+s[1]+"</b>" : I18n.t('javascripts.directions.instructions.unnamed'); }
steps.push([line[s[3]], s[0].split('-')[0], instText, s[2]]);
}
if (steps.length) router.setItinerary({ steps: steps });
return true;
}
});

View file

@ -2122,9 +2122,10 @@ en:
engines: engines:
graphhopper_bicycle: "Bicycle (GraphHopper)" graphhopper_bicycle: "Bicycle (GraphHopper)"
graphhopper_foot: "Foot (GraphHopper)" graphhopper_foot: "Foot (GraphHopper)"
mapquest_bike: "Bicycle (MapQuest)" mapquest_bicycle: "Bicycle (MapQuest)"
mapquest_car: "Car (MapQuest)"
mapquest_foot: "Foot (MapQuest)"
osrm_car: "Car (OSRM)" osrm_car: "Car (OSRM)"
cloudmade_foot: "Foot (Cloudmade)"
directions: "Directions" directions: "Directions"
errors: errors:
no_route: "Couldn't find a route between those two places." no_route: "Couldn't find a route between those two places."