2011-03-29 11:02:07 -04:00
|
|
|
/*
|
2013-10-14 07:42:28 -04:00
|
|
|
|
2015-02-19 03:19:51 -05:00
|
|
|
Copyright (c) 2015, Project OSRM contributors
|
2013-10-14 07:42:28 -04:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
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-03-29 11:02:07 -04:00
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
#ifndef NEAREST_HPP
|
|
|
|
#define NEAREST_HPP
|
2011-03-29 11:02:07 -04:00
|
|
|
|
2014-11-28 09:13:29 -05:00
|
|
|
#include "plugin_base.hpp"
|
|
|
|
|
2014-11-28 06:13:18 -05:00
|
|
|
#include "../data_structures/phantom_node.hpp"
|
2015-01-27 11:44:46 -05:00
|
|
|
#include "../util/integer_range.hpp"
|
|
|
|
#include "../util/json_renderer.hpp"
|
2011-03-29 11:02:07 -04:00
|
|
|
|
2014-11-24 11:57:01 -05:00
|
|
|
#include <osrm/json_container.hpp>
|
|
|
|
|
2014-05-16 10:09:40 -04:00
|
|
|
#include <string>
|
2014-02-11 05:35:29 -05:00
|
|
|
|
2011-03-29 11:02:07 -04:00
|
|
|
/*
|
|
|
|
* This Plugin locates the nearest point on a street in the road network for a given coordinate.
|
|
|
|
*/
|
2013-09-18 12:32:50 -04:00
|
|
|
|
2014-10-13 11:29:53 -04:00
|
|
|
template <class DataFacadeT> class NearestPlugin final : public BasePlugin
|
2014-05-02 12:06:31 -04:00
|
|
|
{
|
|
|
|
public:
|
2014-06-23 11:33:55 -04:00
|
|
|
explicit NearestPlugin(DataFacadeT *facade) : facade(facade), descriptor_string("nearest") {}
|
2014-05-02 12:06:31 -04:00
|
|
|
|
2015-01-28 04:10:28 -05:00
|
|
|
const std::string GetDescriptor() const override final { return descriptor_string; }
|
2014-05-02 12:06:31 -04:00
|
|
|
|
2015-01-28 04:10:28 -05:00
|
|
|
int HandleRequest(const RouteParameters &route_parameters,
|
2015-02-18 04:46:40 -05:00
|
|
|
osrm::json::Object &json_result) override final
|
2014-05-02 12:06:31 -04:00
|
|
|
{
|
|
|
|
// check number of parameters
|
2015-01-27 11:44:46 -05:00
|
|
|
if (route_parameters.coordinates.empty() ||
|
|
|
|
!route_parameters.coordinates.front().is_valid())
|
2014-05-02 12:06:31 -04:00
|
|
|
{
|
2014-11-25 03:14:01 -05:00
|
|
|
return 400;
|
2011-05-10 06:24:13 -04:00
|
|
|
}
|
2014-10-17 08:19:33 -04:00
|
|
|
auto number_of_results = static_cast<std::size_t>(route_parameters.num_results);
|
2014-06-23 11:33:55 -04:00
|
|
|
std::vector<PhantomNode> phantom_node_vector;
|
|
|
|
facade->IncrementalFindPhantomNodeForCoordinate(route_parameters.coordinates.front(),
|
|
|
|
phantom_node_vector,
|
2014-10-17 08:19:33 -04:00
|
|
|
static_cast<int>(number_of_results));
|
2014-10-13 11:29:53 -04:00
|
|
|
|
2014-10-28 10:39:29 -04:00
|
|
|
if (phantom_node_vector.empty() || !phantom_node_vector.front().is_valid())
|
2014-05-02 12:06:31 -04:00
|
|
|
{
|
2014-05-16 10:09:40 -04:00
|
|
|
json_result.values["status"] = 207;
|
2014-05-02 12:06:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-25 03:14:01 -05:00
|
|
|
// reply.status = http::Reply::ok;
|
2014-05-16 10:09:40 -04:00
|
|
|
json_result.values["status"] = 0;
|
2014-10-13 11:29:53 -04:00
|
|
|
|
2014-08-08 14:06:06 -04:00
|
|
|
if (number_of_results > 1)
|
|
|
|
{
|
2015-02-18 04:46:40 -05:00
|
|
|
osrm::json::Array results;
|
2014-10-13 11:29:53 -04:00
|
|
|
|
2014-10-17 08:19:33 -04:00
|
|
|
auto vector_length = phantom_node_vector.size();
|
2015-01-27 11:44:46 -05:00
|
|
|
for (const auto i :
|
|
|
|
osrm::irange<std::size_t>(0, std::min(number_of_results, vector_length)))
|
2014-08-08 14:06:06 -04:00
|
|
|
{
|
2015-02-18 04:46:40 -05:00
|
|
|
osrm::json::Array json_coordinate;
|
|
|
|
osrm::json::Object result;
|
2014-08-08 14:06:06 -04:00
|
|
|
json_coordinate.values.push_back(phantom_node_vector.at(i).location.lat /
|
|
|
|
COORDINATE_PRECISION);
|
|
|
|
json_coordinate.values.push_back(phantom_node_vector.at(i).location.lon /
|
|
|
|
COORDINATE_PRECISION);
|
|
|
|
result.values["mapped coordinate"] = json_coordinate;
|
2015-02-26 04:11:33 -05:00
|
|
|
result.values["name"] =
|
|
|
|
facade->get_name_for_id(phantom_node_vector.at(i).name_id);
|
2014-08-08 14:06:06 -04:00
|
|
|
results.values.push_back(result);
|
|
|
|
}
|
|
|
|
json_result.values["results"] = results;
|
|
|
|
}
|
|
|
|
else
|
2014-08-07 17:15:27 -04:00
|
|
|
{
|
2015-02-18 04:46:40 -05:00
|
|
|
osrm::json::Array json_coordinate;
|
2014-08-08 14:06:06 -04:00
|
|
|
json_coordinate.values.push_back(phantom_node_vector.front().location.lat /
|
2014-08-07 17:15:27 -04:00
|
|
|
COORDINATE_PRECISION);
|
2014-08-08 14:06:06 -04:00
|
|
|
json_coordinate.values.push_back(phantom_node_vector.front().location.lon /
|
2014-08-07 17:15:27 -04:00
|
|
|
COORDINATE_PRECISION);
|
2014-08-08 14:06:06 -04:00
|
|
|
json_result.values["mapped_coordinate"] = json_coordinate;
|
2015-02-26 04:11:33 -05:00
|
|
|
json_result.values["name"] =
|
|
|
|
facade->get_name_for_id(phantom_node_vector.front().name_id);
|
2014-08-07 17:15:27 -04:00
|
|
|
}
|
2013-08-20 11:05:36 -04:00
|
|
|
}
|
2014-11-25 03:14:01 -05:00
|
|
|
return 200;
|
2011-05-10 06:24:13 -04:00
|
|
|
}
|
2012-05-03 17:48:20 -04:00
|
|
|
|
2014-05-02 12:06:31 -04:00
|
|
|
private:
|
|
|
|
DataFacadeT *facade;
|
2013-08-13 12:09:20 -04:00
|
|
|
std::string descriptor_string;
|
2011-03-29 11:02:07 -04:00
|
|
|
};
|
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
#endif /* NEAREST_HPP */
|