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

kml_parser.cpp « indexer_tool « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4c92bd868a176801e41c4cbae1def9ad39c25b8 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "kml_parser.hpp"
#include "feature_sorter.hpp"

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

#include "../../coding/parse_xml.hpp"
#include "../../coding/file_reader.hpp"

#include "../../geometry/rect2d.hpp"
#include "../../geometry/cellid.hpp"

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

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

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

#define MIN_SIMPLIFIED_POINTS_COUNT 4

namespace feature
{
  typedef vector<m2::PointD> points_t;
  void TesselateInterior(points_t const & bound, list<points_t> const & holes,
                        points_t & triangles);
}

namespace kml
{
  typedef vector<Region> PolygonsContainerT;

  class KmlParser
  {
    vector<string> m_tags;
    /// buffer for text with points
    string m_data;
    string m_name;

    PolygonsContainerT & m_country;
    int m_level;

  public:
    KmlParser(PolygonsContainerT & country, int level);

    bool Push(string const & element);
    void Pop(string const & element);
    void AddAttr(string const &, string const &) {}
    void CharData(string const & data);
  };

  KmlParser::KmlParser(PolygonsContainerT & country, int level)
    : m_country(country), m_level(level)
  {
  }

  bool KmlParser::Push(string const & element)
  {
    m_tags.push_back(element);

    return true;
  }

  template <class PointsContainerT>
  class PointsCollector
  {
    PointsContainerT & m_container;

  public:
    PointsCollector(PointsContainerT & container) : m_container(container)
    {
    }

