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

borders_loader.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 018061e0381d6bca20941e65f984d271a6503cd4 (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
#include "borders_loader.hpp"
#include "borders_generator.hpp"

#include "../defines.hpp"

#include "../storage/country_polygon.hpp"

#include "../indexer/geometry_serialization.hpp"
#include "../indexer/scales.hpp"

#include "../geometry/simplification.hpp"
#include "../geometry/distance.hpp"

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

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

#include "../std/fstream.hpp"
#include "../std/vector.hpp"
#include "../std/bind.hpp"


#define BORDERS_DIR "borders/"
#define BORDERS_EXTENSION ".borders"
#define POLYGONS_FILE "polygons.lst"


namespace borders
{
  class PolygonLoader
  {
    CountryPolygons m_polygons;
    m2::RectD m_rect;

    string const & m_baseDir;
    CountriesContainerT & m_countries;

  public:
    PolygonLoader(string const & baseDir, CountriesContainerT & countries)
      : m_baseDir(baseDir), m_countries(countries) {}

    void operator() (string const & name)
    {
      if (m_polygons.m_name.empty())
        m_polygons.m_name = name;

      vector<m2::RegionD> borders;
      if (osm::LoadBorders(m_baseDir + BORDERS_DIR + name + BORDERS_EXTENSION, borders))
      {
        for (size_t i = 0; i < borders.size(); ++i)
        {
          m2::RectD const rect(borders[i].GetRect());
          m_rect.Add(rect);
          m_polygons.m_regions.Add(borders[i], rect);
        }
      }
    }

    void Finish()
    {
      if (!m_polygons.IsEmpty())
      {
        ASSERT_NOT_EQUAL ( m_rect, m2::RectD::GetEmptyRect(), () );
        m_countries.Add(m_polygons, m_rect);
      }

      m_polygons.Clear();
      m_rect.MakeEmpty();
    }
  };

  template <class ToDo>
  void ForEachCountry(string const & baseDir, ToDo & toDo)
  {
    ifstream stream((baseDir + POLYGONS_FILE).c_str());
    string line;

    while (stream.good())
    {
      std::getline(stream, line);
      if (line.empty())
        continue;

      // in polygons file every country is a separate string
      toDo(line);
      toDo.Finish();
    }
  }

  bool LoadCountriesList(string const & baseDir, CountriesContainerT & countries)
  {
    countries.Clear();

    LOG(LINFO, ("Loading countries."));

    PolygonLoader loader(baseDir, countries);
    ForEachCountry(baseDir, loader);

    LOG(LINFO, ("Countries loaded:", countries.GetSize()));

    return !countries.IsEmpty();
  }

  class PackedBordersGenerator
  {
    FilesContainerW m_writer;
    string const & m_baseDir;

    vector<storage::CountryDef> m_polys;

  public:
    PackedBordersGenerator(string const & baseDir)
      : m_writer(baseDir + PACKED_POLYGONS_FILE), m_baseDir(baseDir)
    {
    }

    void operator() (string const & name)
    {
      vector<m2::RegionD> borders;
      if (osm::LoadBorders(m_baseDir + BORDERS_DIR + name + BORDERS_EXTENSION, borders))
      {
        // use index in vector as tag
        FileWriter w = m_writer.GetWriter(strings::to_string(m_polys.size()));
        serial::CodingParams cp;

        uint32_t const count = static_cast<uint32_t>(borders.size());

        // calc rect
        m2::RectD rect;
        for (uint32_t i = 0; i < count; ++i)
          rect.Add(borders[i].GetRect());

        // store polygon info
        m_polys.push_back(storage::CountryDef(name, rect));

        // write polygons as paths
        WriteVarUint(w, count);
        for (uint32_t i = 0; i < count; ++i)
        {
          typedef vector<m2::PointD> VectorT;
          typedef mn::DistanceToLineSquare<m2::PointD> DistanceT;

          VectorT const & in = borders[i].Data();
          VectorT out;

          /// @todo Choose scale level for simplification.
          double const eps = my::sq(scales::GetEpsilonForSimplify(10));
          DistanceT dist;
          SimplifyNearOptimal(20, in.begin(), in.end(), eps, dist,
                              AccumulateSkipSmallTrg<DistanceT, m2::PointD>(dist, out, eps));

          serial::SaveOuterPath(out, cp, w);
        }
      }
    }

    void Finish() {}

    void WritePolygonsInfo()
    {
      FileWriter w = m_writer.GetWriter(PACKED_POLYGONS_INFO_TAG);
      rw::Write(w, m_polys);
    }
  };

  void GeneratePackedBorders(string const & baseDir)
  {
    PackedBordersGenerator generator(baseDir);
    ForEachCountry(baseDir, generator);
    generator.WritePolygonsInfo();
  }
}