Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3party] Restoring the server-side OSRM plugin. #14170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions 3party/osrm/BUILD_NOTE.txt

This file was deleted.

54 changes: 54 additions & 0 deletions 3party/osrm/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
To build the Mapsme plugin for osrm-backend:
============================================

0. Build the regular omim project in the directory ${OMIM_BUILD_PATH}.

1. Ignore all cmake files except those in omim/3party/osrm/osrm-backend, i.e.
run

> cd osrm-backend-build
> cmake ${OMIM_ROOT}/3party/osrm/osrm-backend/ -DOMIM_BUILD_PATH=${OMIM_BUILD_PATH}

2. Build the usual OSRM tools.

> make -k -j4 osrm-extract osrm-prepare osrm-routed

3. Run the OSRM pipeline on an *.osm.pbf file, see project OSRM docs or use
omim/tools/unix/generate_planet_routing.sh to do it automatically.

4. Run the routing daemon (osrm-routed).

5. The mapsme API that current version of the repository expects is

GET /{service}?loc={latlon1}&loc={latlon2}

where {latlon1} and {latlon2} are {coordinates} of source and destination points, respectively, in the usual OSRM format.

For example, if osrm-routed runs on localhost on the default port 5000, use

> curl "http://127.0.0.1:5000/mapsme?loc=55,37&loc=55,40"

The expected response is (note the omim-mercator coordinates)
{"used_mwms":[[36.140419,60.524949,"Russia_Kursk Oblast"],[37.872379,67.58203,"Russia_Moscow Oblast_East"],[37.557854,67.355247,"Russia_Moscow"]]}




LUABIND
=======

