summaryrefslogtreecommitdiff
path: root/include/mbgl/util/projection.hpp
blob: 391e37dd34caf854059abd80cf835eff33a8b989 (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
#ifndef MBGL_UTIL_PROJECTION
#define MBGL_UTIL_PROJECTION

#include <mbgl/util/constants.hpp>
#include <mbgl/util/geo.hpp>

#include <cmath>

namespace mbgl {

class Projection {

public:
    static inline MetersBounds getWorldBoundsMeters() {
        const double d = util::EARTH_RADIUS_M * M_PI;
        return { { -d, -d }, { d, d } };
    }

    static inline LatLngBounds getWorldBoundsLatLng() {
        MetersBounds bounds = getWorldBoundsMeters();
        return { latLngForProjectedMeters(bounds.sw), latLngForProjectedMeters(bounds.ne) };
    }

    static inline double getMetersPerPixelAtLatitude(const double lat, const double zoom) {
        const double mapPixelWidthAtZoom = std::pow(2.0, zoom) * util::tileSize;
        const double constrainedLatitude = ::fmin(::fmax(lat, -util::LATITUDE_MAX), util::LATITUDE_MAX);

        return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / mapPixelWidthAtZoom;
    }

    static inline const ProjectedMeters projectedMetersForLatLng(const LatLng latLng) {
        const double constrainedLatitude = ::fmin(::fmax(latLng.latitude, -util::LATITUDE_MAX), util::LATITUDE_MAX);

        const double m = 1 - 1e-15;
        const double f = ::fmin(::fmax(std::sin(util::DEG2RAD * constrainedLatitude), -m), m);

        const double easting  = util::EARTH_RADIUS_M * latLng.longitude * util::DEG2RAD;
        const double northing = 0.5 * util::EARTH_RADIUS_M * std::log((1 + f) / (1 - f));

        return ProjectedMeters(northing, easting);
    }

    static inline const LatLng latLngForProjectedMeters(const ProjectedMeters projectedMeters) {
        double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2)) * util::RAD2DEG;
        double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M;

        latitude = ::fmin(::fmax(latitude, -util::LATITUDE_MAX), util::LATITUDE_MAX);
        
        return LatLng(latitude, longitude);
    }
};

}

#endif