Implements issue #173

This commit is contained in:
DennisOSRM 2012-05-04 14:49:30 +02:00
parent cacf8f17d3
commit c4f213f64e
4 changed files with 76 additions and 53 deletions

View File

@ -231,7 +231,8 @@ public:
FindPhantomNodeForCoordinate( target, routingStarts.targetPhantom) ); FindPhantomNodeForCoordinate( target, routingStarts.targetPhantom) );
} }
void FindNearestCoordinateOnEdgeInNodeBasedGraph(const _Coordinate& inputCoordinate, _Coordinate& outputCoordinate) { bool FindNearestCoordinateOnEdgeInNodeBasedGraph(const _Coordinate& inputCoordinate, _Coordinate& outputCoordinate) {
bool found = false;
unsigned fileIndex = GetFileIndexForLatLon(100000*(lat2y(static_cast<double>(inputCoordinate.lat)/100000.)), inputCoordinate.lon); unsigned fileIndex = GetFileIndexForLatLon(100000*(lat2y(static_cast<double>(inputCoordinate.lat)/100000.)), inputCoordinate.lon);
std::vector<_GridEdge> candidates; std::vector<_GridEdge> candidates;
boost::unordered_map< unsigned, unsigned, IdenticalHashFunction > cellMap; boost::unordered_map< unsigned, unsigned, IdenticalHashFunction > cellMap;
@ -246,11 +247,13 @@ public:
double r = 0.; double r = 0.;
double tmpDist = ComputeDistance(inputCoordinate, candidate.startCoord, candidate.targetCoord, tmp, &r); double tmpDist = ComputeDistance(inputCoordinate, candidate.startCoord, candidate.targetCoord, tmp, &r);
if(tmpDist < dist) { if(tmpDist < dist) {
found = true;
dist = tmpDist; dist = tmpDist;
outputCoordinate = tmp; outputCoordinate = tmp;
} }
} }
outputCoordinate.lat = 100000*(y2lat(static_cast<double>(outputCoordinate.lat)/100000.)); outputCoordinate.lat = 100000*(y2lat(static_cast<double>(outputCoordinate.lat)/100000.));
return found;
} }
void FindNearestPointOnEdge(const _Coordinate& inputCoordinate, _Coordinate& outputCoordinate) { void FindNearestPointOnEdge(const _Coordinate& inputCoordinate, _Coordinate& outputCoordinate) {

View File

@ -92,11 +92,11 @@ public:
inline NodeID getNumberOfNodes() const { return numberOfNodes; } inline NodeID getNumberOfNodes() const { return numberOfNodes; }
inline NodeID getNumberOfNodes2() const { return coordinateVector.size(); } inline NodeID getNumberOfNodes2() const { return coordinateVector.size(); }
inline void FindNearestNodeCoordForLatLon(const _Coordinate& coord, _Coordinate& result) const { inline bool FindNearestNodeCoordForLatLon(const _Coordinate& coord, _Coordinate& result) const {
readOnlyGrid->FindNearestCoordinateOnEdgeInNodeBasedGraph(coord, result); return readOnlyGrid->FindNearestCoordinateOnEdgeInNodeBasedGraph(coord, result);
} }
inline void FindPhantomNodeForCoordinate( const _Coordinate & location, PhantomNode & resultNode) const { inline bool FindPhantomNodeForCoordinate( const _Coordinate & location, PhantomNode & resultNode) const {
readOnlyGrid->FindPhantomNodeForCoordinate(location, resultNode); return readOnlyGrid->FindPhantomNodeForCoordinate(location, resultNode);
} }
inline void FindRoutingStarts(const _Coordinate &start, const _Coordinate &target, PhantomNodes & phantomNodes) const { inline void FindRoutingStarts(const _Coordinate &start, const _Coordinate &target, PhantomNodes & phantomNodes) const {

View File

@ -41,24 +41,28 @@ public:
std::string GetVersionString() const { return std::string("0.3 (DL)"); } std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) { void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
//check number of parameters //check number of parameters
if(routeParameters.parameters.size() != 2) { if(!routeParameters.viaPoints.size()) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
std::vector<std::string> textCoord;
stringSplit (routeParameters.viaPoints[0], ',', textCoord);
if(textCoord.size() != 2) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
return; return;
} }
int lat = static_cast<int>(100000.*atof(routeParameters.parameters[0].c_str())); int lat = 100000.*atof(textCoord[0].c_str());
int lon = static_cast<int>(100000.*atof(routeParameters.parameters[1].c_str())); int lon = 100000.*atof(textCoord[1].c_str());
_Coordinate myCoordinate(lat, lon);
if(lat>90*100000 || lat <-90*100000 || lon>180*100000 || lon <-180*100000) { if(false == checkCoord(myCoordinate)) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
return; return;
} }
//query to helpdesk //query to helpdesk
_Coordinate result; _Coordinate result;
nodeHelpDesk->FindNearestNodeCoordForLatLon(_Coordinate(lat, lon), result); std::string JSONParameter, tmp;
std::string tmp;
std::string JSONParameter;
//json //json
JSONParameter = routeParameters.options.Find("jsonp"); JSONParameter = routeParameters.options.Find("jsonp");
@ -66,13 +70,17 @@ public:
reply.content += JSONParameter; reply.content += JSONParameter;
reply.content += "("; reply.content += "(";
} }
//Write to stream
reply.status = http::Reply::ok; reply.status = http::Reply::ok;
reply.content += ("{"); reply.content += ("{");
reply.content += ("\"version\":0.3,"); reply.content += ("\"version\":0.3,");
if(!nodeHelpDesk->FindNearestNodeCoordForLatLon(myCoordinate, result)) {
reply.content += ("\"status\":207,");
reply.content += ("\"mapped_coordinate\":[]");
} else {
//Write coordinate to stream
reply.status = http::Reply::ok;
reply.content += ("\"status\":0,"); reply.content += ("\"status\":0,");
reply.content += ("\"result\":"); reply.content += ("\"mapped_coordinate\":");
convertInternalLatLonToString(result.lat, tmp); convertInternalLatLonToString(result.lat, tmp);
reply.content += "["; reply.content += "[";
reply.content += tmp; reply.content += tmp;
@ -80,6 +88,7 @@ public:
reply.content += ","; reply.content += ",";
reply.content += tmp; reply.content += tmp;
reply.content += "]"; reply.content += "]";
}
reply.content += ",\"transactionId\": \"OSRM Routing Engine JSON Locate (v0.3)\""; reply.content += ",\"transactionId\": \"OSRM Routing Engine JSON Locate (v0.3)\"";
reply.content += ("}"); reply.content += ("}");
reply.headers.resize(3); reply.headers.resize(3);
@ -101,6 +110,13 @@ public:
return; return;
} }
private: 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;
}
NodeInformationHelpDesk * nodeHelpDesk; NodeInformationHelpDesk * nodeHelpDesk;
}; };

