/* open source routing machine Copyright (C) Dennis Luxen, 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 HTTP_ROUTER_REQUEST_HANDLER_HPP #define HTTP_ROUTER_REQUEST_HANDLER_HPP #include #include #include #include #include #include #include "reply.h" #include "request.h" #include "../typedefs.h" namespace http { struct reply; struct Request; /// The common handler for all incoming requests. template class request_handler : private boost::noncopyable { public: explicit request_handler(SearchEngine * s) : sEngine(s){} /// Handle a request and produce a reply. void handle_request(const Request& req, reply& rep){ try { std::string request(req.uri); std::string command; std::size_t first_amp_pos = request.find_first_of("&"); command = request.substr(1,first_amp_pos-1); if(command == "locate") { std::size_t last_amp_pos = request.find_last_of("&"); int lat = static_cast(100000*atof(request.substr(first_amp_pos+1, request.length()-last_amp_pos-1).c_str())); int lon = static_cast(100000*atof(request.substr(last_amp_pos+1).c_str())); _Coordinate result; sEngine->findNearestNodeForLatLon(_Coordinate(lat, lon), result); rep.status = reply::ok; rep.content.append(""); rep.content.append(""); rep.content.append(""); std::stringstream out1; out1 << setprecision(10); out1 << "Nearest Place in map to " << lat/100000. << "," << lon/100000. << ""; rep.content.append(out1.str()); rep.content.append(""); std::stringstream out2; out2 << setprecision(10); out2 << "" << result.lon / 100000. << "," << result.lat / 100000. << ""; rep.content.append(out2.str()); rep.content.append(""); rep.content.append(""); rep.content.append(""); rep.headers.resize(3); rep.headers[0].name = "Content-Length"; rep.headers[0].value = boost::lexical_cast(rep.content.size()); rep.headers[1].name = "Content-Type"; rep.headers[1].value = "application/vnd.google-earth.kml+xml"; rep.headers[2].name = "Content-Disposition"; rep.headers[2].value = "attachment; filename=\"placemark.kml\""; return; } if(command == "route") { double timestamp = get_timestamp(); //http://localhost:5000/route&45.1427&12.14144&54.8733&8.59438 std::size_t second_amp_pos = request.find_first_of("&", first_amp_pos+1); std::size_t third_amp_pos = request.find_first_of("&", second_amp_pos+1); std::size_t fourth_amp_pos = request.find_last_of("&"); int lat1 = static_cast(100000*atof(request.substr(first_amp_pos+1, request.length()-second_amp_pos-1).c_str())); int lon1 = static_cast(100000*atof(request.substr(second_amp_pos+1, request.length()-third_amp_pos-1).c_str())); int lat2 = static_cast(100000*atof(request.substr(third_amp_pos+1, request.length()-fourth_amp_pos-1).c_str())); int lon2 = static_cast(100000*atof(request.substr(fourth_amp_pos+1).c_str())); _Coordinate startCoord(lat1, lon1); _Coordinate targetCoord(lat2, lon2); vector * path = new vector(); PhantomNodes * phantomNodes = new PhantomNodes(); sEngine->FindRoutingStarts(startCoord, targetCoord, phantomNodes); unsigned int distance = sEngine->ComputeRoute(phantomNodes, path, startCoord, targetCoord); rep.status = reply::ok; string tmp; rep.content += (""); rep.content += (""); rep.content += (""); if(distance != std::numeric_limits::max()) computeDescription(tmp, path, phantomNodes); // rep.content += tmp; rep.content += (""); rep.content += ("OSM Routing Engine (c) Dennis Luxen and others "); rep.content += "Route from "; convertLatLon(lat1, tmp); rep.content += tmp; rep.content += ","; convertLatLon(lon1, tmp); rep.content += tmp; rep.content += " to "; convertLatLon(lat2, tmp); rep.content += tmp; rep.content += ","; convertLatLon(lon2, tmp); rep.content += tmp; rep.content += " "; rep.content += (""); rep.content += ("1"); rep.content += ("1"); rep.content += ("absolute"); rep.content += (""); if(distance != std::numeric_limits::max()) { //A route has been found convertLatLon(phantomNodes->startCoord.lon, tmp); rep.content += tmp; rep.content += (","); doubleToString(phantomNodes->startCoord.lat/100000., tmp); rep.content += tmp; rep.content += (" "); _Coordinate result; for(vector::iterator it = path->begin(); it != path->end(); it++) { sEngine->getNodeInfo(*it, result); convertLatLon(result.lon, tmp); rep.content += tmp; rep.content += (","); convertLatLon(result.lat, tmp); rep.content += tmp; rep.content += (" "); } convertLatLon(phantomNodes->targetCoord.lon, tmp); rep.content += tmp; rep.content += (","); convertLatLon(phantomNodes->targetCoord.lat, tmp); rep.content += tmp; } rep.content += (""); rep.content += (""); rep.content += (""); rep.content += (""); rep.content += (""); rep.headers.resize(3); rep.headers[0].name = "Content-Length"; intToString(rep.content.size(), tmp); rep.headers[0].value = tmp; rep.headers[1].name = "Content-Type"; rep.headers[1].value = "application/vnd.google-earth.kml+xml"; rep.headers[2].name = "Content-Disposition"; rep.headers[2].value = "attachment; filename=\"route.kml\""; delete path; delete phantomNodes; // timestamp2 = get_timestamp(); //cout << "description in " << timestamp2 - timestamp << "s" << endl; return; } rep = reply::stock_reply(reply::bad_request); return; } catch(std::exception& e) { //TODO: log exception somewhere rep = reply::stock_reply(reply::internal_server_error); return; } }; private: //SearchEngine object that is queried SearchEngine * sEngine; /* used to be boosts lexical cast, but this was too slow */ inline void doubleToString(const double value, std::string & output) { // The largest 32-bit integer is 4294967295, that is 10 chars // On the safe side, add 1 for sign, and 1 for trailing zero char buffer[12] ; sprintf(buffer, "%f", value) ; output = buffer ; } inline void intToString(const int value, std::string & output) { // The largest 32-bit integer is 4294967295, that is 10 chars // On the safe side, add 1 for sign, and 1 for trailing zero char buffer[12] ; sprintf(buffer, "%i", value) ; output = buffer ; } inline void convertLatLon(const int value, std::string & output) { char buffer[100]; buffer[10] = 0; // Nullterminierung char* string = printInt< 10, 5 >( buffer, value ); output = string; } // precision: Nachkommastellen // length: Maximallänge inklusive Komma template< int length, int precision > inline char* printInt( char* buffer, int value ) { bool minus = false; if ( value < 0 ) { minus = true; value = -value; } buffer += length - 1; for ( int i = 0; i < precision; i++ ) { *buffer = '0' + ( value % 10 ); value /= 10; buffer--; } *buffer = '.'; buffer--; for ( int i = precision + 1; i < length; i++ ) { *buffer = '0' + ( value % 10 ); value /= 10; if ( value == 0 ) break; buffer--; } if ( minus ) { buffer--; *buffer = '-'; } return buffer; } void computeDescription(string &tmp, vector * path, PhantomNodes * phantomNodes) { _Coordinate previous(phantomNodes->startCoord.lat, phantomNodes->startCoord.lon); _Coordinate next, current, lastPlace; stringstream numberString; double tempDist = 0; NodeID nextID = UINT_MAX; NodeID nameID = sEngine->GetNameIDForOriginDestinationNodeID(phantomNodes->startNode1, phantomNodes->startNode2); short type = sEngine->GetTypeOfEdgeForOriginDestinationNodeID(phantomNodes->startNode1, phantomNodes->startNode2); lastPlace.lat = phantomNodes->startCoord.lat; lastPlace.lon = phantomNodes->startCoord.lon; short nextType = SHRT_MAX; short prevType = SHRT_MAX; tmp += ""; for(vector::iterator it = path->begin(); it != path->end(); it++) { sEngine->getNodeInfo(*it, current); if(it==path->end()-1){ next = _Coordinate(phantomNodes->targetCoord.lat, phantomNodes->targetCoord.lon); nextID = sEngine->GetNameIDForOriginDestinationNodeID(phantomNodes->targetNode1, phantomNodes->targetNode2); nextType = sEngine->GetTypeOfEdgeForOriginDestinationNodeID(phantomNodes->targetNode1, phantomNodes->targetNode2); } else { sEngine->getNodeInfo(*(it+1), next); nextID = sEngine->GetNameIDForOriginDestinationNodeID(*it, *(it+1)); nextType = sEngine->GetTypeOfEdgeForOriginDestinationNodeID(*it, *(it+1)); } if(nextID == nameID) { tempDist += ApproximateDistance(previous.lat, previous.lon, current.lat, current.lon); } else { if(type == 0 && prevType != 0) tmp += "enter motorway and "; if(type != 0 && prevType == 0 ) tmp += "leave motorway and "; double angle = GetAngleBetweenTwoEdges(previous, current, next); tmp += "follow road "; tmp += sEngine->GetNameForNameID(nameID); tmp += " (type: "; numberString << type; tmp += numberString.str(); numberString.str(""); tmp += ")drive for "; numberString << ApproximateDistance(previous.lat, previous.lon, current.lat, current.lon)+tempDist; tmp += numberString.str(); numberString.str(""); tmp += "m "; string lat; string lon; convertLatLon(lastPlace.lon, lon); convertLatLon(lastPlace.lat, lat); lastPlace = current; tmp += ""; tmp += lon; tmp += ","; tmp += lat; tmp += ""; tmp += ""; tmp += ""; if(angle > 160 && angle < 200) { tmp += /* " (" << angle << ")*/"drive ahead, "; } else if (angle > 290 && angle <= 360) { tmp += /*" (" << angle << ")*/ "turn sharp left, "; } else if (angle > 245 && angle <= 290) { tmp += /*" (" << angle << ")*/ "turn left, "; } else if (angle > 200 && angle <= 245) { tmp += /*" (" << angle << ") */"bear left, "; } else if (angle > 115 && angle <= 160) { tmp += /*" (" << angle << ") */"bear right, "; } else if (angle > 70 && angle <= 115) { tmp += /*" (" << angle << ") */"turn right, "; } else { tmp += /*" (" << angle << ") */"turn sharp right, "; } tempDist = 0; prevType = type; } nameID = nextID; previous = current; type = nextType; } nameID = sEngine->GetNameIDForOriginDestinationNodeID(phantomNodes->targetNode1, phantomNodes->targetNode2); type = sEngine->GetTypeOfEdgeForOriginDestinationNodeID(phantomNodes->targetNode1, phantomNodes->targetNode2); tmp += "follow road "; tmp += sEngine->GetNameForNameID(nameID); tmp += " (type: "; numberString << type; tmp += numberString.str(); numberString.str(""); tmp += ") drive for "; numberString << (previous.lat, previous.lon, phantomNodes->targetCoord.lat, phantomNodes->targetCoord.lon) + tempDist; tmp += numberString.str(); numberString.str(""); tmp += "m"; string lat; string lon; convertLatLon(lastPlace.lon, lon); convertLatLon(lastPlace.lat, lat); tmp += ""; tmp += lon; tmp += ","; tmp += lat; tmp += ""; tmp += ""; tmp += "you have reached your destination"; tmp += "End of Route"; convertLatLon(phantomNodes->targetCoord.lon, lon); convertLatLon(phantomNodes->targetCoord.lat, lat); tmp += ""; tmp += lon; tmp += ","; tmp += lat; tmp +=""; tmp += ""; } }; } #endif // HTTP_ROUTER_REQUEST_HANDLER_HPP