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

polygonizer.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33cf3e26f992d3d9b3c7c5e14a1882b5036af14f (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
#pragma once

#include "borders_loader.hpp"
#include "feature_builder.hpp"
#include "multiproducer_oneconsumer.hpp"

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

#include "../base/buffer_vector.hpp"

#include "../std/string.hpp"


namespace feature
{
  // Groups features according to country polygons
  template <class FeatureOutT> class Polygonizer
  {
    string m_prefix;
    string m_suffix;

    vector<FeatureOutT*> m_Buckets;
    vector<string> m_Names;
    borders::CountriesContainerT m_countries;

    MultiProducerOneConsumer m_impl;

  public:
    template <class TInfo>
    explicit Polygonizer(TInfo const & info)
      : m_prefix(info.m_datFilePrefix), m_suffix(info.m_datFileSuffix), m_impl(8)
    {
      if (info.m_splitByPolygons)
      {
        CHECK(borders::LoadCountriesList(info.m_datFilePrefix, m_countries),
            ("Error loading country polygons files"));
      }
      else
      {
        // Insert fake country polygon equal to whole world to
        // create only one output file which contains all features
        m_countries.Add(borders::CountryPolygons(),
                        m2::RectD(MercatorBounds::minX, MercatorBounds::minY,
                                  MercatorBounds::maxX, MercatorBounds::maxY));
      }
    }
    ~Polygonizer()
    {
      m_impl.Finish();
      for_each(m_Buckets.begin(), m_Buckets.end(), DeleteFunctor());
    }

    struct PointChecker
    {
      borders::RegionsContainerT const & m_regions;
      bool m_belongs;

      PointChecker(borders::RegionsContainerT const & regions)
        : m_regions(regions), m_belongs(false) {}

      bool operator()(m2::PointD const & pt)
      {
        m_regions.ForEachInRect(m2::RectD(pt, pt), bind<void>(ref(*this), _1, cref(pt)));
        return !m_belongs;
      }

      void operator() (borders::Region const & rgn, borders::Region::ValueT const & point)
      {
        if (!m_belongs)
          m_belongs = rgn.Contains(point);
      }
    };

    typedef borders::CountryPolygons PolygonsT;
    typedef buffer_vector<PolygonsT const *, 32> PolygonsVectorT;

    class InsertCountriesPtr
    {
      PolygonsVectorT & m_vec;

    public:
      InsertCountriesPtr(PolygonsVectorT & vec) : m_vec(vec) {}
      void operator() (PolygonsT const & c)
      {
        m_vec.push_back(&c);
      }
    };

    void operator () (FeatureBuilder1 const & fb)
    {
      PolygonsVectorT vec;
      m_countries.ForEachInRect(fb.GetLimitRect(), InsertCountriesPtr(vec));

      switch (vec.size())
      {
      case 0:
        break;
      case 1:
        EmitFeature(vec[0], fb);
        break;
      default:
        m_impl.RunTask(new PolygonizerTask(*this, vec, fb));
        break;
      }
    }

    void EmitFeature(PolygonsT const * country, FeatureBuilder1 const & fb)
    {
      if (country->m_index == -1)
      {
        m_Names.push_back(country->m_name);
        m_Buckets.push_back(new FeatureOutT(m_prefix + country->m_name + m_suffix));
        country->m_index = m_Buckets.size()-1;
      }

      (*(m_Buckets[country->m_index]))(fb);
    }

    inline vector<string> const & Names() const { return m_Names; }

  private:

    class PolygonizerTask : public MultiProducerOneConsumer::ITask
    {
    public:
      PolygonizerTask(Polygonizer & polygonizer,
                      PolygonsVectorT const & countries,
                      FeatureBuilder1 const & fb)
        : m_polygonizer(polygonizer), m_Countries(countries), m_FB(fb) {}

      // Override
      virtual void RunBase()
      {
        for (size_t i = 0; i < m_Countries.size(); ++i)
        {
          PointChecker doCheck(m_Countries[i]->m_regions);
          m_FB.ForEachGeometryPoint(doCheck);

          if (doCheck.m_belongs)
            Emit(const_cast<PolygonsT *>(m_Countries[i]));
        }
      }

      // Override
      virtual void EmitBase(void * p)
      {
        m_polygonizer.EmitFeature(reinterpret_cast<PolygonsT const *>(p), m_FB);
      }

    private:
      Polygonizer & m_polygonizer;

      /// @name Do copy of all input parameters.
      //@{
      PolygonsVectorT m_Countries;
      FeatureBuilder1 m_FB;
      //@}
    };
  };
}