2011-01-09 16:42:27 -05: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 ROUTEPLUGIN_H_
|
|
|
|
#define ROUTEPLUGIN_H_
|
|
|
|
|
2011-05-10 06:24:13 -04:00
|
|
|
#include <cstdlib>
|
2011-01-09 16:42:27 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2011-05-26 05:16:04 -04:00
|
|
|
#include "ObjectForPluginStruct.h"
|
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
#include "BaseDescriptor.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
#include "BasePlugin.h"
|
2011-04-18 04:12:44 -04:00
|
|
|
#include "RouteParameters.h"
|
|
|
|
#include "KMLDescriptor.h"
|
|
|
|
#include "JSONDescriptor.h"
|
2011-06-30 10:24:43 -04:00
|
|
|
#include "GPXDescriptor.h"
|
2011-04-18 04:12:44 -04:00
|
|
|
|
|
|
|
#include "../DataStructures/HashTable.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
#include "../DataStructures/StaticGraph.h"
|
|
|
|
#include "../DataStructures/SearchEngine.h"
|
|
|
|
|
2011-07-07 04:05:58 -04:00
|
|
|
#include "../Util/StringUtil.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
|
|
|
|
class RoutePlugin : public BasePlugin {
|
|
|
|
public:
|
2011-03-28 12:34:06 -04:00
|
|
|
|
2011-05-26 05:16:04 -04:00
|
|
|
RoutePlugin(ObjectsForQueryStruct * objects, std::string psd = "route") : pluginDescriptorString(psd) {
|
|
|
|
nodeHelpDesk = objects->nodeHelpDesk;
|
|
|
|
graph = objects->graph;
|
|
|
|
names = objects->names;
|
2011-03-28 12:34:06 -04:00
|
|
|
|
|
|
|
sEngine = new SearchEngine<EdgeData, StaticGraph<EdgeData> >(graph, nodeHelpDesk, names);
|
2011-04-18 04:12:44 -04:00
|
|
|
descriptorTable.Set("", 0); //default descriptor
|
|
|
|
descriptorTable.Set("kml", 0);
|
|
|
|
descriptorTable.Set("json", 1);
|
2011-06-30 10:24:43 -04:00
|
|
|
descriptorTable.Set("gpx", 2);
|
2011-03-28 12:34:06 -04:00
|
|
|
}
|
2011-05-26 05:16:04 -04:00
|
|
|
|
2011-07-21 10:30:36 -04:00
|
|
|
virtual ~RoutePlugin() {
|
2011-07-12 13:12:30 -04:00
|
|
|
DELETE(sEngine);
|
2011-03-28 12:34:06 -04:00
|
|
|
}
|
2011-04-18 04:12:44 -04:00
|
|
|
|
2011-05-18 07:52:53 -04:00
|
|
|
std::string GetDescriptor() { return pluginDescriptorString; }
|
2011-03-28 12:34:06 -04:00
|
|
|
std::string GetVersionString() { return std::string("0.3 (DL)"); }
|
2011-04-18 04:12:44 -04:00
|
|
|
void HandleRequest(RouteParameters routeParameters, http::Reply& reply) {
|
2011-03-28 12:34:06 -04:00
|
|
|
//check number of parameters
|
2011-04-18 04:12:44 -04:00
|
|
|
if(routeParameters.parameters.size() != 4) {
|
2011-03-28 12:34:06 -04:00
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
int lat1 = static_cast<int>(100000.*atof(routeParameters.parameters[0].c_str()));
|
|
|
|
int lon1 = static_cast<int>(100000.*atof(routeParameters.parameters[1].c_str()));
|
|
|
|
int lat2 = static_cast<int>(100000.*atof(routeParameters.parameters[2].c_str()));
|
|
|
|
int lon2 = static_cast<int>(100000.*atof(routeParameters.parameters[3].c_str()));
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2011-07-07 12:51:23 -04:00
|
|
|
_DescriptorConfig descriptorConfig;
|
2011-05-11 07:35:15 -04:00
|
|
|
|
|
|
|
if("false" == routeParameters.options["geometry"]) {
|
2011-07-07 12:51:23 -04:00
|
|
|
descriptorConfig.geometry = false;
|
2011-05-11 07:35:15 -04:00
|
|
|
}
|
|
|
|
|
2011-02-15 12:00:39 -05:00
|
|
|
if(lat1>90*100000 || lat1 <-90*100000 || lon1>180*100000 || lon1 <-180*100000) {
|
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lat2>90*100000 || lat2 <-90*100000 || lon2>180*100000 || lon2 <-180*100000) {
|
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-28 12:34:06 -04:00
|
|
|
_Coordinate startCoord(lat1, lon1);
|
|
|
|
_Coordinate targetCoord(lat2, lon2);
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2011-07-12 10:03:31 -04:00
|
|
|
vector< _PathData > path;
|
|
|
|
RawRouteData rawRoute;
|
|
|
|
PhantomNodes phantomNodes;
|
2011-03-28 12:34:06 -04:00
|
|
|
sEngine->FindRoutingStarts(startCoord, targetCoord, phantomNodes);
|
2011-07-07 12:51:23 -04:00
|
|
|
unsigned int distance = sEngine->ComputeRoute(phantomNodes, path);
|
2011-07-12 10:03:31 -04:00
|
|
|
rawRoute.routeSegments.push_back(path);
|
2011-03-28 12:34:06 -04:00
|
|
|
reply.status = http::Reply::ok;
|
2011-04-18 04:12:44 -04:00
|
|
|
BaseDescriptor<SearchEngine<EdgeData, StaticGraph<EdgeData> > > * desc;
|
2011-04-18 05:18:29 -04:00
|
|
|
std::string JSONParameter = routeParameters.options.Find("jsonp");
|
|
|
|
if("" != JSONParameter) {
|
|
|
|
reply.content += JSONParameter;
|
|
|
|
reply.content += "(\n";
|
|
|
|
}
|
|
|
|
unsigned descriptorType = descriptorTable[routeParameters.options.Find("output")];
|
2011-05-11 07:35:15 -04:00
|
|
|
unsigned short zoom = 18;
|
2011-05-10 06:24:13 -04:00
|
|
|
if(routeParameters.options.Find("z") != ""){
|
|
|
|
zoom = atoi(routeParameters.options.Find("z").c_str());
|
|
|
|
if(18 < zoom)
|
|
|
|
zoom = 18;
|
|
|
|
}
|
2011-05-13 05:16:58 -04:00
|
|
|
descriptorConfig.z = zoom;
|
|
|
|
if(routeParameters.options.Find("instructions") == "false") {
|
|
|
|
descriptorConfig.instructions = false;
|
|
|
|
}
|
|
|
|
if(routeParameters.options.Find("geometry") == "false" ) {
|
|
|
|
descriptorConfig.geometry = false;
|
|
|
|
}
|
|
|
|
|
2011-06-27 18:08:53 -04:00
|
|
|
if("cmp" == routeParameters.options.Find("geomformat") || "cmp6" == routeParameters.options.Find("geomformat") ) {
|
|
|
|
descriptorConfig.encodeGeometry = true;
|
|
|
|
}
|
2011-05-13 05:16:58 -04:00
|
|
|
|
2011-04-18 05:18:29 -04:00
|
|
|
switch(descriptorType){
|
2011-04-18 04:12:44 -04:00
|
|
|
case 0:
|
2011-05-13 05:16:58 -04:00
|
|
|
desc = new KMLDescriptor<SearchEngine<EdgeData, StaticGraph<EdgeData> > >();
|
2011-05-11 07:35:15 -04:00
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-05-13 05:16:58 -04:00
|
|
|
desc = new JSONDescriptor<SearchEngine<EdgeData, StaticGraph<EdgeData> > >();
|
2011-05-11 07:35:15 -04:00
|
|
|
|
2011-06-30 10:24:43 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
desc = new GPXDescriptor<SearchEngine<EdgeData, StaticGraph<EdgeData> > >();
|
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
break;
|
|
|
|
default:
|
2011-05-13 05:16:58 -04:00
|
|
|
desc = new KMLDescriptor<SearchEngine<EdgeData, StaticGraph<EdgeData> > >();
|
2011-05-11 07:35:15 -04:00
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
break;
|
|
|
|
}
|
2011-05-13 05:16:58 -04:00
|
|
|
desc->SetConfig(descriptorConfig);
|
2011-07-12 10:03:31 -04:00
|
|
|
desc->Run(reply, rawRoute, phantomNodes, *sEngine, distance);
|
2011-04-18 05:18:29 -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:
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/vnd.google-earth.kml+xml; charset=UTF-8";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.kml\"";
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
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";
|
2011-05-10 06:24:13 -04:00
|
|
|
reply.headers[1].value = "application/x-javascript";
|
2011-04-18 05:18:29 -04:00
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.json\"";
|
|
|
|
}
|
|
|
|
|
2011-06-30 10:24:43 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
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-04-18 05:18:29 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/vnd.google-earth.kml+xml; charset=UTF-8";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"route.kml\"";
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2011-07-12 10:03:31 -04:00
|
|
|
DELETE( desc );
|
2011-03-28 12:34:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
private:
|
2011-03-28 12:34:06 -04:00
|
|
|
NodeInformationHelpDesk * nodeHelpDesk;
|
2011-07-12 10:03:31 -04:00
|
|
|
SearchEngine<EdgeData, StaticGraph<EdgeData> > *sEngine;
|
2011-03-28 12:34:06 -04:00
|
|
|
std::vector<std::string> * names;
|
|
|
|
StaticGraph<EdgeData> * graph;
|
2011-04-18 04:12:44 -04:00
|
|
|
HashTable<std::string, unsigned> descriptorTable;
|
2011-05-18 07:52:53 -04:00
|
|
|
std::string pluginDescriptorString;
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* ROUTEPLUGIN_H_ */
|