Merge pull request #242 from DennisOSRM/develop/enhancedNearestPlugin

Develop/enhanced nearest plugin
This commit is contained in:
Project OSRM 2012-05-04 05:56:47 -07:00
commit 71b90ab216
4 changed files with 100 additions and 57 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

@ -37,28 +37,32 @@ public:
LocatePlugin(QueryObjectsStorage * objects) { LocatePlugin(QueryObjectsStorage * objects) {
nodeHelpDesk = objects->nodeHelpDesk; nodeHelpDesk = objects->nodeHelpDesk;
} }
std::string GetDescriptor() const { return std::string("locate"); } std::string GetDescriptor() const { return std::string("locate"); }
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); reply = http::Reply::stockReply(http::Reply::badRequest);
return; return;
} }
std::vector<std::string> textCoord;
stringSplit (routeParameters.viaPoints[0], ',', textCoord);
if(textCoord.size() != 2) {
reply = http::Reply::stockReply(http::Reply::badRequest);
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(false == checkCoord(myCoordinate)) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
if(lat>90*100000 || lat <-90*100000 || lon>180*100000 || lon <-180*100000) { //query to helpdesk
reply = http::Reply::stockReply(http::Reply::badRequest); _Coordinate result;
return; std::string JSONParameter, tmp;
}
//query to helpdesk
_Coordinate result;
nodeHelpDesk->FindNearestNodeCoordForLatLon(_Coordinate(lat, lon), result);
std::string tmp;
std::string JSONParameter;
//json //json
JSONParameter = routeParameters.options.Find("jsonp"); JSONParameter = routeParameters.options.Find("jsonp");
@ -66,20 +70,25 @@ 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,");
reply.content += ("\"status\":0,"); if(!nodeHelpDesk->FindNearestNodeCoordForLatLon(myCoordinate, result)) {
reply.content += ("\"result\":"); reply.content += ("\"status\":207,");
convertInternalLatLonToString(result.lat, tmp); reply.content += ("\"mapped_coordinate\":[]");
reply.content += "["; } else {
reply.content += tmp; //Write coordinate to stream
convertInternalLatLonToString(result.lon, tmp); reply.status = http::Reply::ok;
reply.content += ", "; reply.content += ("\"status\":0,");
reply.content += tmp; reply.content += ("\"mapped_coordinate\":");
reply.content += "]"; convertInternalLatLonToString(result.lat, tmp);
reply.content += "[";
reply.content += tmp;
convertInternalLatLonToString(result.lon, tmp);
reply.content += ",";
reply.content += tmp;
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);
@ -98,10 +107,17 @@ public:
reply.headers[0].name = "Content-Length"; reply.headers[0].name = "Content-Length";
intToString(reply.content.size(), tmp); intToString(reply.content.size(), tmp);
reply.headers[0].value = tmp; reply.headers[0].value = tmp;
return; return;
} }
private: private:
NodeInformationHelpDesk * nodeHelpDesk; 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;
}; };
#endif /* LOCATEPLUGIN_H_ */ #endif /* LOCATEPLUGIN_H_ */

View File

@ -37,31 +37,37 @@ or see http://www.gnu.org/licenses/agpl.txt.
*/ */
class NearestPlugin : public BasePlugin { class NearestPlugin : public BasePlugin {
public: public:
NearestPlugin(QueryObjectsStorage * objects) { NearestPlugin(QueryObjectsStorage * objects) : names(objects->names) {
nodeHelpDesk = objects->nodeHelpDesk; nodeHelpDesk = objects->nodeHelpDesk;
descriptorTable.Set("", 0); //default descriptor descriptorTable.Set("", 0); //default descriptor
descriptorTable.Set("kml", 0);
descriptorTable.Set("json", 1); descriptorTable.Set("json", 1);
} }
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) {
//check number of parameters //check number of parameters
if(routeParameters.parameters.size() != 2) { if(!routeParameters.viaPoints.size()) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
return; return;
} }
std::vector<std::string> textCoord;
stringSplit (routeParameters.viaPoints[0], ',', textCoord);
if(textCoord.size() != 2) {
reply = http::Reply::stockReply(http::Reply::badRequest);
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; PhantomNode result;
nodeHelpDesk->FindNearestPointOnEdge(_Coordinate(lat, lon), result); nodeHelpDesk->FindPhantomNodeForCoordinate(myCoordinate, result);
std::string tmp; std::string tmp;
std::string JSONParameter; std::string JSONParameter;
@ -76,16 +82,26 @@ 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.lat, tmp); reply.content += "0,";
else
reply.content += "207,";
reply.content += ("\"mapped_coordinate\":");
reply.content += "["; reply.content += "[";
reply.content += tmp; if(UINT_MAX != result.edgeBasedNode) {
convertInternalLatLonToString(result.lon, tmp); convertInternalLatLonToString(result.location.lat, tmp);
reply.content += ", "; reply.content += tmp;
reply.content += tmp; convertInternalLatLonToString(result.location.lon, tmp);
reply.content += "]"; reply.content += ",";
reply.content += ",\"transactionId\": \"OSRM Routing Engine JSON Nearest (v0.3)\""; reply.content += tmp;
}
reply.content += "],";
reply.content += "\"name\":\"";
if(UINT_MAX != result.edgeBasedNode)
reply.content += names[result.nodeBasedEdgeNameID];
reply.content += "\"";
reply.content += ",\"transactionId\":\"OSRM Routing Engine JSON Nearest (v0.3)\"";
reply.content += ("}"); reply.content += ("}");
reply.headers.resize(3); reply.headers.resize(3);
if("" != JSONParameter) { if("" != JSONParameter) {
@ -105,8 +121,16 @@ public:
reply.headers[0].value = tmp; reply.headers[0].value = tmp;
} }
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;
HashTable<std::string, unsigned> descriptorTable; HashTable<std::string, unsigned> descriptorTable;
std::vector<std::string> & names;
}; };
#endif /* NearestPlugin_H_ */ #endif /* NearestPlugin_H_ */