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

mapshot.cpp « mapshot - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96534c515aa1c265105a2cba96e0c27b9e0ada9e (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
#include "map/framework.hpp"

#include "geometry/mercator.hpp"

#include "base/string_utils.hpp"

#include "std/fstream.hpp"
#include "std/iostream.hpp"
#include "std/string.hpp"

#include "3party/gflags/src/gflags/gflags.h"

#pragma mark Define options
//----------------------------------------------------------------------------------------
DEFINE_bool(c, false, "Read places from stdin");
DEFINE_string(place, "", "Define place in format \"lat;lon;zoom\"");
DEFINE_string(outpath, "./", "Path for output files");
DEFINE_string(datapath, "", "Path to data directory");
DEFINE_string(mwmpath, "", "Path to mwm files");
DEFINE_int32(width, 480, "Resulting image width");
DEFINE_int32(height, 640, "Resulting image height");
//----------------------------------------------------------------------------------------

namespace
{
struct Place
{
  double lat;
  double lon;
  int zoom;
  int width;
  int height;
};

Place ParsePlace(string const & src)
{
  Place p;
  try
  {
    strings::SimpleTokenizer token(src, ";");
    p.lat = stod(*token);
    p.lon = stod(*(++token));
    p.zoom = static_cast<int>(stoi(*(++token)));
  }
  catch (exception & e)
  {
    cerr << "Error in [" << src << "]: " << e.what() << endl;
    exit(1);
  }
  return p;
}

void RenderPlace(Framework & framework, Place const & place, string const & filename)
{
  df::watch::FrameImage frame;
  df::watch::FrameSymbols sym;
  sym.m_showSearchResult = false;

  // If you are interested why, look at CPUDrawer::CalculateScreen.
  // It is almost UpperComfortScale but there is some magic involved.
  int constexpr kMagicBaseScale = 17;

  framework.DrawWatchFrame(MercatorBounds::FromLatLon(place.lat, place.lon), place.zoom - kMagicBaseScale,
                           place.width, place.height, sym, frame);

  ofstream file(filename.c_str());
  file.write(reinterpret_cast<char const *>(frame.m_data.data()), frame.m_data.size());
  file.close();
}

string FilenameSeq(string const & path)
{
  static size_t counter = 0;
  stringstream filename;
  filename << path << "mapshot" << setw(6) << setfill('0') << counter++ << ".png";
  return filename.str();
}
}  // namespace

int main(int argc, char * argv[])
{
  google::SetUsageMessage(
      "Generate screenshots of MAPS.ME maps in chosen places, specified by coordinates and zoom.");
  google::ParseCommandLineFlags(&argc, &argv, true);

  if (!FLAGS_c && FLAGS_place.empty())
  {
    cerr << "Either -c or -place must be set" << endl;
    return 1;
  }

  if (!FLAGS_datapath.empty())
    GetPlatform().SetResourceDir(FLAGS_datapath);

  if (!FLAGS_mwmpath.empty())
    GetPlatform().SetWritableDirForTests(FLAGS_mwmpath);

  try
  {
    Framework f;

    auto processPlace = [&](string const & place)
    {
      Place p = ParsePlace(place);
      p.width = FLAGS_width;
      p.height = FLAGS_height;
      string const & filename = FilenameSeq(FLAGS_outpath);
      RenderPlace(f, p, filename);
      cout << "Rendering " << place << " into " << filename << " is finished." << endl;
    };

    // This magic constant was determined in several attempts.
    // It is a scale level, basically, dpi factor. 1 means 90 or 96, it seems,
    // and with 1.1 the map looks subjectively better.
    f.InitWatchFrameRenderer(1.1 /* visualScale */);

    if (!FLAGS_place.empty())
      processPlace(FLAGS_place);

    if (FLAGS_c)
    {
      for (string line; getline(cin, line);)
        processPlace(line);
    }

    f.ReleaseWatchFrameRenderer();
    return 0;
  }
  catch (exception & e)
  {
    cerr << e.what() << endl;
  }
  return 1;
}