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: f109d046fdc1f2705d9142d652e0f31f6a23e52d (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
#include "generator/borders_loader.hpp"
#include "generator/borders_generator.hpp"

#include "defines.hpp"

#include "platform/platform.hpp"

#include "storage/country_polygon.hpp"

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

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

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

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

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


namespace borders
{

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

  CountriesContainerT & m_countries;

public:
  PolygonLoader(CountriesContainerT & 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))
    {
      my::GetNameWithoutExt(file);
      toDo(file, borders);
      toDo.Finish();
    }
  }
}

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

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

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

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

  return !countries.IsEmpty();
}

class PackedBordersGenerator
{
  FilesContainerW m_writer;

  vector<storage::CountryDef> m_polys;

public:
  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::CodingParams 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)
    {
      typedef vector<m2::PointD> VectorT;
      typedef m2::DistanceToLineSquare<m2::PointD> DistanceT;

      VectorT const & in = border.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();
}

} // namespace borders