aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/reverse_geocoder/core/reverse_geocoder.h
blob: c74eddb40e0e73a49fcec3dbfc9e54e296b2b36d (plain) (blame)
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
#pragma once

#include "common.h"
#include "geo_data/geo_data.h"
#include "geo_data/proxy.h"

#include <util/generic/noncopyable.h>
#include <util/generic/vector.h>

#include <functional>

namespace NReverseGeocoder {
    const TGeoId UNKNOWN_GEO_ID = static_cast<TGeoId>(-1);

    // NOTE: Be careful! It's work fine and fast on real world dataset.
    //       But in theory it's can spent O(n^2) memory (on real world dataset it's just 6n).
    //       Point in polygon test will be O(log n) always. Memory spent will be O(n) in future!
    class TReverseGeocoder: public TNonCopyable {
    public:
        using TDebug = TVector<TGeoId>;
        using TKvCallback = std::function<void(const char*, const char*)>;
        using TPolygonCallback = std::function<void(const TPolygon&)>;
        using TPartCallback = std::function<void(const TPart&, TNumber)>;

        TReverseGeocoder()
            : GeoDataProxy_()
        {
        }

        TReverseGeocoder(TReverseGeocoder&& g)
            : GeoDataProxy_()
        {
            DoSwap(GeoDataProxy_, g.GeoDataProxy_);
        }

        TReverseGeocoder& operator=(TReverseGeocoder&& g) {
            DoSwap(GeoDataProxy_, g.GeoDataProxy_);
            return *this;
        }

        explicit TReverseGeocoder(const char* path)
            : GeoDataProxy_(new TGeoDataMapProxy(path))
        {
        }

        explicit TReverseGeocoder(const IGeoData& geoData)
            : GeoDataProxy_(new TGeoDataWrapper(geoData))
        {
        }

        TReverseGeocoder(const char* data, size_t dataSize)
            : GeoDataProxy_(new TGeoDataRawProxy(data, dataSize))
        {
        }

        TGeoId Lookup(const TLocation& location, TDebug* debug = nullptr) const;

        TGeoId RawLookup(const TLocation& location, TDebug* debug = nullptr) const;

        bool EachKv(TGeoId regionId, TKvCallback callback) const;

        void EachPolygon(TPolygonCallback callback) const;

        void EachPart(const TPolygon& polygon, TPartCallback callback) const;

        const IGeoData& GeoData() const {
            return *GeoDataProxy_->GeoData();
        }

    private:
        TGeoDataProxyPtr GeoDataProxy_;
    };
}