2011-07-08 13:27:16 -04:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, others 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VIAROUTEPLUGIN_H_
|
|
|
|
#define VIAROUTEPLUGIN_H_
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "BasePlugin.h"
|
|
|
|
#include "RouteParameters.h"
|
2012-03-05 13:08:10 -05:00
|
|
|
|
|
|
|
#include "../Algorithms/ObjectToBase64.h"
|
|
|
|
|
2011-11-17 12:56:45 -05:00
|
|
|
#include "../Descriptors/BaseDescriptor.h"
|
|
|
|
#include "../Descriptors/GPXDescriptor.h"
|
|
|
|
#include "../Descriptors/JSONDescriptor.h"
|
2011-07-08 13:27:16 -04:00
|
|
|
|
|
|
|
#include "../DataStructures/HashTable.h"
|
2012-04-25 04:51:16 -04:00
|
|
|
#include "../DataStructures/QueryEdge.h"
|
2011-07-08 13:27:16 -04:00
|
|
|
#include "../DataStructures/StaticGraph.h"
|
|
|
|
#include "../DataStructures/SearchEngine.h"
|
|
|
|
|
|
|
|
#include "../Util/StringUtil.h"
|
|
|
|
|
2012-04-14 14:07:30 -04:00
|
|
|
#include "../Server/DataStructures/QueryObjectsStorage.h"
|
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
class ViaRoutePlugin : public BasePlugin {
|
|
|
|
private:
|
|
|
|
NodeInformationHelpDesk * nodeHelpDesk;
|
2012-04-14 11:40:59 -04:00
|
|
|
std::vector<std::string> & names;
|
2012-04-25 04:51:16 -04:00
|
|
|
StaticGraph<QueryEdge::EdgeData> * graph;
|
2011-07-08 13:27:16 -04:00
|
|
|
HashTable<std::string, unsigned> descriptorTable;
|
|
|
|
std::string pluginDescriptorString;
|
2012-04-25 04:51:16 -04:00
|
|
|
SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > * searchEngine;
|
2011-07-08 13:27:16 -04:00
|
|
|
public:
|
|
|
|
|
2012-04-14 14:07:30 -04:00
|
|
|
ViaRoutePlugin(QueryObjectsStorage * objects, std::string psd = "viaroute") : names(objects->names), pluginDescriptorString(psd) {
|
2011-07-08 13:27:16 -04:00
|
|
|
nodeHelpDesk = objects->nodeHelpDesk;
|
|
|
|
graph = objects->graph;
|
|
|
|
|
2012-04-25 04:51:16 -04:00
|
|
|
searchEngine = new SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> >(graph, nodeHelpDesk, names);
|
2011-07-08 13:27:16 -04:00
|
|
|
|
|
|
|
descriptorTable.Set("", 0); //default descriptor
|
2011-11-15 04:57:29 -05:00
|
|
|
descriptorTable.Set("json", 0);
|
|
|
|
descriptorTable.Set("gpx", 1);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
2011-07-21 10:30:36 -04:00
|
|
|
virtual ~ViaRoutePlugin() {
|
2012-04-12 13:35:47 -04:00
|
|
|
delete searchEngine;
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
2011-12-30 06:20:36 -05:00
|
|
|
std::string GetDescriptor() const { return pluginDescriptorString; }
|
|
|
|
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
|
2011-12-28 08:14:09 -05:00
|
|
|
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
|
2011-07-08 13:27:16 -04:00
|
|
|
//check number of parameters
|
2012-03-05 13:08:10 -05:00
|
|
|
if( 2 > routeParameters.viaPoints.size() ) {
|
2011-07-08 13:27:16 -04:00
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 11:16:14 -04:00
|
|
|
RawRouteData rawRoute;
|
2012-02-17 02:34:52 -05:00
|
|
|
rawRoute.checkSum = nodeHelpDesk->GetCheckSum();
|
2012-03-07 04:43:25 -05:00
|
|
|
bool checksumOK = ((unsigned)atoi(routeParameters.options.Find("checksum").c_str()) == rawRoute.checkSum);
|
2012-03-07 02:49:10 -05:00
|
|
|
// if(!checksumOK) {
|
|
|
|
// INFO((unsigned)atoi(routeParameters.options.Find("checksum").c_str()) << "!=" << rawRoute.checkSum);
|
|
|
|
// INFO("mismatching checksum");
|
|
|
|
// }
|
2012-03-05 13:08:10 -05:00
|
|
|
std::vector<std::string> textCoord;
|
2011-12-20 12:45:48 -05:00
|
|
|
for(unsigned i = 0; i < routeParameters.viaPoints.size(); ++i) {
|
2012-03-21 10:41:38 -04:00
|
|
|
textCoord.resize(0);
|
2012-03-21 09:52:38 -04:00
|
|
|
stringSplit (routeParameters.viaPoints[i], ',', textCoord);
|
2011-07-08 13:27:16 -04:00
|
|
|
if(textCoord.size() != 2) {
|
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int vialat = static_cast<int>(100000.*atof(textCoord[0].c_str()));
|
|
|
|
int vialon = static_cast<int>(100000.*atof(textCoord[1].c_str()));
|
|
|
|
_Coordinate viaCoord(vialat, vialon);
|
|
|
|
if(false == checkCoord(viaCoord)) {
|
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-11 11:16:14 -04:00
|
|
|
rawRoute.rawViaNodeCoordinates.push_back(viaCoord);
|
|
|
|
}
|
2012-03-05 13:08:10 -05:00
|
|
|
std::vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size());
|
2011-12-20 12:45:48 -05:00
|
|
|
for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); ++i) {
|
2012-03-05 13:08:10 -05:00
|
|
|
if(checksumOK && i < routeParameters.hints.size() && "" != routeParameters.hints[i]) {
|
2012-03-07 02:49:10 -05:00
|
|
|
// INFO("Decoding hint: " << routeParameters.hints[i] << " for location index " << i);
|
2012-03-05 13:08:10 -05:00
|
|
|
DecodeObjectFromBase64(phantomNodeVector[i], routeParameters.hints[i]);
|
|
|
|
if(phantomNodeVector[i].isValid(nodeHelpDesk->getNumberOfNodes())) {
|
2012-03-07 02:49:10 -05:00
|
|
|
// INFO("Decoded hint " << i << " successfully");
|
2012-03-05 13:08:10 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 02:49:10 -05:00
|
|
|
// INFO("Brute force lookup of coordinate " << i);
|
2011-11-16 11:29:00 -05:00
|
|
|
searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i]);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
2011-12-20 12:45:48 -05:00
|
|
|
unsigned distance = 0;
|
2011-11-16 11:29:00 -05:00
|
|
|
//single route or via point routing
|
2012-02-23 10:29:55 -05:00
|
|
|
if(2 == rawRoute.rawViaNodeCoordinates.size()) {
|
2011-08-07 06:56:37 -04:00
|
|
|
PhantomNodes segmentPhantomNodes;
|
2011-11-16 11:29:00 -05:00
|
|
|
segmentPhantomNodes.startPhantom = phantomNodeVector[0];
|
|
|
|
segmentPhantomNodes.targetPhantom = phantomNodeVector[1];
|
2011-12-20 12:45:48 -05:00
|
|
|
distance = searchEngine->ComputeRoute(segmentPhantomNodes, rawRoute.computedRouted);
|
2011-12-30 16:11:48 -05:00
|
|
|
rawRoute.segmentEndCoordinates.push_back(segmentPhantomNodes);
|
2011-11-16 11:29:00 -05:00
|
|
|
} else {
|
|
|
|
//Getting the shortest via path is a dynamic programming problem and is solved as such.
|
2011-12-20 12:45:48 -05:00
|
|
|
for(unsigned i = 0; i < phantomNodeVector.size()-1; ++i) {
|
|
|
|
PhantomNodes segmentPhantomNodes;
|
|
|
|
segmentPhantomNodes.startPhantom = phantomNodeVector[i];
|
|
|
|
segmentPhantomNodes.targetPhantom = phantomNodeVector[i+1];
|
2011-12-30 16:11:48 -05:00
|
|
|
rawRoute.segmentEndCoordinates.push_back(segmentPhantomNodes);
|
2011-11-16 11:29:00 -05:00
|
|
|
}
|
2011-12-30 16:11:48 -05:00
|
|
|
distance = searchEngine->ComputeViaRoute(rawRoute.segmentEndCoordinates, rawRoute.computedRouted);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
2011-12-20 12:45:48 -05:00
|
|
|
if(INT_MAX == distance ) {
|
|
|
|
DEBUG( "Error occurred, single path not found" );
|
|
|
|
}
|
2011-07-08 13:27:16 -04:00
|
|
|
reply.status = http::Reply::ok;
|
|
|
|
|
2012-04-12 13:35:47 -04:00
|
|
|
//TODO: Move to member as smart pointer
|
2012-04-25 04:51:16 -04:00
|
|
|
BaseDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > > * desc;
|
2011-07-08 13:27:16 -04:00
|
|
|
std::string JSONParameter = routeParameters.options.Find("jsonp");
|
|
|
|
if("" != JSONParameter) {
|
|
|
|
reply.content += JSONParameter;
|
2012-02-17 02:34:52 -05:00
|
|
|
reply.content += "(";
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_DescriptorConfig descriptorConfig;
|
|
|
|
unsigned descriptorType = descriptorTable[routeParameters.options.Find("output")];
|
|
|
|
unsigned short zoom = 18;
|
|
|
|
if(routeParameters.options.Find("z") != ""){
|
|
|
|
zoom = atoi(routeParameters.options.Find("z").c_str());
|
|
|
|
if(18 < zoom)
|
|
|
|
zoom = 18;
|
|
|
|
}
|
|
|
|
descriptorConfig.z = zoom;
|
|
|
|
if(routeParameters.options.Find("instructions") == "false") {
|
|
|
|
descriptorConfig.instructions = false;
|
|
|
|
}
|
|
|
|
if(routeParameters.options.Find("geometry") == "false" ) {
|
|
|
|
descriptorConfig.geometry = false;
|
|
|
|
}
|
2012-05-14 05:38:29 -04:00
|
|
|
if("cmp" == routeParameters.options.Find("no") || "cmp6" == routeParameters.options.Find("no") ) {
|
|
|
|
descriptorConfig.encodeGeometry = false;
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
switch(descriptorType){
|
|
|
|
case 0:
|
2012-04-25 04:51:16 -04:00
|
|
|
desc = new JSONDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > >();
|
2011-07-08 13:27:16 -04:00
|
|
|
|
2011-07-11 11:16:14 -04:00
|
|
|
break;
|
2011-11-14 13:36:31 -05:00
|
|
|
case 1:
|
2012-04-25 04:51:16 -04:00
|
|
|
desc = new GPXDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > >();
|
2011-07-11 11:16:14 -04:00
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
break;
|
|
|
|
default:
|
2012-04-25 04:51:16 -04:00
|
|
|
desc = new JSONDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > >();
|
2011-07-08 13:27:16 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-07-12 10:03:31 -04:00
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
PhantomNodes phantomNodes;
|
2011-07-12 10:03:31 -04:00
|
|
|
phantomNodes.startPhantom = rawRoute.segmentEndCoordinates[0].startPhantom;
|
2011-12-30 16:11:48 -05:00
|
|
|
// INFO("Start location: " << phantomNodes.startPhantom.location)
|
2011-07-12 10:03:31 -04:00
|
|
|
phantomNodes.targetPhantom = rawRoute.segmentEndCoordinates[rawRoute.segmentEndCoordinates.size()-1].targetPhantom;
|
2011-12-30 16:11:48 -05:00
|
|
|
// INFO("TargetLocation: " << phantomNodes.targetPhantom.location);
|
|
|
|
// INFO("Number of segments: " << rawRoute.segmentEndCoordinates.size());
|
2011-07-08 13:27:16 -04:00
|
|
|
desc->SetConfig(descriptorConfig);
|
2011-08-07 06:56:37 -04:00
|
|
|
|
|
|
|
desc->Run(reply, rawRoute, phantomNodes, *searchEngine, distance);
|
2011-07-08 13:27:16 -04:00
|
|
|
if("" != JSONParameter) {
|
|
|
|
reply.content += ")\n";
|
|
|
|
}
|
|
|
|
reply.headers.resize(3);
|
|
|
|
reply.headers[0].name = "Content-Length";
|
|
|
|
std::string tmp;
|
|
|
|
intToString(reply.content.size(), tmp);
|
|
|
|
reply.headers[0].value = tmp;
|
|
|
|
switch(descriptorType){
|
|
|
|
case 0:
|
|
|
|
if("" != JSONParameter){
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "text/javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.js\"";
|
|
|
|
} else {
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/x-javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.json\"";
|
|
|
|
}
|
|
|
|
|
2011-07-11 11:16:14 -04:00
|
|
|
break;
|
2011-11-16 11:29:00 -05:00
|
|
|
case 1:
|
2011-07-11 11:16:14 -04:00
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/gpx+xml; charset=UTF-8";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.gpx\"";
|
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
break;
|
|
|
|
default:
|
2011-11-16 11:29:00 -05:00
|
|
|
if("" != JSONParameter){
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "text/javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.js\"";
|
|
|
|
} else {
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/x-javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.json\"";
|
|
|
|
}
|
2011-07-08 13:27:16 -04:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:35:47 -04:00
|
|
|
delete desc;
|
2011-07-08 13:27:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
inline bool checkCoord(const _Coordinate & c) {
|
|
|
|
if(c.lat > 90*100000 || c.lat < -90*100000 || c.lon > 180*100000 || c.lon <-180*100000) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* VIAROUTEPLUGIN_H_ */
|