summaryrefslogtreecommitdiff
path: root/platform/default/src/mbgl/storage/offline.cpp
blob: fd945c724f3f1c3dff1202428a4d4e7c6a6cf65f (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
161
162
163
#include <mbgl/storage/offline.hpp>
#include <mbgl/util/tileset.hpp>
#include <mbgl/util/projection.hpp>

#include <mapbox/geojson.hpp>
#include <mapbox/geojson/rapidjson.hpp>

#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>

#include <cmath>

namespace mbgl {

// OfflineTilePyramidRegionDefinition

OfflineTilePyramidRegionDefinition::OfflineTilePyramidRegionDefinition(
    std::string styleURL_, LatLngBounds bounds_, double minZoom_, double maxZoom_, float pixelRatio_, bool includeIdeographs_)
    : styleURL(std::move(styleURL_)),
      bounds(std::move(bounds_)),
      minZoom(minZoom_),
      maxZoom(maxZoom_),
      pixelRatio(pixelRatio_),
      includeIdeographs(includeIdeographs_) {
    if (minZoom < 0 || maxZoom < 0 || maxZoom < minZoom || pixelRatio < 0 ||
        !std::isfinite(minZoom) || std::isnan(maxZoom) || !std::isfinite(pixelRatio)) {
        throw std::invalid_argument("Invalid offline region definition");
    }
}


// OfflineGeometryRegionDefinition

OfflineGeometryRegionDefinition::OfflineGeometryRegionDefinition(std::string styleURL_, Geometry<double> geometry_, double minZoom_, double maxZoom_, float pixelRatio_, bool includeIdeographs_)
    : styleURL(styleURL_)
    , geometry(std::move(geometry_))
    , minZoom(minZoom_)
    , maxZoom(maxZoom_)
    , pixelRatio(pixelRatio_)
    , includeIdeographs(includeIdeographs_){
    if (minZoom < 0 || maxZoom < 0 || maxZoom < minZoom || pixelRatio < 0 ||
        !std::isfinite(minZoom) || std::isnan(maxZoom) || !std::isfinite(pixelRatio)) {
        throw std::invalid_argument("Invalid offline region definition");
    }
}

OfflineRegionDefinition decodeOfflineRegionDefinition(const std::string& region) {
    rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
    doc.Parse<0>(region.c_str());

    // validation

    auto hasValidBounds = [&] {
        return doc.HasMember("bounds") && doc["bounds"].IsArray() && doc["bounds"].Size() == 4
               && doc["bounds"][0].IsDouble() && doc["bounds"][1].IsDouble()
               && doc["bounds"][2].IsDouble() && doc["bounds"][3].IsDouble();
    };

    auto hasValidGeometry = [&] {
        return doc.HasMember("geometry") && doc["geometry"].IsObject();
    };

    if (doc.HasParseError()
        || !doc.HasMember("style_url") || !doc["style_url"].IsString()
        || !(hasValidBounds() || hasValidGeometry())
        || !doc.HasMember("min_zoom") || !doc["min_zoom"].IsDouble()
        || (doc.HasMember("max_zoom") && !doc["max_zoom"].IsDouble())
        || !doc.HasMember("pixel_ratio") || !doc["pixel_ratio"].IsDouble()
        || (doc.HasMember("include_ideographs") && !doc["include_ideographs"].IsBool())) {
        throw std::runtime_error("Malformed offline region definition");
    }

    // Common properties

    std::string styleURL { doc["style_url"].GetString(), doc["style_url"].GetStringLength() };
    double minZoom = doc["min_zoom"].GetDouble();
    double maxZoom = doc.HasMember("max_zoom") ? doc["max_zoom"].GetDouble() : INFINITY;
    float pixelRatio = doc["pixel_ratio"].GetDouble();
    bool includeIdeographs = doc.HasMember("include_ideographs") ? doc["include_ideographs"].GetBool() : true;
    
    if (doc.HasMember("bounds")) {
        return OfflineTilePyramidRegionDefinition{
                styleURL,
                LatLngBounds::hull(
                         LatLng(doc["bounds"][0].GetDouble(), doc["bounds"][1].GetDouble()),
                         LatLng(doc["bounds"][2].GetDouble(), doc["bounds"][3].GetDouble())),
                minZoom, maxZoom, pixelRatio, includeIdeographs };
    } else {
        return OfflineGeometryRegionDefinition{
                styleURL,
                mapbox::geojson::convert<Geometry<double>>(doc["geometry"].GetObject()),
                minZoom, maxZoom, pixelRatio, includeIdeographs };
    };

}

std::string encodeOfflineRegionDefinition(const OfflineRegionDefinition& region) {
    rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> doc;
    doc.SetObject();

    // Encode common properties
    region.match([&](auto& _region) {
        doc.AddMember("style_url", rapidjson::StringRef(_region.styleURL.data(), _region.styleURL.length()), doc.GetAllocator());
        doc.AddMember("min_zoom", _region.minZoom, doc.GetAllocator());
        if (std::isfinite(_region.maxZoom)) {
            doc.AddMember("max_zoom", _region.maxZoom, doc.GetAllocator());
        }

        doc.AddMember("pixel_ratio", _region.pixelRatio, doc.GetAllocator());
        doc.AddMember("include_ideographs", _region.includeIdeographs, doc.GetAllocator());
    });

    // Encode specific properties
    region.match(
            [&] (const OfflineTilePyramidRegionDefinition& _region) {
                rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator> bounds(rapidjson::kArrayType);
                bounds.PushBack(_region.bounds.south(), doc.GetAllocator());
                bounds.PushBack(_region.bounds.west(), doc.GetAllocator());
                bounds.PushBack(_region.bounds.north(), doc.GetAllocator());
                bounds.PushBack(_region.bounds.east(), doc.GetAllocator());
                doc.AddMember("bounds", bounds, doc.GetAllocator());

            },
            [&] (const OfflineGeometryRegionDefinition& _region) {
                doc.AddMember("geometry", mapbox::geojson::convert(_region.geometry, doc.GetAllocator()), doc.GetAllocator());

            }
    );

    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    doc.Accept(writer);

    return buffer.GetString();
}


// OfflineRegion

OfflineRegion::OfflineRegion(int64_t id_,
                             OfflineRegionDefinition definition_,
                             OfflineRegionMetadata metadata_)
    : id(id_),
      definition(std::move(definition_)),
      metadata(std::move(metadata_)) {
}

OfflineRegion::OfflineRegion(OfflineRegion&&) = default;
OfflineRegion::~OfflineRegion() = default;

const OfflineRegionDefinition& OfflineRegion::getDefinition() const {
    return definition;
}

const OfflineRegionMetadata& OfflineRegion::getMetadata() const {
    return metadata;
}

int64_t OfflineRegion::getID() const {
    return id;
}
} // namespace mbgl