View File

@ -46,11 +46,9 @@ public:
std::string GetDescriptor() const { return std::string("nearest"); } std::string GetDescriptor() const { return std::string("nearest"); }
std::string GetVersionString() const { return std::string("0.3 (DL)"); } std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) { void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
INFO("1");
//check number of parameters //check number of parameters
if(!routeParameters.viaPoints.size()) { if(!routeParameters.viaPoints.size()) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
INFO("2, size: " << routeParameters.viaPoints.size());
return; return;
} }
std::vector<std::string> textCoord; std::vector<std::string> textCoord;
@ -65,10 +63,8 @@ public:
_Coordinate myCoordinate(lat, lon); _Coordinate myCoordinate(lat, lon);
if(false == checkCoord(myCoordinate)) { if(false == checkCoord(myCoordinate)) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
INFO("3");
return; return;
} }
INFO("4");
//query to helpdesk //query to helpdesk
PhantomNode result; PhantomNode result;
nodeHelpDesk->FindPhantomNodeForCoordinate(myCoordinate, result); nodeHelpDesk->FindPhantomNodeForCoordinate(myCoordinate, result);
@ -86,15 +82,23 @@ public:
reply.status = http::Reply::ok; reply.status = http::Reply::ok;
reply.content += ("{"); reply.content += ("{");
reply.content += ("\"version\":0.3,"); reply.content += ("\"version\":0.3,");
reply.content += ("\"status\":0,"); reply.content += ("\"status\":");
reply.content += ("\"result\":"); if(UINT_MAX != result.edgeBasedNode)
convertInternalLatLonToString(result.location.lat, tmp); reply.content += "0,";
else
reply.content += "207,";
reply.content += ("\"mapped_coordinate\":");
reply.content += "["; reply.content += "[";
if(UINT_MAX != result.edgeBasedNode) {
convertInternalLatLonToString(result.location.lat, tmp);
reply.content += tmp; reply.content += tmp;
convertInternalLatLonToString(result.location.lon, tmp); convertInternalLatLonToString(result.location.lon, tmp);
reply.content += ","; reply.content += ",";
reply.content += tmp; reply.content += tmp;
reply.content += "], \"name\": \""; }
reply.content += "],";
reply.content += "\"name\":\"";
if(UINT_MAX != result.edgeBasedNode)
reply.content += names[result.nodeBasedEdgeNameID]; reply.content += names[result.nodeBasedEdgeNameID];
reply.content += "\""; reply.content += "\"";
reply.content += ",\"transactionId\":\"OSRM Routing Engine JSON Nearest (v0.3)\""; reply.content += ",\"transactionId\":\"OSRM Routing Engine JSON Nearest (v0.3)\"";