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

grid_generator.cpp « indexer_tool « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f4f6c5df0501df0fc6a2fe9a9d98be80b3701eb (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "grid_generator.hpp"

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

#include "../../indexer/cell_id.hpp"
#include "../../indexer/mercator.hpp"

// tags used for grid drawing
#define GRIDKEY "mapswithme"
#define GRIDVALUE "grid"
#define CAPTIONKEY "place"
#define CAPTIONVALUE "country"

namespace grid
{
  static size_t const MIN_GRID_LEVEL = 1;
  static size_t const MAX_GRID_LEVEL = 10;

  template <class TCellId>
  string MercatorPointToCellIdString(double x, double y, size_t bucketingLevel)
  {
    TCellId id = CellIdConverter<MercatorBounds, TCellId>::ToCellId(x, y);
    return id.ToString().substr(0, bucketingLevel);
  }

  void GenerateGridToStdout(size_t bucketingLevel)
  {
    if (bucketingLevel < MIN_GRID_LEVEL || bucketingLevel > MAX_GRID_LEVEL)
    {
      LOG(LERROR, ("Bucketing level", bucketingLevel, "for grid is not within valid range [", MIN_GRID_LEVEL, "..", MAX_GRID_LEVEL, "]"));
      return;
    }

    size_t const COUNT = 2 << (bucketingLevel - 1);
    double const STEPX = (MercatorBounds::maxX - MercatorBounds::minX) / COUNT;
    double const STEPY = (MercatorBounds::maxY - MercatorBounds::minY) / COUNT;

    cout <<
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    "<osm version=\"0.6\" generator=\"MapsWithMe Indexer Tool\">\n"
    "  <bounds minlat=\"" << MercatorBounds::YToLat(MercatorBounds::minY) <<
        "\" minlon=\"" << MercatorBounds::XToLon(MercatorBounds::minX) <<
        "\" maxlat=\"" << MercatorBounds::YToLat(MercatorBounds::maxY) <<
        "\" maxlon=\"" << MercatorBounds::XToLon(MercatorBounds::maxX) << "\"/>\n";

    // generate nodes and ways
    size_t nodeID = 1;
    size_t wayID = 1;
    for (double y = MercatorBounds::minY; y <= MercatorBounds::maxY; y += STEPY)
    {
      size_t const firstID = nodeID;
      cout <<
      "  <node id=\"" << nodeID++ <<
          "\" lat=\"" << MercatorBounds::YToLat(y) <<
          "\" lon=\"" << MercatorBounds::XToLon(MercatorBounds::minX) <<
          "\"/>\n";
      size_t const secondID = nodeID;
      cout <<
      "  <node id=\"" << nodeID++ <<
          "\" lat=\"" << MercatorBounds::YToLat(y) <<
          "\" lon=\"" << MercatorBounds::XToLon(MercatorBounds::maxX) <<
          "\"/>\n";
      cout <<
      "  <way id=\"" << wayID++ << "\">\n"
      "    <nd ref=\"" << firstID << "\"/>\n"
      "    <nd ref=\"" << secondID << "\"/>\n"
      "    <tag k=\"" << GRIDKEY << "\" v=\"" << GRIDVALUE << "\"/>\n"
      "    <tag k=\"layer\" v=\"-5\"/>\n"
      "  </way>\n";
    }
    for (double x = MercatorBounds::minX; x <= MercatorBounds::maxX; x += STEPX)
    {
      size_t const firstID = nodeID;
      cout <<
      "  <node id=\"" << nodeID++ <<
          "\" lat=\"" << MercatorBounds::YToLat(MercatorBounds::minY) <<
          "\" lon=\"" << MercatorBounds::XToLon(x) <<
          "\"/>\n";
      size_t const secondID = nodeID;
      cout <<
      "  <node id=\"" << nodeID++ <<
          "\" lat=\"" << MercatorBounds::YToLat(MercatorBounds::maxY) <<
          "\" lon=\"" << MercatorBounds::XToLon(x) <<
          "\"/>\n";
      cout <<
      "  <way id=\"" << wayID++ << "\">\n"
      "    <nd ref=\"" << firstID << "\"/>\n"
      "    <nd ref=\"" << secondID << "\"/>\n"
      "    <tag k=\"" << GRIDKEY << "\" v=\"" << GRIDVALUE << "\"/>\n"
      "    <tag k=\"layer\" v=\"-5\"/>\n"
      "  </way>\n";
    }

    // generate nodes with captions
    for (size_t y = 0; y <= COUNT - 1; ++y)
    {
      for (size_t x = 0; x <= COUNT - 1; ++x)
      {
        double const mercY = MercatorBounds::minY + y * STEPY + STEPY / 2;
        double const mercX = MercatorBounds::minX + x * STEPX + STEPX / 2;
        string const title = MercatorPointToCellIdString<m2::CellId<MAX_GRID_LEVEL> >(mercX, mercY, bucketingLevel);
        cout <<
        "  <node id=\"" << nodeID++ <<
            "\" lat=\"" << MercatorBounds::YToLat(mercY) <<
            "\" lon=\"" << MercatorBounds::XToLon(mercX) <<
            "\">\n";
        cout <<
        "    <tag k=\"" << CAPTIONKEY << "\" v=\"" << CAPTIONVALUE << "\"/>\n";
        cout <<
        "    <tag k=\"name\" v=\"" << title << "\"/>\n";
        cout <<
        "  </node>\n";
      }
    }
    cout <<
    "</osm>\n";
  }

/*  void GenerateGridToStdout(size_t bucketingLevel)
  {
    if (bucketingLevel < MIN_GRID_LEVEL || bucketingLevel > MAX_GRID_LEVEL)
    {
      LOG(LERROR, ("Bucketing level", bucketingLevel, "for grid is not within valid range [", MIN_GRID_LEVEL, "..", MAX_GRID_LEVEL, "]"));
      return;
    }

    size_t const COUNT = 2 << (bucketingLevel - 1);
    double const STEPX = (MercatorBounds::maxX - MercatorBounds::minX) / COUNT;
    double const STEPY = (MercatorBounds::maxY - MercatorBounds::minY) / COUNT;

    cout <<
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    "<osm version=\"0.6\" generator=\"MapsWithMe Indexer Tool\">\n"
    "  <bounds minlat=\"" << MercatorBounds::YToLat(MercatorBounds::minY) <<
        "\" minlon=\"" << MercatorBounds::XToLon(MercatorBounds::minX) <<
        "\" maxlat=\"" << MercatorBounds::YToLat(MercatorBounds::maxY) <<
        "\" maxlon=\"" << MercatorBounds::XToLon(MercatorBounds::maxX) << "\"/>\n";

    // generate nodes
    size_t nodeID = 1;
    for (double y = MercatorBounds::minY; y <= MercatorBounds::maxY; y += STEPY)
    {
      for (double x = MercatorBounds::minX; x <= MercatorBounds::maxX; x += STEPX)
      {
        cout <<
        "  <node id=\"" << nodeID++ <<
            "\" lat=\"" << MercatorBounds::YToLat(y) <<
            "\" lon=\"" << MercatorBounds::XToLon(x) <<
            "\"/>\n";
      }
    }
    // generate squares and captions
    size_t wayID = 1;
    for (size_t y = 0; y <= COUNT - 1; ++y)
    {
      for (size_t x = 0; x <= COUNT - 1; ++x)
      {
        size_t const first = x + y * (COUNT + 1) + 1;
        size_t const second = first + 1;
        size_t const third = second + COUNT + 1;
        size_t const fourth = third - 1;
        string title = CellStringFromXYLevel(x, y, bucketingLevel);
        ++nodeID;
        cout <<
        "  <node id=\"" << nodeID <<
            "\" lat=\"" << MercatorBounds::YToLat(MercatorBounds::minY + y * STEPY + STEPY / 2) <<
            "\" lon=\"" << MercatorBounds::XToLon(MercatorBounds::minX + x * STEPX + STEPX / 2) <<
            "\">\n";
        cout <<
        "    <tag k=\"" << TAGKEY << "\" v=\"" << CAPTIONVALUE << "\"/>\n";
        cout <<
        "    <tag k=\"name\" v=\"" << title << "\"/>\n";
        cout <<
        "  </node>\n";

        cout <<
        "  <way id=\"" << wayID++  << "\">\n"
        "    <nd ref=\"" << first   << "\"/>\n"
        "    <nd ref=\"" << second << "\"/>\n"
        "    <nd ref=\"" << third  << "\"/>\n"
        "    <nd ref=\"" << fourth << "\"/>\n"
        "    <nd ref=\"" << first   << "\"/>\n"
        "    <tag k=\"name\" v=\"" << title << "\"/>\n"
        "    <tag k=\"" << TAGKEY << "\" v=\"" << GRIDVALUE << "\"/>\n"
        "    <tag k=\"layer\" v=\"-5\"/>\n"
        "  </way>\n";
      }
    }
    cout <<
    "</osm>\n";
  }*/
}