Welcome to mirror list, hosted at ThFree Co, Russian Federation.

online_cross_fetcher.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 21f09e4c168684ff2520191f0acb98839d14e2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "routing/online_cross_fetcher.hpp"

#include "platform/http_request.hpp"
#include "platform/platform.hpp"

#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include "3party/jansson/myjansson.hpp"

#include "geometry/mercator.hpp"

using namespace std;

namespace
{
inline string LatLonToURLArgs(ms::LatLon const & point)
{
  return strings::to_string(point.lat) + ','+ strings::to_string(point.lon);
}
}  // namespace

namespace routing
{
bool ParseResponse(string const & serverResponse, vector<m2::PointD> & outPoints)
{
  try
  {
    base::Json parser(serverResponse.c_str());

    json_t const * countries = json_object_get(parser.get(), "used_mwms");
    size_t const pointsCount = json_array_size(countries);
    for (size_t i = 0; i < pointsCount; ++i)
    {
      json_t * pointArray = json_array_get(countries, i);
      outPoints.push_back({json_number_value(json_array_get(pointArray, 0)),
                           json_number_value(json_array_get(pointArray, 1))});
    }
    return pointsCount > 0;
  }
  catch (base::Json::Exception const & exception)
  {
    LOG(LWARNING, ("Can't parse server response:", exception.what()));
    LOG(LWARNING, ("Response body:", serverResponse));
    return false;
  }
}

string GenerateOnlineRequest(string const & serverURL, ms::LatLon const & startPoint,
                             ms::LatLon const & finalPoint)
{
  return serverURL + "/mapsme?loc=" + LatLonToURLArgs(startPoint) + "&loc=" +
    LatLonToURLArgs(finalPoint);
}

OnlineCrossFetcher::OnlineCrossFetcher(TCountryFileFn const & countryFileFn,
                                       string const & serverURL, Checkpoints const & checkpoints)
  : m_countryFileFn(countryFileFn), m_serverURL(serverURL), m_checkpoints(checkpoints)
{
  CHECK(m_countryFileFn, ());
}

void OnlineCrossFetcher::Do()
{
  m_mwmPoints.clear();

  for (size_t i = 0; i < m_checkpoints.GetNumSubroutes(); ++i)
  {
    m2::PointD const & pointFrom = m_checkpoints.GetPoint(i);
    m2::PointD const & pointTo = m_checkpoints.GetPoint(i + 1);

    string const mwmFrom = m_countryFileFn(pointFrom);
    string const mwmTo = m_countryFileFn(pointTo);
    if (mwmFrom == mwmTo)
      continue;

    string const url = GenerateOnlineRequest(m_serverURL, MercatorBounds::ToLatLon(pointFrom),
                                             MercatorBounds::ToLatLon(pointTo));
    platform::HttpClient request(url);
    request.SetRawHeader("User-Agent", GetPlatform().GetAppUserAgent());
    LOG(LINFO, ("Check mwms by URL: ", url));

    if (request.RunHttpRequest() && request.ErrorCode() == 200 && !request.WasRedirected())
      ParseResponse(request.ServerResponse(), m_mwmPoints);
    else
      LOG(LWARNING, ("Can't get OSRM server response. Code: ", request.ErrorCode()));
  }
}
}  // namespace routing