2011-07-08 13:27:16 -04:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2011-07-08 13:27:16 -04:00
|
|
|
|
|
|
|
#ifndef VIAROUTEPLUGIN_H_
|
|
|
|
#define VIAROUTEPLUGIN_H_
|
|
|
|
|
|
|
|
#include "BasePlugin.h"
|
2012-03-05 13:08:10 -05:00
|
|
|
|
|
|
|
#include "../Algorithms/ObjectToBase64.h"
|
2012-04-25 04:51:16 -04:00
|
|
|
#include "../DataStructures/QueryEdge.h"
|
2011-07-08 13:27:16 -04:00
|
|
|
#include "../DataStructures/SearchEngine.h"
|
2013-06-26 19:48:02 -04:00
|
|
|
#include "../Descriptors/BaseDescriptor.h"
|
|
|
|
#include "../Descriptors/GPXDescriptor.h"
|
|
|
|
#include "../Descriptors/JSONDescriptor.h"
|
2013-08-08 08:17:01 -04:00
|
|
|
#include "../Util/SimpleLogger.h"
|
2011-07-08 13:27:16 -04:00
|
|
|
#include "../Util/StringUtil.h"
|
|
|
|
|
2013-09-17 08:45:37 -04:00
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
2013-06-26 19:48:02 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-04-14 14:07:30 -04:00
|
|
|
|
2013-09-18 12:32:50 -04:00
|
|
|
//TODO: Rework data access to go through facade
|
|
|
|
template<class DataFacadeT>
|
2011-07-08 13:27:16 -04:00
|
|
|
class ViaRoutePlugin : public BasePlugin {
|
|
|
|
private:
|
2013-09-19 12:53:10 -04:00
|
|
|
boost::unordered_map<std::string, unsigned> descriptorTable;
|
|
|
|
SearchEngine<DataFacadeT> * search_engine_ptr;
|
2011-07-08 13:27:16 -04:00
|
|
|
public:
|
|
|
|
|
2013-09-19 12:53:10 -04:00
|
|
|
ViaRoutePlugin(DataFacadeT * facade)
|
2013-08-13 12:09:20 -04:00
|
|
|
:
|
2013-09-19 12:53:10 -04:00
|
|
|
descriptor_string("viaroute"),
|
|
|
|
facade(facade)
|
2013-08-13 12:09:20 -04:00
|
|
|
{
|
2013-09-19 12:53:10 -04:00
|
|
|
//TODO: set up an engine for each thread!!
|
|
|
|
search_engine_ptr = new SearchEngine<DataFacadeT>(facade);
|
2011-07-08 13:27:16 -04:00
|
|
|
|
2013-09-17 08:45:37 -04:00
|
|
|
descriptorTable.emplace("json", 0);
|
|
|
|
descriptorTable.emplace("gpx" , 1);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
2011-07-21 10:30:36 -04:00
|
|
|
virtual ~ViaRoutePlugin() {
|
2013-09-19 12:53:10 -04:00
|
|
|
delete search_engine_ptr;
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
2013-08-13 12:09:20 -04:00
|
|
|
const std::string & GetDescriptor() const { return descriptor_string; }
|
|
|
|
|
2013-09-19 12:53:10 -04:00
|
|
|
void HandleRequest(
|
|
|
|
const RouteParameters & routeParameters,
|
|
|
|
http::Reply& reply
|
|
|
|
) {
|
2011-07-08 13:27:16 -04:00
|
|
|
//check number of parameters
|
2012-09-12 09:01:37 -04:00
|
|
|
if( 2 > routeParameters.coordinates.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;
|
2013-09-19 12:53:10 -04:00
|
|
|
rawRoute.checkSum = facade->GetCheckSum();
|
2012-09-12 09:01:37 -04:00
|
|
|
bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum);
|
2012-03-05 13:08:10 -05:00
|
|
|
std::vector<std::string> textCoord;
|
2012-09-12 09:01:37 -04:00
|
|
|
for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) {
|
2013-09-19 12:53:10 -04:00
|
|
|
if( !checkCoord(routeParameters.coordinates[i]) ) {
|
2011-07-08 13:27:16 -04:00
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-12 09:01:37 -04:00
|
|
|
rawRoute.rawViaNodeCoordinates.push_back(routeParameters.coordinates[i]);
|
2011-07-11 11:16:14 -04:00
|
|
|
}
|
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]) {
|
2013-08-08 08:17:01 -04:00
|
|
|
// SimpleLogger().Write() <<"Decoding hint: " << routeParameters.hints[i] << " for location index " << i;
|
2013-07-08 08:59:51 -04:00
|
|
|
DecodeObjectFromBase64(routeParameters.hints[i], phantomNodeVector[i]);
|
2013-09-19 12:53:10 -04:00
|
|
|
if(phantomNodeVector[i].isValid(facade->GetNumberOfNodes())) {
|
2013-08-08 08:17:01 -04:00
|
|
|
// SimpleLogger().Write() << "Decoded hint " << i << " successfully";
|
2012-03-05 13:08:10 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 08:17:01 -04:00
|
|
|
// SimpleLogger().Write() << "Brute force lookup of coordinate " << i;
|
2013-09-19 12:53:10 -04:00
|
|
|
facade->FindPhantomNodeForCoordinate(
|
|
|
|
rawRoute.rawViaNodeCoordinates[i],
|
|
|
|
phantomNodeVector[i],
|
|
|
|
routeParameters.zoomLevel
|
|
|
|
);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
2012-06-19 11:26:34 -04:00
|
|
|
|
2012-06-15 12:47:27 -04:00
|
|
|
for(unsigned i = 0; i < phantomNodeVector.size()-1; ++i) {
|
2011-08-07 06:56:37 -04:00
|
|
|
PhantomNodes segmentPhantomNodes;
|
2012-06-15 12:47:27 -04:00
|
|
|
segmentPhantomNodes.startPhantom = phantomNodeVector[i];
|
|
|
|
segmentPhantomNodes.targetPhantom = phantomNodeVector[i+1];
|
2011-12-30 16:11:48 -05:00
|
|
|
rawRoute.segmentEndCoordinates.push_back(segmentPhantomNodes);
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
2013-09-19 12:53:10 -04:00
|
|
|
if(
|
|
|
|
( routeParameters.alternateRoute ) &&
|
|
|
|
(1 == rawRoute.segmentEndCoordinates.size())
|
|
|
|
) {
|
2013-09-20 12:30:47 -04:00
|
|
|
search_engine_ptr->alternative_path(
|
2013-09-19 12:53:10 -04:00
|
|
|
rawRoute.segmentEndCoordinates[0],
|
|
|
|
rawRoute
|
|
|
|
);
|
2012-06-19 11:26:34 -04:00
|
|
|
} else {
|
2013-09-20 05:09:07 -04:00
|
|
|
search_engine_ptr->shortest_path(
|
2013-09-19 12:53:10 -04:00
|
|
|
rawRoute.segmentEndCoordinates,
|
|
|
|
rawRoute
|
|
|
|
);
|
2012-06-15 12:47:27 -04:00
|
|
|
}
|
2012-06-19 11:26:34 -04:00
|
|
|
|
|
|
|
if(INT_MAX == rawRoute.lengthOfShortestPath ) {
|
2013-09-19 12:53:10 -04:00
|
|
|
SimpleLogger().Write(logDEBUG) <<
|
|
|
|
"Error occurred, single path not found";
|
2011-12-20 12:45:48 -05:00
|
|
|
}
|
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
|
2013-09-19 12:53:10 -04:00
|
|
|
BaseDescriptor<DataFacadeT> * desc;
|
2012-09-12 09:01:37 -04:00
|
|
|
if("" != routeParameters.jsonpParameter) {
|
|
|
|
reply.content += routeParameters.jsonpParameter;
|
2012-02-17 02:34:52 -05:00
|
|
|
reply.content += "(";
|
2011-07-08 13:27:16 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 05:09:07 -04:00
|
|
|
DescriptorConfig descriptorConfig;
|
2013-09-17 08:45:37 -04:00
|
|
|
|
|
|
|
unsigned descriptorType = 0;
|
|
|
|
if(descriptorTable.find(routeParameters.outputFormat) != descriptorTable.end() ) {
|
|
|
|
descriptorType = descriptorTable.find(routeParameters.outputFormat)->second;
|
|
|
|
}
|
2013-09-20 05:09:07 -04:00
|
|
|
descriptorConfig.zoom_level = routeParameters.zoomLevel;
|
2012-09-12 09:01:37 -04:00
|
|
|
descriptorConfig.instructions = routeParameters.printInstructions;
|
|
|
|
descriptorConfig.geometry = routeParameters.geometry;
|
2013-09-20 05:09:07 -04:00
|
|
|
descriptorConfig.encode_geometry = routeParameters.compression;
|
2012-09-12 09:01:37 -04:00
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
switch(descriptorType){
|
|
|
|
case 0:
|
2013-09-19 12:53:10 -04:00
|
|
|
desc = new JSONDescriptor<DataFacadeT>();
|
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:
|
2013-09-19 12:53:10 -04:00
|
|
|
desc = new GPXDescriptor<DataFacadeT>();
|
2011-07-11 11:16:14 -04:00
|
|
|
|
2011-07-08 13:27:16 -04:00
|
|
|
break;
|
|
|
|
default:
|
2013-09-19 12:53:10 -04:00
|
|
|
desc = new JSONDescriptor<DataFacadeT>();
|
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;
|
2013-08-08 08:17:01 -04:00
|
|
|
// SimpleLogger().Write() << "Start location: " << phantomNodes.startPhantom.location;
|
2011-07-12 10:03:31 -04:00
|
|
|
phantomNodes.targetPhantom = rawRoute.segmentEndCoordinates[rawRoute.segmentEndCoordinates.size()-1].targetPhantom;
|
2013-08-08 08:17:01 -04:00
|
|
|
// SimpleLogger().Write() << "TargetLocation: " << phantomNodes.targetPhantom.location;
|
|
|
|
// SimpleLogger().Write() << "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
|
|
|
|
2013-09-19 12:53:10 -04:00
|
|
|
desc->Run(reply, rawRoute, phantomNodes, facade);
|
2012-09-12 09:01:37 -04:00
|
|
|
if("" != routeParameters.jsonpParameter) {
|
2011-07-08 13:27:16 -04:00
|
|
|
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:
|
2013-09-19 12:53:10 -04:00
|
|
|
if( !routeParameters.jsonpParameter.empty() ){
|
2011-07-08 13:27:16 -04:00
|
|
|
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:
|
2013-09-19 12:53:10 -04:00
|
|
|
if( !routeParameters.jsonpParameter.empty() ){
|
2011-11-16 11:29:00 -05:00
|
|
|
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:
|
2013-08-13 12:09:20 -04:00
|
|
|
std::string descriptor_string;
|
2013-09-19 12:53:10 -04:00
|
|
|
DataFacadeT * facade;
|
2011-07-08 13:27:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* VIAROUTEPLUGIN_H_ */
|