aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/geo/point.h
blob: 70c91ab2ddcc189e5bcac41bec80a32f95ddb99a (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
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
#pragma once

#include <util/generic/string.h>
#include <util/stream/output.h>
#include <util/string/cast.h>
#include <util/generic/maybe.h>

#include <algorithm>
#include <cmath>

namespace NGeo {
    class TSize;

    class TGeoPoint {
    public:
        TGeoPoint(double lon, double lat) noexcept
            : Lon_(lon)
            , Lat_(lat)
        {
        }

        TGeoPoint() noexcept
            : Lon_(BadX)
            , Lat_(BadY)
        {
        }

        double Lon() const noexcept {
            return Lon_;
        }

        double Lat() const noexcept {
            return Lat_;
        }

        float Distance(const TGeoPoint& p) const noexcept;

        void swap(TGeoPoint& p) noexcept {
            std::swap(Lon_, p.Lon_);
            std::swap(Lat_, p.Lat_);
        }

        bool IsValid() const {
            return (Lon_ != BadX) && (Lat_ != BadY);
        }

        /// Returns true if the point represents either North or South Pole
        bool IsPole() const noexcept;

        /// Returns true if the point may be shown on the Yandex Map (fits into the valid range of latitudes)
        bool IsVisibleOnMap() const noexcept;

        bool operator!() const {
            return !IsValid();
        }

        TString ToCgiStr() const {
            return ToString();
        }

        TString ToString(const char* delimiter = ",") const {
            return TString::Join(::ToString(Lon_), delimiter, ::ToString(Lat_));
        }

        /**
         * \note Parsing functions work is safe way. They discard invalid points:
         * 1) on the Poles and 'beyond' the Poles;
         * 2) not belonging to the 'main' world and +/-1 world to the left or to the right.
         * If you need such cases, construct the TGeoPoint manually.
         */

        /// Throws TBadCastException on error
        static TGeoPoint Parse(TStringBuf s, TStringBuf delimiter = TStringBuf(","));

        /// Returns Nothing() on error
        static TMaybe<TGeoPoint> TryParse(TStringBuf s, TStringBuf delimiter = TStringBuf(","));

    private:
        double Lon_;
        double Lat_;

        static constexpr double BadX{361.};
        static constexpr double BadY{181.};
    };

    double GeodeticDistance(TGeoPoint p1, TGeoPoint p2);

    /**
     * \class TMercatorPoint
     *
     * Represents a point in EPSG:3395 projection
     * (WGS 84 / World Mercator)
     */
    class TMercatorPoint {
    public:
        friend class TMercatorWindow;
        friend TGeoPoint MercatorToLL(TMercatorPoint);

        /**
         * Constructs a point with the given coordinates.
         */
        constexpr TMercatorPoint(double x, double y) noexcept
            : X_{x}
            , Y_{y}
        {
        }

        /**
         * Constructs a point with two NaN coordinates.
         *
         * Should not be called directly.
         * If your `point` variable might be undefined,
         * declare it explicitly as TMaybe<TMercatorPoint>.
         */
        constexpr TMercatorPoint() noexcept
            : X_{std::numeric_limits<double>::quiet_NaN()}
            , Y_{std::numeric_limits<double>::quiet_NaN()}
        {
        }

        /**
         * Returns the X_ coordinate.
         *
         * The line X_ == 0 corresponds to the Prime meridian.
         */
        constexpr double X() const noexcept {
            return X_;
        }

        /**
         * Returns the Y_ coordinate.
         *
         * The line Y_ == 0 corresponds to the Equator.
         */
        constexpr double Y() const noexcept {
            return Y_;
        }

    private:
        bool IsDefined() const noexcept {
            return !std::isnan(X_) && !std::isnan(Y_);
        }

    private:
        double X_;
        double Y_;
    };

    /**
     * Operators
     */

    inline bool operator==(const TGeoPoint& p1, const TGeoPoint& p2) {
        return p1.Lon() == p2.Lon() && p1.Lat() == p2.Lat();
    }

    inline bool operator==(const TMercatorPoint& p1, const TMercatorPoint& p2) {
        return p1.X() == p2.X() && p1.Y() == p2.Y();
    }

    inline bool operator<(const TGeoPoint& p1, const TGeoPoint& p2) {
        if (p1.Lon() != p2.Lon()) {
            return p1.Lon() < p2.Lon();
        }
        return p1.Lat() < p2.Lat();
    }

    /**
     * Conversion
     */

    namespace WGS84 {
        /* Radius of reference ellipsoid, default to WGS 84 */
        const double R = 6378137.0;
    } // namespace WGS84

    using TPointLL = TGeoPoint;
    using TPointXY = TMercatorPoint;

    TGeoPoint MercatorToLL(TMercatorPoint);
    TMercatorPoint LLToMercator(TGeoPoint);

    /**
     * Input/output
     */

    TSize operator-(const TGeoPoint& p1, const TGeoPoint& p2);
} // namespace NGeo

template <>
inline void Out<NGeo::TGeoPoint>(IOutputStream& o, const NGeo::TGeoPoint& p) {
    o << '[' << p.Lon() << ", " << p.Lat() << ']';
}

template <>
inline void Out<NGeo::TMercatorPoint>(IOutputStream& o, const NGeo::TMercatorPoint& p) {
    o << '[' << p.X() << ", " << p.Y() << ']';
}