2011-01-09 16:42:27 -05:00
|
|
|
/*
|
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-10-14 07:42:28 -04:00
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2011-01-09 16:42:27 -05:00
|
|
|
|
|
|
|
#ifndef LOCATEPLUGIN_H_
|
|
|
|
#define LOCATEPLUGIN_H_
|
|
|
|
|
|
|
|
#include "BasePlugin.h"
|
2013-06-26 09:32:03 -04:00
|
|
|
#include "../Util/StringUtil.h"
|
|
|
|
|
2013-09-19 12:53:10 -04:00
|
|
|
//locates the nearest node in the road network for a given coordinate.
|
2013-09-18 12:32:50 -04:00
|
|
|
//TODO: Rework data access to go through facade
|
|
|
|
|
|
|
|
template<class DataFacadeT>
|
2011-01-09 16:42:27 -05:00
|
|
|
class LocatePlugin : public BasePlugin {
|
|
|
|
public:
|
2013-09-19 12:53:10 -04:00
|
|
|
LocatePlugin(DataFacadeT * facade)
|
|
|
|
:
|
|
|
|
descriptor_string("locate"),
|
|
|
|
facade(facade)
|
|
|
|
{ }
|
2013-08-13 12:09:20 -04:00
|
|
|
const std::string & GetDescriptor() const { return descriptor_string; }
|
2013-09-19 12:53:10 -04:00
|
|
|
|
|
|
|
void HandleRequest(
|
|
|
|
const RouteParameters & routeParameters,
|
|
|
|
http::Reply& reply
|
|
|
|
) {
|
2012-05-04 08:49:30 -04:00
|
|
|
//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
|
2013-08-14 07:12:28 -04:00
|
|
|
FixedPointCoordinate result;
|
2013-02-01 07:21:12 -05:00
|
|
|
std::string tmp;
|
2012-02-23 10:29:55 -05:00
|
|
|
//json
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2013-09-19 04:19:54 -04:00
|
|
|
if(!routeParameters.jsonpParameter.empty()) {
|
2013-02-01 07:21:12 -05:00
|
|
|
reply.content += routeParameters.jsonpParameter;
|
2012-02-23 10:29:55 -05:00
|
|
|
reply.content += "(";
|
|
|
|
}
|
|
|
|
reply.status = http::Reply::ok;
|
|
|
|
reply.content += ("{");
|
|
|
|
reply.content += ("\"version\":0.3,");
|
2013-09-19 12:53:10 -04:00
|
|
|
if(!facade->LocateClosestEndPointForCoordinate(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);
|
2013-09-19 04:19:54 -04:00
|
|
|
if(!routeParameters.jsonpParameter.empty()) {
|
2012-02-23 10:29:55 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-07 08:02:10 -04:00
|
|
|
private:
|
2013-08-13 12:09:20 -04:00
|
|
|
std::string descriptor_string;
|
2013-09-19 12:53:10 -04:00
|
|
|
DataFacadeT * facade;
|
2011-01-09 16:42:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* LOCATEPLUGIN_H_ */
|