2011-01-09 16:42:27 -05:00
|
|
|
/*
|
|
|
|
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 LOCATEPLUGIN_H_
|
|
|
|
#define LOCATEPLUGIN_H_
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
2012-04-14 14:07:30 -04:00
|
|
|
#include "../Server/DataStructures/QueryObjectsStorage.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
#include "BasePlugin.h"
|
2011-04-18 04:12:44 -04:00
|
|
|
#include "RouteParameters.h"
|
2012-02-23 10:29:55 -05:00
|
|
|
#include "../Util/StringUtil.h"
|
2011-01-09 16:42:27 -05:00
|
|
|
#include "../DataStructures/NodeInformationHelpDesk.h"
|
|
|
|
|
|
|
|
/*
|
2011-03-29 11:02:07 -04:00
|
|
|
* This Plugin locates the nearest node in the road network for a given coordinate.
|
2011-01-09 16:42:27 -05:00
|
|
|
*/
|
|
|
|
class LocatePlugin : public BasePlugin {
|
|
|
|
public:
|
2012-04-14 14:07:30 -04:00
|
|
|
LocatePlugin(QueryObjectsStorage * objects) {
|
2011-05-26 05:16:04 -04:00
|
|
|
nodeHelpDesk = objects->nodeHelpDesk;
|
|
|
|
}
|
2012-05-04 08:49:30 -04:00
|
|
|
std::string GetDescriptor() const { return std::string("locate"); }
|
|
|
|
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
|
|
|
|
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
|
|
|
|
//check number of parameters
|
2012-09-12 09:01:37 -04:00
|
|
|
if(!routeParameters.coordinates.size()) {
|
2012-05-04 08:49:30 -04:00
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-12 09:01:37 -04:00
|
|
|
if(false == checkCoord(routeParameters.coordinates[0])) {
|
2012-05-04 08:49:30 -04:00
|
|
|
reply = http::Reply::stockReply(http::Reply::badRequest);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2012-05-04 08:49:30 -04:00
|
|
|
//query to helpdesk
|
|
|
|
_Coordinate result;
|
|
|
|
std::string JSONParameter, tmp;
|
2012-02-23 10:29:55 -05:00
|
|
|
//json
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2012-09-12 09:01:37 -04:00
|
|
|
// JSONParameter = routeParameters.options.Find("jsonp");
|
|
|
|
if("" != routeParameters.jsonpParameter) {
|
2012-02-23 10:29:55 -05:00
|
|
|
reply.content += JSONParameter;
|
|
|
|
reply.content += "(";
|
|
|
|
}
|
|
|
|
reply.status = http::Reply::ok;
|
|
|
|
reply.content += ("{");
|
|
|
|
reply.content += ("\"version\":0.3,");
|
2012-09-12 09:01:37 -04:00
|
|
|
if(!nodeHelpDesk->FindNearestNodeCoordForLatLon(routeParameters.coordinates[0], result)) {
|
2012-05-04 08:49:30 -04:00
|
|
|
reply.content += ("\"status\":207,");
|
|
|
|
reply.content += ("\"mapped_coordinate\":[]");
|
|
|
|
} else {
|
|
|
|
//Write coordinate to stream
|
|
|
|
reply.status = http::Reply::ok;
|
|
|
|
reply.content += ("\"status\":0,");
|
|
|
|
reply.content += ("\"mapped_coordinate\":");
|
|
|
|
convertInternalLatLonToString(result.lat, tmp);
|
|
|
|
reply.content += "[";
|
|
|
|
reply.content += tmp;
|
|
|
|
convertInternalLatLonToString(result.lon, tmp);
|
|
|
|
reply.content += ",";
|
|
|
|
reply.content += tmp;
|
|
|
|
reply.content += "]";
|
|
|
|
}
|
2012-02-23 10:29:55 -05:00
|
|
|
reply.content += ",\"transactionId\": \"OSRM Routing Engine JSON Locate (v0.3)\"";
|
|
|
|
reply.content += ("}");
|
|
|
|
reply.headers.resize(3);
|
|
|
|
if("" != JSONParameter) {
|
|
|
|
reply.content += ")";
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "text/javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"location.js\"";
|
|
|
|
} else {
|
|
|
|
reply.headers[1].name = "Content-Type";
|
|
|
|
reply.headers[1].value = "application/x-javascript";
|
|
|
|
reply.headers[2].name = "Content-Disposition";
|
|
|
|
reply.headers[2].value = "attachment; filename=\"location.json\"";
|
|
|
|
}
|
|
|
|
reply.headers[0].name = "Content-Length";
|
|
|
|
intToString(reply.content.size(), tmp);
|
|
|
|
reply.headers[0].value = tmp;
|
2012-05-04 08:49:30 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-01-09 16:42:27 -05:00
|
|
|
private:
|
2012-05-04 08:49:30 -04:00
|
|
|
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;
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* LOCATEPLUGIN_H_ */
|