summaryrefslogtreecommitdiff
path: root/src/mbgl/util/tile_range.hpp
blob: eb7d2916c59400c4198ea9682c7b8784b99a37f4 (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
#pragma once

#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/projection.hpp>

namespace mbgl {

namespace util {

class TileRange {
public:
    Range<Point<double>> range;
    uint8_t z;

    // Compute the range of tiles covered by the bounds.
    static TileRange fromLatLngBounds(const LatLngBounds& bounds, uint8_t z) {
        auto swProj = Projection::project(bounds.southwest().wrapped(), z);
        auto ne = bounds.northeast();
        auto neProj = Projection::project(ne.longitude() > util::LONGITUDE_MAX ? ne.wrapped() : ne , z);
        auto minX = std::floor(swProj.x);
        auto maxX = std::ceil(neProj.x);
        auto minY = std::floor(neProj.y);
        auto maxY = std::ceil(swProj.y);
        return TileRange({ {minX, minY}, {maxX, maxY} }, z);
    }

    bool contains(const CanonicalTileID& tileID) {
        return z == tileID.z &&
        (range.min.x >= range.max.x ? //For wrapped bounds
            tileID.x >= range.min.x || tileID.x < range.max.x :
            tileID.x < range.max.x && tileID.x >= range.min.x) &&
        tileID.y < range.max.y &&
        tileID.y >= range.min.y;
    }

private:
    TileRange(Range<Point<double>> range_, uint8_t z_)
     : range(range_),
     z(z_) {
    }
    
};

} // namespace util
} // namespace mbgl