    void operator()(string const & latLon)
    {
      size_t const firstCommaPos = latLon.find(',');
      CHECK(firstCommaPos != string::npos, ("invalid latlon", latLon));
      string const lonStr = latLon.substr(0, firstCommaPos);
      double lon;
      CHECK(utils::to_double(lonStr, lon), ("invalid lon", lonStr));
      size_t const secondCommaPos = latLon.find(',', firstCommaPos + 1);
      string latStr;
      if (secondCommaPos == string::npos)
        latStr = latLon.substr(firstCommaPos + 1);
      else
        latStr = latLon.substr(firstCommaPos + 1, secondCommaPos - firstCommaPos - 1);
      double lat;
      CHECK(utils::to_double(latStr, lat), ("invalid lon", latStr));
      // to mercator
      m2::PointD const mercPoint(MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat));
      m_container.push_back(mercPoint);
    }
  };

  class AreaFeature : public FeatureType
  {
  public:
    template <class IterT>
    AreaFeature(IterT beg, IterT end)
    {
      // manually fill bordering geometry points
      m_bPointsParsed = true;
      for (IterT it = beg; it != end; ++it)
      {
        m_Points.push_back(*it);
        m_LimitRect.Add(*it);
      }

      // manually fill triangles points
      m_bTrianglesParsed = true;
      list<feature::points_t> const holes;
      feature::points_t points(beg, end);
      feature::points_t triangles;
      feature::TesselateInterior(points, holes, triangles);
      CHECK(!triangles.empty(), ("Tesselation unsuccessfull?"));
      for (size_t i = 0; i < triangles.size(); ++i)
        m_Triangles.push_back(triangles[i]);
    }
  };
  void KmlParser::Pop(string const & element)
  {
    if (element == "Placemark")
    {
    }
    else if (element == "coordinates")
    {
      size_t const size = m_tags.size();
      CHECK(m_tags.size() > 3, ());
      CHECK(m_tags[size - 2] == "LinearRing", ());

      if (m_tags[size - 3] == "outerBoundaryIs")
      {
        // first, collect points in Mercator
        typedef vector<m2::PointD> MercPointsContainerT;
        MercPointsContainerT points;
        PointsCollector<MercPointsContainerT> collector(points);
        utils::TokenizeString(m_data, " \n\r\a", collector);
        size_t const numPoints = points.size();
        if (numPoints > 3 && points[numPoints - 1] == points[0])
        {
//          // create feature for country's polygon
//          AreaFeature ft(points.begin(), points.end());
//          // get polygon covering (cellids)
//          vector<int64_t> ids;
//          ids = covering::CoverFeature(ft, -1);
//          // debug output
//          set<int64_t> ids8;
//          for (size_t i = 0; i < ids.size(); ++i)
//          {
//            int64_t a = ids[i] >> (2 * 11);
//            if (ids8.insert(a).second)
//              LOG(LINFO, (RectId::FromInt64(a).ToString()));
//          }
//          LOG(LINFO, ("Total cellids:", ids8.size()));
          // second, simplify points if necessary
          if (m_level > 0)
          {
            MercPointsContainerT simplifiedPoints;
            feature::SimplifyPoints(points, simplifiedPoints, m_level);
            if (simplifiedPoints.size() > MIN_SIMPLIFIED_POINTS_COUNT)
            {
              // LOG_SHORT(LINFO, (m_name, numPoints, "simplified to ", simplifiedPoints.size()));
              points.swap(simplifiedPoints);
            }
            else
            {
              // LOG_SHORT(LINFO, (m_name, numPoints, "NOT simplified"));
            }
          }

          // remove last point which is equal to first
          // it's not needed for Region::Contains
          points.pop_back();

          m_country.push_back(Region());
          for (MercPointsContainerT::iterator it = points.begin(); it != points.end(); ++it)
            m_country.back().AddPoint(*it);
        }
        else
        {
          LOG(LWARNING, ("Invalid region for country"/*, m_country.m_name*/));
        }
      }
      else if (m_tags[size - 3] == "innerBoundaryIs")
      { // currently we're ignoring holes
      }
      else
      {
        CHECK(false, ("Unsupported tag", m_tags[size - 3]));
      }

      m_data.clear();
    }
    else if (element == "Polygon")
    {
    }

    m_tags.pop_back();
  }

  void KmlParser::CharData(string const & data)
  {
    size_t const size = m_tags.size();

    if (size > 1 && m_tags[size - 1] == "name" && m_tags[size - 2] == "Placemark")
    {
      m_name = data;
    }
    else if (size > 4 && m_tags[size - 1] == "coordinates"
        && m_tags[size - 2] == "LinearRing" && m_tags[size - 4] == "Polygon")
    {
      // text block can be really huge
      m_data.append(data);
    }
  }

  bool LoadPolygonsFromKml(string const & kmlFile, PolygonsContainerT & country, int level)
  {
    KmlParser parser(country, level);
    try
    {
      FileReader file(kmlFile);
      ReaderSource<FileReader> source(file);
      bool const bRes = ParseXML(source, parser, true);
      return bRes;
    }
    catch (std::exception const &)
    {
    }
    return false;
  }

  class PolygonLoader
  {
    string m_baseDir;
    int m_level;

    CountryPolygons & m_polygons;
    m2::RectD & m_rect;

  public:
    PolygonLoader(string const & basePolygonsDir, int level, CountryPolygons & polygons, m2::RectD & rect)
      : m_baseDir(basePolygonsDir), m_level(level), m_polygons(polygons), m_rect(rect)
    {
    }

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

      PolygonsContainerT current;
      if (LoadPolygonsFromKml(m_baseDir + BORDERS_DIR + name + BORDERS_EXTENSION, current, m_level))
      {
        for (size_t i = 0; i < current.size(); ++i)
        {
          m2::RectD const rect(current[i].GetRect());
          m_rect.Add(rect);
          m_polygons.m_regions.Add(current[i], rect);
        }
      }
    }
  };

  bool LoadCountriesList(string const & baseDir, CountriesContainerT & countries,
                         int simplifyCountriesLevel)
  {
    if (simplifyCountriesLevel > 0)
    {
      LOG_SHORT(LINFO, ("Simplificator level for country polygons:", simplifyCountriesLevel));
    }

    countries.Clear();
    ifstream stream((baseDir + POLYGONS_FILE).c_str());
    string line;
    LOG(LINFO, ("Loading countries."));
    while (stream.good())
    {
      std::getline(stream, line);
      if (line.empty())
        continue;

      CountryPolygons country;
      m2::RectD rect;

      PolygonLoader loader(baseDir, simplifyCountriesLevel, country, rect);
      utils::TokenizeString(line, "|", loader);
      if (!country.m_regions.IsEmpty())
        countries.Add(country, rect);
    }
    LOG(LINFO, ("Countries loaded:", countries.GetSize()));
    return !countries.IsEmpty();
  }
}