If you youse luabind library with a version lesser than 0.9.2,
you may have troubles compiling OSRM code with a boost >=1.57
(more details here: https://github.com/luabind/luabind/issues/27 ).

You can fix it either by using latest luabind from source https://github.com/rpavlik/luabind
or https://github.com/DennisOSRM/luabind, or by applying the following patch
to /usr/include/luabind/object.hpp:
https://github.com/alex85k/luabind/commit/2340928e00f4d606255dd5d570e1e142353a5fdd


On macOS, luabind is no longer supported by Homebrew so you may need to

> brew edit luabind

and comment out the "disable!" line.
10 changes: 8 additions & 2 deletions 3party/osrm/osrm-backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ file(GLOB AlgorithmTestsGlob unit_tests/algorithms/*.cpp)

file(GLOB MapsMeSources mapsme/*.cpp)
file(GLOB MapsMeHeaders mapsme/*.h)
file(GLOB MapsMeGenerator "${OMIM_PATH}/storage/country.cpp" "${OMIM_PATH}/storage/country_decl.cpp" "${OMIM_PATH}/storage/country_info.cpp")

set(
OSRMSources
Expand All @@ -101,9 +100,16 @@ set(
${DatastructureGlob}
${AlgorithmGlob}
${HttpGlob}
${MapsMeGenerator}
)

# Hacky but hey, all this time we've been compiling plugins by including them from a cpp-file bypassing cmake.
# This can't be worse, can it?
# Problem is our 2015 version of OSRM must be built with C++98, and MapsMePlugin compiles
# files from omim/ (because this CMakeLists.txt is inherited from the OSRM project and is
# not integrated in the omim cmake hierarchy). So MapsMePlugin must be built with C++11 or higher.
# That's why we cannot move this line to the beginning of the file but everything works when it's here.
set(CMAKE_CXX_STANDARD 17)

add_library(COORDINATE OBJECT ${CoordinateGlob})
add_library(GITDESCRIPTION OBJECT util/git_sha.cpp)
add_library(OSRM ${OSRMSources} $<TARGET_OBJECTS:ANGLE> $<TARGET_OBJECTS:COORDINATE> $<TARGET_OBJECTS:GITDESCRIPTION> $<TARGET_OBJECTS:FINGERPRINT> $<TARGET_OBJECTS:COORDINATE> $<TARGET_OBJECTS:LOGGER> $<TARGET_OBJECTS:PHANTOMNODE> $<TARGET_OBJECTS:EXCEPTION> $<TARGET_OBJECTS:MERCATOR>)
Expand Down
20 changes: 15 additions & 5 deletions 3party/osrm/osrm-backend/mapsme/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../server/data_structures/internal_datafacade.hpp"

#include <iostream>
#include <utility>
#include <vector>

#include "../../../../base/bits.hpp"
#include "../../../../base/logging.hpp"
Expand All @@ -18,6 +20,17 @@
#include "../../../succinct/rs_bit_vector.hpp"
#include "../../../succinct/mapper.hpp"

#define ROUTING_MATRIX_FILE_TAG "mercedes"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это такой вариант обфускации? это ещё нужно или можно назвать routing_matrix и так далее?

#define ROUTING_EDGEDATA_FILE_TAG "daewoo"
#define ROUTING_EDGEID_FILE_TAG "infinity"
#define ROUTING_SHORTCUTS_FILE_TAG "skoda"
#define ROUTING_CROSS_CONTEXT_TAG "chrysler"

#define ROUTING_FTSEG_FILE_TAG "ftseg"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не используется

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поэтому и драфт
сейчас оживлены части, которые нужны для компиляции, но на самом деле не нужны
например, неправильно оставлять в routing/ файлы, не добавленные в cmake

эта версия уже рабочая, но я ещё доделываю

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

понятно, спасибо

#define ROUTING_NODEIND_TO_FTSEGIND_FILE_TAG "node2ftseg"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже не используется

эти define ещё нужны?


using namespace std;

namespace
{
template <typename T>
Expand Down Expand Up @@ -74,8 +87,6 @@ string EdgeDataToString(EdgeOsrmT const & d)
return ss.str();
}



void GenerateRoutingIndex(const std::string & fPath)
{
ServerPaths server_paths;
Expand Down Expand Up @@ -274,7 +285,7 @@ void GenerateRoutingIndex(const std::string & fPath)
container.Open(path);
typedef routing::OsrmDataFacade<QueryEdge::EdgeData> DataFacadeT;
DataFacadeT facadeNew;
facadeNew.Load(container);
// facadeNew.Load(container);

uint64_t edgesCount = facadeNew.GetNumberOfEdges() - copiedEdges + ignoredEdges;
std::cout << "Check node count " << facade.GetNumberOfNodes() << " == " << facadeNew.GetNumberOfNodes() << "..." << std::endl;
Expand Down Expand Up @@ -403,5 +414,4 @@ void GenerateRoutingIndex(const std::string & fPath)
}
PrintStatus(!error);
}

}
} // namespace mapsme
3 changes: 0 additions & 3 deletions 3party/osrm/osrm-backend/mapsme/converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

namespace mapsme
{


void GenerateRoutingIndex(const std::string & fPath);

}
108 changes: 63 additions & 45 deletions 3party/osrm/osrm-backend/plugins/MapsMePlugin.hpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
#pragma once

#include "../../../../base/string_utils.hpp"
#include "../../../../coding/files_container.hpp"
#include "../../../../coding/read_write_utils.hpp"
#include "../../../../defines.hpp"
#include "../../../../geometry/mercator.hpp"
#include "../../../../geometry/region2d.hpp"
#include "../../../../storage/country_decl.hpp"
#include "../../../../storage/country_polygon.hpp"
#include "plugin_base.hpp"

#include "../algorithms/object_encoder.hpp"
#include "../data_structures/search_engine.hpp"
#include "../data_structures/edge_based_node_data.hpp"
#include "../descriptors/descriptor_base.hpp"
#include "../descriptors/gpx_descriptor.hpp"
#include "../descriptors/json_descriptor.hpp"
#include "../util/integer_range.hpp"
#include "../util/json_renderer.hpp"
#include "../util/make_unique.hpp"
#include "../util/simple_logger.hpp"
#include "storage/country_decl.hpp"

#include "coding/files_container.hpp"
#include "coding/read_write_utils.hpp"

#include "geometry/region2d.hpp"

#include "base/string_utils.hpp"

#include "defines.hpp"

#include <algorithm>
#include <limits>
#include <memory>
#include <unordered_map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using TMapRepr = pair<size_t, m2::PointD>;
#include "3party/osrm/osrm-backend/algorithms/object_encoder.hpp"
#include "3party/osrm/osrm-backend/data_structures/edge_based_node_data.hpp"
#include "3party/osrm/osrm-backend/data_structures/search_engine.hpp"
#include "3party/osrm/osrm-backend/descriptors/descriptor_base.hpp"
#include "3party/osrm/osrm-backend/descriptors/gpx_descriptor.hpp"
#include "3party/osrm/osrm-backend/descriptors/json_descriptor.hpp"
#include "3party/osrm/osrm-backend/plugins/plugin_base.hpp"
#include "3party/osrm/osrm-backend/util/integer_range.hpp"
#include "3party/osrm/osrm-backend/util/json_renderer.hpp"
#include "3party/osrm/osrm-backend/util/make_unique.hpp"
#include "3party/osrm/osrm-backend/util/simple_logger.hpp"

using TMapRepr = std::pair<size_t, m2::PointD>;

class UsedMwmChecker
{
public:
static size_t constexpr kInvalidIndex = numeric_limits<size_t>::max();
static size_t constexpr kInvalidIndex = std::numeric_limits<size_t>::max();

UsedMwmChecker() : m_lastUsedMwm(kInvalidIndex) {}

Expand All @@ -49,7 +52,7 @@ class UsedMwmChecker
m_lastMwmPoints.push_back(pt);
}

vector<TMapRepr> const & GetUsedMwms()
std::vector<TMapRepr> const & GetUsedMwms()
{
// Get point from the last mwm.
CommitUsedPoints();
Expand Down Expand Up @@ -79,9 +82,9 @@ class UsedMwmChecker
m_lastMwmPoints.clear();
}

vector<TMapRepr> m_usedMwms;
std::vector<TMapRepr> m_usedMwms;
size_t m_lastUsedMwm;
vector<m2::PointD> m_lastMwmPoints;
std::vector<m2::PointD> m_lastMwmPoints;
};

template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
Expand All @@ -94,7 +97,7 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
public:
size_t m_res;

GetByPoint(std::vector<std::vector<m2::RegionD>> const &regions, m2::PointD const &pt)
GetByPoint(std::vector<std::vector<m2::RegionD>> const & regions, m2::PointD const &pt)
: m_pt(pt), m_regions(regions), m_res(-1)
{
}
Expand All @@ -103,13 +106,12 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
/// @return false If point is in country.
bool operator()(size_t id)
{
auto it =
find_if(m_regions[id].begin(), m_regions[id].end(), [&](m2::RegionD const &region)
{ return region.Contains(m_pt);});
if (it == m_regions[id].end())
return true;
m_res = id;
return false;
auto it = std::find_if(m_regions[id].begin(), m_regions[id].end(),
[&](m2::RegionD const & region) { return region.Contains(m_pt); });
if (it == m_regions[id].end())
return true;
m_res = id;
return false;
}
};

Expand All @@ -133,21 +135,26 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
uint32_t const count = ReadVarUint<uint32_t>(src);
for (size_t j = 0; j < count; ++j)
{
vector<m2::PointD> points;
std::vector<m2::PointD> points;
serial::LoadOuterPath(src, serial::GeometryCodingParams(), points);

m_regions[i].emplace_back(move(m2::RegionD(points.begin(), points.end())));
m_regions[i].emplace_back(std::move(m2::RegionD(points.begin(), points.end())));
}
}
m_searchEngine = osrm::make_unique<SearchEngine<DataFacadeT>>(facade);
}

template <class ToDo> void ForEachCountry(m2::PointD const &pt, ToDo &toDo) const
template <class ToDo>
void ForEachCountry(m2::PointD const & pt, ToDo & toDo) const
{
for (size_t i = 0; i < m_countries.size(); ++i)
if (m_countries[i].m_rect.IsPointInside(pt))
if (!toDo(i))
return;
for (size_t i = 0; i < m_countries.size(); ++i)
{
if (m_countries[i].m_rect.IsPointInside(pt))
{
if (!toDo(i))
return;
}
}
}

virtual ~MapsMePlugin() {}
Expand Down Expand Up @@ -196,12 +203,12 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
return phantom_pair.first.component_id != 0;
};

const bool every_phantom_is_in_tiny_cc =
bool const every_phantom_is_in_tiny_cc =
std::all_of(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list),
check_component_id_is_tiny);

// are all phantoms from a tiny cc?
const auto component_id = phantom_node_pair_list.front().first.component_id;
auto const component_id = phantom_node_pair_list.front().first.component_id;

auto check_component_id_is_equal = [component_id](const phantom_node_pair &phantom_pair)
{
Expand Down Expand Up @@ -238,7 +245,7 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin

osrm::for_each_pair(phantom_node_pair_list, build_phantom_pairs);

vector<bool> uturns;
std::vector<bool> uturns;
m_searchEngine->shortest_path(raw_route.segment_end_coordinates, uturns, raw_route);
if (INVALID_EDGE_WEIGHT == raw_route.shortest_path_length)
{
Expand All @@ -258,7 +265,7 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
if (data.m_segments.empty())
continue;
auto const & seg = data.m_segments.front();
m2::PointD pt = MercatorBounds::FromLatLon(seg.lat1, seg.lon1);
m2::PointD pt = FromLatLon(seg.lat1, seg.lon1);
GetByPoint doGet(m_regions, pt);
ForEachCountry(pt, doGet);
usedChecker.AddPoint(doGet.m_res, pt);
Expand All @@ -280,6 +287,17 @@ template <class DataFacadeT> class MapsMePlugin final : public BasePlugin
}

private:
// Reimplementation of coding/mercator.hpp due to namespace clash with OSRM's mercator struct.
double ClampY(double d) { return base::Clamp(d, -180.0, 180.0); }
double LonToX(double lon) { return lon; }
double LatToY(double lat)
{
double const sinx = std::sin(base::DegToRad(base::Clamp(lat, -86.0, 86.0)));
double const res = base::RadToDeg(0.5 * log((1.0 + sinx) / (1.0 - sinx)));
return ClampY(res);
}
m2::PointD FromLatLon(double lat, double lon) { return m2::PointD(LonToX(lon), LatToY(lat)); }

std::unique_ptr<SearchEngine<DataFacadeT>> m_searchEngine;
std::vector<storage::CountryDef> m_countries;
std::vector<std::vector<m2::RegionD>> m_regions;
Expand Down
7 changes: 4 additions & 3 deletions 3party/osrm/osrm-backend/plugins/WayIdPlugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

template <class DataFacadeT>
Expand Down Expand Up @@ -121,7 +122,7 @@ class WayIdPlugin final : public BasePlugin

osrm::for_each_pair(phantom_node_pair_list, build_phantom_pairs);

vector<bool> uturns;
std::vector<bool> uturns;
m_searchEngine->shortest_path(raw_route.segment_end_coordinates, uturns, raw_route);
if (INVALID_EDGE_WEIGHT == raw_route.shortest_path_length)
{
Expand All @@ -130,7 +131,7 @@ class WayIdPlugin final : public BasePlugin
}

// Get ids of ways used in path.
set<uint64_t> wayIds;
std::set<uint64_t> wayIds;

for (auto i : osrm::irange<std::size_t>(0, raw_route.unpacked_path_segments.size()))
{
Expand All @@ -147,7 +148,7 @@ class WayIdPlugin final : public BasePlugin
// Format answer.
osrm::json::Array json_array;
json_array.values.assign(wayIds.begin(), wayIds.end());
reply.values["way_ids"] = move(json_array);
reply.values["way_ids"] = std::move(json_array);

return 200;
}
Expand Down
Loading