2011-04-15 12:31:04 -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.
|
|
|
|
*/
|
|
|
|
|
2011-07-07 12:51:23 -04:00
|
|
|
#ifndef JSON_DESCRIPTOR_H_
|
|
|
|
#define JSON_DESCRIPTOR_H_
|
|
|
|
|
2011-11-14 13:36:31 -05:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2011-04-18 04:12:44 -04:00
|
|
|
#include "BaseDescriptor.h"
|
2011-11-17 12:04:49 -05:00
|
|
|
#include "../DataStructures/DescriptionFactory.h"
|
|
|
|
#include "../DataStructures/SegmentInformation.h"
|
|
|
|
#include "../DataStructures/TurnInstructions.h"
|
2011-11-16 11:29:00 -05:00
|
|
|
#include "../Util/StringUtil.h"
|
2011-04-15 12:31:04 -04:00
|
|
|
|
2011-05-13 05:16:58 -04:00
|
|
|
template<class SearchEngineT>
|
2011-07-07 12:51:23 -04:00
|
|
|
class JSONDescriptor : public BaseDescriptor<SearchEngineT>{
|
2011-05-10 06:24:13 -04:00
|
|
|
private:
|
2011-07-07 12:51:23 -04:00
|
|
|
_DescriptorConfig config;
|
2011-11-16 11:29:00 -05:00
|
|
|
_RouteSummary summary;
|
2011-11-17 12:04:49 -05:00
|
|
|
DescriptionFactory descriptionFactory;
|
2011-07-07 12:51:23 -04:00
|
|
|
std::string tmp;
|
2011-11-16 11:29:00 -05:00
|
|
|
_Coordinate current;
|
2011-07-07 12:51:23 -04:00
|
|
|
|
2011-04-15 12:31:04 -04:00
|
|
|
public:
|
2011-05-13 05:16:58 -04:00
|
|
|
JSONDescriptor() {}
|
2011-07-07 12:51:23 -04:00
|
|
|
void SetConfig(const _DescriptorConfig & c) { config = c; }
|
2011-05-10 06:24:13 -04:00
|
|
|
|
2011-11-16 11:29:00 -05:00
|
|
|
void Run(http::Reply & reply, RawRouteData &rawRoute, PhantomNodes &phantomNodes, SearchEngineT &sEngine, unsigned durationOfTrip) {
|
2011-07-07 12:51:23 -04:00
|
|
|
WriteHeaderToOutput(reply.content);
|
|
|
|
//We do not need to do much, if there is no route ;-)
|
2011-07-12 10:03:31 -04:00
|
|
|
|
2011-11-16 11:29:00 -05:00
|
|
|
if(durationOfTrip != INT_MAX && rawRoute.routeSegments.size() > 0) {
|
|
|
|
summary.startName = sEngine.GetEscapedNameForNameID(phantomNodes.startPhantom.nodeBasedEdgeNameID);
|
2011-11-17 12:04:49 -05:00
|
|
|
descriptionFactory.SetStartSegment(phantomNodes.startPhantom);
|
2011-11-16 11:29:00 -05:00
|
|
|
summary.destName = sEngine.GetEscapedNameForNameID(phantomNodes.targetPhantom.nodeBasedEdgeNameID);
|
2011-07-07 12:51:23 -04:00
|
|
|
reply.content += "0,"
|
|
|
|
"\"status_message\": \"Found route between points\",";
|
2011-07-12 10:03:31 -04:00
|
|
|
for(unsigned segmentIdx = 0; segmentIdx < rawRoute.routeSegments.size(); segmentIdx++) {
|
|
|
|
const std::vector< _PathData > & path = rawRoute.routeSegments[segmentIdx];
|
2011-11-14 13:36:31 -05:00
|
|
|
BOOST_FOREACH(_PathData pathData, path) {
|
|
|
|
sEngine.GetCoordinatesForNodeID(pathData.node, current);
|
2011-11-17 12:04:49 -05:00
|
|
|
descriptionFactory.AppendSegment(current, pathData );
|
2011-04-15 12:31:04 -04:00
|
|
|
}
|
2011-11-17 12:04:49 -05:00
|
|
|
//TODO: Add via points
|
2011-04-15 12:31:04 -04:00
|
|
|
}
|
2011-11-17 12:04:49 -05:00
|
|
|
descriptionFactory.SetEndSegment(phantomNodes.targetPhantom);
|
2011-04-15 12:31:04 -04:00
|
|
|
} else {
|
2011-07-07 12:51:23 -04:00
|
|
|
//no route found
|
|
|
|
reply.content += "207,"
|
|
|
|
"\"status_message\": \"Cannot find route between points\",";
|
2011-04-15 12:31:04 -04:00
|
|
|
}
|
2011-07-07 12:51:23 -04:00
|
|
|
|
2011-11-17 12:04:49 -05:00
|
|
|
summary.BuildDurationAndLengthStrings(descriptionFactory.Run(), durationOfTrip);
|
|
|
|
|
2011-07-07 12:51:23 -04:00
|
|
|
reply.content += "\"route_summary\": {"
|
|
|
|
"\"total_distance\":";
|
|
|
|
reply.content += summary.lengthString;
|
|
|
|
reply.content += ","
|
|
|
|
"\"total_time\":";
|
|
|
|
reply.content += summary.durationString;
|
|
|
|
reply.content += ","
|
|
|
|
"\"start_point\":\"";
|
|
|
|
reply.content += summary.startName;
|
|
|
|
reply.content += "\","
|
|
|
|
"\"end_point\":\"";
|
|
|
|
reply.content += summary.destName;
|
|
|
|
reply.content += "\"";
|
2011-05-10 06:24:13 -04:00
|
|
|
reply.content += "},";
|
2011-06-28 04:27:38 -04:00
|
|
|
reply.content += "\"route_geometry\": ";
|
2011-05-13 05:16:58 -04:00
|
|
|
if(config.geometry) {
|
2011-07-07 12:51:23 -04:00
|
|
|
if(config.encodeGeometry)
|
2011-11-17 12:04:49 -05:00
|
|
|
descriptionFactory.AppendEncodedPolylineString(reply.content, config.encodeGeometry);
|
2011-07-06 08:42:44 -04:00
|
|
|
} else {
|
|
|
|
reply.content += "[]";
|
2011-05-11 07:35:15 -04:00
|
|
|
}
|
2011-07-07 12:51:23 -04:00
|
|
|
|
|
|
|
reply.content += ","
|
|
|
|
"\"route_instructions\": [";
|
2011-11-17 12:04:49 -05:00
|
|
|
if(config.instructions) {
|
|
|
|
unsigned prefixSumOfNecessarySegments = 0;
|
|
|
|
std::string tmpDist, tmpLength, tmp;
|
|
|
|
//Fetch data from Factory and generate a string from it.
|
|
|
|
BOOST_FOREACH(SegmentInformation segment, descriptionFactory.pathDescription) {
|
|
|
|
//["instruction","streetname",length,position,time,"length","earth_direction",azimuth]
|
|
|
|
if(0 != segment.turnInstruction) {
|
|
|
|
if(0 != prefixSumOfNecessarySegments)
|
|
|
|
reply.content += ",";
|
|
|
|
reply.content += "[\"";
|
|
|
|
reply.content += TurnInstructions.TurnStrings[segment.turnInstruction];
|
|
|
|
reply.content += "\",\"";
|
|
|
|
reply.content += sEngine.GetEscapedNameForNameID(segment.nameID);
|
|
|
|
reply.content += "\",";
|
|
|
|
intToString(segment.length, tmpDist);
|
|
|
|
reply.content += tmpDist;
|
|
|
|
reply.content += ",";
|
|
|
|
intToString(prefixSumOfNecessarySegments, tmpLength);
|
|
|
|
reply.content += tmpLength;
|
|
|
|
reply.content += ",";
|
|
|
|
intToString(segment.duration, tmp);
|
|
|
|
reply.content += ",\"";
|
|
|
|
reply.content += tmpLength;
|
|
|
|
//TODO: fix heading
|
|
|
|
reply.content += "\",\"NE\",22.5";
|
|
|
|
reply.content += "]";
|
|
|
|
}
|
|
|
|
if(segment.necessary)
|
|
|
|
++prefixSumOfNecessarySegments;
|
|
|
|
}
|
|
|
|
// descriptionFactory.AppendRouteInstructionString(reply.content);
|
|
|
|
|
|
|
|
}
|
2011-05-10 06:24:13 -04:00
|
|
|
reply.content += "],";
|
2011-07-07 12:51:23 -04:00
|
|
|
//list all viapoints so that the client may display it
|
|
|
|
reply.content += "\"via_points\":[";
|
2011-07-12 10:03:31 -04:00
|
|
|
for(unsigned segmentIdx = 1; (true == config.geometry) && (segmentIdx < rawRoute.segmentEndCoordinates.size()); segmentIdx++) {
|
2011-07-07 12:51:23 -04:00
|
|
|
if(segmentIdx > 1)
|
|
|
|
reply.content += ",";
|
|
|
|
reply.content += "[";
|
2011-07-12 10:03:31 -04:00
|
|
|
if(rawRoute.segmentEndCoordinates[segmentIdx].startPhantom.location.isSet())
|
|
|
|
convertInternalReversedCoordinateToString(rawRoute.segmentEndCoordinates[segmentIdx].startPhantom.location, tmp);
|
2011-07-11 11:16:14 -04:00
|
|
|
else
|
2011-07-12 10:03:31 -04:00
|
|
|
convertInternalReversedCoordinateToString(rawRoute.rawViaNodeCoordinates[segmentIdx], tmp);
|
2011-07-07 12:51:23 -04:00
|
|
|
reply.content += tmp;
|
|
|
|
reply.content += "]";
|
|
|
|
}
|
|
|
|
reply.content += "],"
|
|
|
|
"\"transactionId\": \"OSRM Routing Engine JSON Descriptor (v0.2)\"";
|
2011-04-15 12:31:04 -04:00
|
|
|
reply.content += "}";
|
2011-07-07 12:51:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteHeaderToOutput(std::string & output) {
|
|
|
|
output += "{"
|
|
|
|
"\"version\": 0.3,"
|
|
|
|
"\"status\":";
|
2011-04-15 12:31:04 -04:00
|
|
|
}
|
|
|
|
};
|
2011-07-07 12:51:23 -04:00
|
|
|
#endif /* JSON_DESCRIPTOR_H_ */
|