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

utils.cpp « track_analyzing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b16cefe377b595dc781f3e05eccca989ec545512 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "track_analyzing/utils.hpp"

#include "track_analyzing/serialization.hpp"

#include "routing/segment.hpp"

#include "platform/platform.hpp"

#include "coding/file_name_utils.hpp"

#include "geometry/mercator.hpp"

#include <cstdint>

using namespace routing;
using namespace std;

namespace track_analyzing
{
double CalcSubtrackLength(MatchedTrack::const_iterator begin, MatchedTrack::const_iterator end,
                          Geometry & geometry)
{
  double length = 0.0;

  Segment prevSegment;
  for (auto it = begin; it != end; ++it)
  {
    MatchedTrackPoint const & point = *it;
    Segment const & segment = point.GetSegment();
    if (segment != prevSegment)
    {
      length += MercatorBounds::DistanceOnEarth(
          geometry.GetPoint(segment.GetRoadPoint(false /* front */)),
          geometry.GetPoint(segment.GetRoadPoint(true /* front */)));
      prevSegment = segment;
    }
  }

  return length;
}

double CalcTrackLength(MatchedTrack const & track, Geometry & geometry)
{
  return CalcSubtrackLength(track.begin(), track.end(), geometry);
}

double CalcSpeedKMpH(double meters, uint64_t secondsElapsed)
{
  CHECK_GREATER(secondsElapsed, 0, ());
  double constexpr kMPS2KMPH = 60.0 * 60.0 / 1000.0;
  return kMPS2KMPH * meters / static_cast<double>(secondsElapsed);
}

void ReadTracks(shared_ptr<NumMwmIds> numMwmIds, string const & filename,
                MwmToMatchedTracks & mwmToMatchedTracks)
{
  FileReader reader(filename);
  ReaderSource<FileReader> src(reader);
  MwmToMatchedTracksSerializer serializer(numMwmIds);
  serializer.Deserialize(mwmToMatchedTracks, src);
}

MatchedTrack const & GetMatchedTrack(MwmToMatchedTracks const & mwmToMatchedTracks,
                                     NumMwmIds const & numMwmIds, string const & mwmName,
                                     string const & user, size_t trackIdx)
{
  auto const countryFile = platform::CountryFile(mwmName);
  if (!numMwmIds.ContainsFile(countryFile))
    MYTHROW(MessageException, ("Invalid mwm name", mwmName));

  NumMwmId const numMwmId = numMwmIds.GetId(countryFile);

  auto mIt = mwmToMatchedTracks.find(numMwmId);
  if (mIt == mwmToMatchedTracks.cend())
    MYTHROW(MessageException, ("There are no tracks for mwm", mwmName));

  UserToMatchedTracks const & userToMatchedTracks = mIt->second;

  auto uIt = userToMatchedTracks.find(user);
  if (uIt == userToMatchedTracks.end())
    MYTHROW(MessageException, ("There is no user", user));

  vector<MatchedTrack> const & tracks = uIt->second;

  if (trackIdx >= tracks.size())
  {
    MYTHROW(MessageException, ("There is no track", trackIdx, "for user", user, ", she has",
                               tracks.size(), "tracks only"));
  }

  return tracks[trackIdx];
}

std::string GetCurrentVersionMwmFile(storage::Storage const & storage, std::string const & mwmName)
{
  return base::JoinPath(GetPlatform().WritableDir(), to_string(storage.GetCurrentDataVersion()),
                      mwmName + DATA_FILE_EXTENSION);
}

void ForEachTrackFile(
    std::string const & filepath, std::string const & extension,
    shared_ptr<routing::NumMwmIds> numMwmIds,
    std::function<void(std::string const & filename, MwmToMatchedTracks const &)> && toDo)
{
  Platform::EFileType fileType = Platform::FILE_TYPE_UNKNOWN;
  Platform::EError const result = Platform::GetFileType(filepath, fileType);

  if (result == Platform::ERR_FILE_DOES_NOT_EXIST)
    MYTHROW(MessageException, ("File doesn't exist", filepath));

  if (result != Platform::ERR_OK)
    MYTHROW(MessageException, ("Can't get file type for", filepath, "result:", result));

  if (fileType == Platform::FILE_TYPE_REGULAR)
  {
    MwmToMatchedTracks mwmToMatchedTracks;
    ReadTracks(numMwmIds, filepath, mwmToMatchedTracks);
    toDo(filepath, mwmToMatchedTracks);
    return;
  }

  if (fileType == Platform::FILE_TYPE_DIRECTORY)
  {
    Platform::FilesList filesList;
    Platform::GetFilesRecursively(filepath, filesList);

    for (string const & file : filesList)
    {
      if (base::GetFileExtension(file) != extension)
        continue;

      MwmToMatchedTracks mwmToMatchedTracks;
      ReadTracks(numMwmIds, file, mwmToMatchedTracks);
      toDo(file, mwmToMatchedTracks);
    }

    return;
  }

  MYTHROW(MessageException, (filepath, "is neither a regular file nor a directory't exist"));
}
}  // namespace track_analyzing