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: 283040081bb83814f613d3c31d851c652332b48e (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "generator/borders_loader.hpp"

#include "generator/borders_generator.hpp"

#include "defines.hpp"

#include "platform/platform.hpp"

#include "storage/country_polygon.hpp"

#include "indexer/scales.hpp"

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

#include "geometry/mercator.hpp"
#include "geometry/parametrized_segment.hpp"
#include "geometry/simplification.hpp"

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

#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

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

  CountriesContainer & m_countries;

public:
  PolygonLoader(CountriesContainer & countries) : m_countries(countries) {}

  void operator()(string const & name, vector<m2::RegionD> const & borders)
  {
    if (m_polygons.m_name.empty())
      m_polygons.m_name = name;

    for (m2::RegionD const & border : borders)
    {
      m2::RectD const rect(border.GetRect());
      m_rect.Add(rect);
      m_polygons.m_regions.Add(border, 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)
{
  string const bordersDir = baseDir + BORDERS_DIR;
  CHECK(Platform::IsFileExistsByFullPath(bordersDir),
        ("Cannot read borders directory", bordersDir));

  Platform::FilesList files;
  Platform::GetFilesByExt(bordersDir, BORDERS_EXTENSION, files);
  for (string file : files)
  {
    vector<m2::RegionD> borders;
    if (osm::LoadBorders(bordersDir + file, borders))
    {
      base::GetNameWithoutExt(file);
      toDo(file, borders);
      toDo.Finish();
    }
  }
}

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

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

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

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

  return !countries.IsEmpty();
}

class PackedBordersGenerator
{
public:
  explicit PackedBordersGenerator(string const & baseDir) : m_writer(baseDir + PACKED_POLYGONS_FILE)
  {
  }

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

    // calc rect
    m2::RectD rect;
    for (m2::RegionD const & border : borders)
      rect.Add(border.GetRect());

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

    // write polygons as paths
    WriteVarUint(w, borders.size());
    for (m2::RegionD const & border : borders)
    {
      vector<m2::PointD> const & in = border.Data();
      vector<m2::PointD> out;

      /// @todo Choose scale level for simplification.
      double const eps = pow(scales::GetEpsilonForSimplify(10), 2);
      m2::SquaredDistanceFromSegmentToPoint<m2::PointD> distFn;
      SimplifyNearOptimal(20, in.begin(), in.end(), eps, distFn,
                          AccumulateSkipSmallTrg<decltype(distFn), m2::PointD>(distFn, 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);
  }

private:
  FilesContainerW m_writer;

  vector<storage::CountryDef> m_polys;
};

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

void UnpackBorders(string const & baseDir, string const & targetDir)
{
  if (!Platform::IsFileExistsByFullPath(targetDir) && !Platform::MkDirChecked(targetDir))
    MYTHROW(FileSystemException, ("Unable to find or create directory", targetDir));

  vector<storage::CountryDef> countries;
  FilesContainerR reader(base::JoinPath(baseDir, PACKED_POLYGONS_FILE));
  ReaderSource<ModelReaderPtr> src(reader.GetReader(PACKED_POLYGONS_INFO_TAG));
  rw::Read(src, countries);

  for (size_t id = 0; id < countries.size(); id++)
  {
    ofstream poly(base::JoinPath(targetDir, countries[id].m_countryId + ".poly"));
    poly << countries[id].m_countryId << endl;
    src = reader.GetReader(strings::to_string(id));
    uint32_t const count = ReadVarUint<uint32_t>(src);
    for (size_t i = 0; i < count; ++i)
    {
      poly << i + 1 << endl;
      vector<m2::PointD> points;
      serial::LoadOuterPath(src, serial::GeometryCodingParams(), points);
      for (auto p : points)
      {
        ms::LatLon const ll = MercatorBounds::ToLatLon(p);
        poly << "    " << scientific << ll.lon << "    " << ll.lat << endl;
      }
      poly << "END" << endl;
    }
    poly << "END" << endl;
    poly.close();
  }
}

bool GetBordersRect(string const & baseDir, string const & country, m2::RectD & bordersRect)
{
  string const bordersFile = base::JoinPath(baseDir, BORDERS_DIR, country + BORDERS_EXTENSION);
  if (!Platform::IsFileExistsByFullPath(bordersFile))
  {
    LOG(LWARNING, ("File with borders does not exist:", bordersFile));
    return false;
  }

  vector<m2::RegionD> borders;
  CHECK(osm::LoadBorders(bordersFile, borders), ());
  bordersRect.MakeEmpty();
  for (auto const & border : borders)
    bordersRect.Add(border.GetRect());

  return true;
}
}  // namespace borders