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

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

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

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

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

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

#include "generator/std/fstream.hpp"

#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 LoadPolygons(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;
  }
}