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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
#include <mbgl/source/source.hpp>
#include <mbgl/map/transform.hpp>
#include <mbgl/tile/tile.hpp>
#include <mbgl/tile/vector_tile.hpp>
#include <mbgl/annotation/annotation_tile.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/renderer/painter.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_update_parameters.hpp>
#include <mbgl/style/style_query_parameters.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/math/minmax.hpp>
#include <mbgl/math/clamp.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/tile_cover.hpp>
#include <mbgl/tile/vector_tile_data.hpp>
#include <mbgl/tile/raster_tile_data.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/gl/debugging.hpp>
#include <mbgl/algorithm/update_renderables_impl.hpp>
#include <mapbox/geojsonvt.hpp>
#include <mapbox/geojsonvt/convert.hpp>
#include <mapbox/geometry/envelope.hpp>
#include <rapidjson/error/en.h>
#include <algorithm>
#include <sstream>
namespace mbgl {
Source::Source(SourceType type_,
const std::string& id_,
const std::string& url_,
uint16_t tileSize_,
std::unique_ptr<SourceInfo>&& info_,
std::unique_ptr<mapbox::geojsonvt::GeoJSONVT>&& geojsonvt_)
: type(type_),
id(id_),
url(url_),
tileSize(tileSize_),
info(std::move(info_)),
geojsonvt(std::move(geojsonvt_)) {
}
Source::~Source() = default;
bool Source::isLoaded() const {
if (!loaded) return false;
for (const auto& pair : tileDataMap) {
if (pair.second->getState() != TileData::State::parsed) {
return false;
}
}
return true;
}
bool Source::isLoading() const {
return !loaded && req.operator bool();
}
void Source::load(FileSource& fileSource) {
if (url.empty()) {
// In case there is no URL set, we assume that we already have all of the data because the
// TileJSON was specified inline in the stylesheet.
loaded = true;
return;
}
if (req) {
// We don't have a SourceInfo object yet, but there's already a request underway to load
// the data.
return;
}
// URL may either be a TileJSON file, or a GeoJSON file.
req = fileSource.request(Resource::source(url), [this](Response res) {
if (res.error) {
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {
return;
} else if (res.noContent) {
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty source")));
} else {
bool reloadTiles = false;
if (type == SourceType::Vector || type == SourceType::Raster) {
std::unique_ptr<SourceInfo> newInfo;
// Create a new copy of the SourceInfo object that holds the base values we've parsed
// from the stylesheet. Then merge in the values parsed from the TileJSON we retrieved
// via the URL.
try {
newInfo = StyleParser::parseTileJSON(*res.data, url, type, tileSize);
} catch (...) {
observer->onSourceError(*this, std::current_exception());
return;
}
// Check whether previous information specifies different tile
if (info && info->tiles != newInfo->tiles) {
reloadTiles = true;
// Tile size changed: We need to recalculate the tiles we need to load because we
// might have to load tiles for a different zoom level
// This is done automatically when we trigger the onSourceLoaded observer below.
// Min/Max zoom changed: We need to recalculate what tiles to load, if we have tiles
// loaded that are outside the new zoom range
// This is done automatically when we trigger the onSourceLoaded observer below.
// Attribution changed: We need to notify the embedding application that this
// changed. See https://github.com/mapbox/mapbox-gl-native/issues/2723
// This is not yet implemented.
// Center/bounds changed: We're not using these values currently
}
info = std::move(newInfo);
} else if (type == SourceType::GeoJSON) {
std::unique_ptr<SourceInfo> newInfo = std::make_unique<SourceInfo>();
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
d.Parse<0>(res.data->c_str());
if (d.HasParseError()) {
std::stringstream message;
message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError());
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(message.str())));
return;
}
geojsonvt = StyleParser::parseGeoJSON(d);
reloadTiles = true;
newInfo->maxZoom = geojsonvt->options.maxZoom;
info = std::move(newInfo);
}
if (reloadTiles) {
// Tile information changed because we got new GeoJSON data, or a new tile URL.
tileDataMap.clear();
tiles.clear();
cache.clear();
}
loaded = true;
observer->onSourceLoaded(*this);
}
});
}
void Source::updateMatrices(const mat4 &projMatrix, const TransformState &transform) {
for (auto& pair : tiles) {
auto& tile = pair.second;
transform.matrixFor(tile.matrix, tile.id);
matrix::multiply(tile.matrix, projMatrix, tile.matrix);
}
}
void Source::finishRender(Painter &painter) {
for (auto& pair : tiles) {
auto& tile = pair.second;
painter.renderTileDebug(tile);
}
}
const std::map<UnwrappedTileID, Tile>& Source::getTiles() const {
return tiles;
}
std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledTileID,
const StyleUpdateParameters& parameters) {
std::unique_ptr<TileData> data = cache.get(overscaledTileID);
if (data) {
return data;
}
auto callback = std::bind(&Source::tileLoadingCallback, this, overscaledTileID,
std::placeholders::_1, true);
// If we don't find working tile data, we're just going to load it.
if (type == SourceType::Raster) {
data = std::make_unique<RasterTileData>(overscaledTileID, parameters.pixelRatio,
info->tiles.at(0), parameters.texturePool,
parameters.worker, parameters.fileSource, callback);
} else {
std::unique_ptr<GeometryTileMonitor> monitor;
if (type == SourceType::Vector) {
monitor = std::make_unique<VectorTileMonitor>(overscaledTileID, parameters.pixelRatio, info->tiles.at(0), parameters.fileSource);
} else if (type == SourceType::Annotations) {
monitor = std::make_unique<AnnotationTileMonitor>(overscaledTileID, parameters.annotationManager);
} else if (type == SourceType::GeoJSON) {
monitor = std::make_unique<GeoJSONTileMonitor>(geojsonvt.get(), overscaledTileID);
} else {
Log::Warning(Event::Style, "Source type '%s' is not implemented", SourceTypeClass(type).c_str());
return nullptr;
}
data = std::make_unique<VectorTileData>(overscaledTileID, std::move(monitor), id,
parameters.style, parameters.mode, callback);
}
return data;
}
TileData* Source::getTileData(const OverscaledTileID& overscaledTileID) const {
auto it = tileDataMap.find(overscaledTileID);
if (it != tileDataMap.end()) {
return it->second.get();
} else {
return nullptr;
}
}
bool Source::update(const StyleUpdateParameters& parameters) {
bool allTilesUpdated = true;
if (!loaded || parameters.animationTime <= updated) {
return allTilesUpdated;
}
// Determine the overzooming/underzooming amounts and required tiles.
int32_t overscaledZoom = util::coveringZoomLevel(parameters.transformState.getZoom(), type, tileSize);
int32_t dataTileZoom = overscaledZoom;
std::vector<UnwrappedTileID> idealTiles;
if (overscaledZoom >= info->minZoom) {
int32_t idealZoom = std::min<int32_t>(info->maxZoom, overscaledZoom);
// Make sure we're not reparsing overzoomed raster tiles.
if (type == SourceType::Raster) {
dataTileZoom = idealZoom;
}
idealTiles = util::tileCover(parameters.transformState, idealZoom);
}
// Stores a list of all the data tiles that we're definitely going to retain. There are two
// kinds of tiles we need: the ideal tiles determined by the tile cover. They may not yet be in
// use because they're still loading. In addition to that, we also need to retain all tiles that
// we're actively using, e.g. as a replacement for tile that aren't loaded yet.
std::set<OverscaledTileID> retain;
// Create all tiles that we definitely want to load
for (const auto& unwrappedTileID : idealTiles) {
const OverscaledTileID dataTileID(dataTileZoom, unwrappedTileID.canonical);
retain.emplace(dataTileID);
auto it = tileDataMap.find(dataTileID);
if (it == tileDataMap.end()) {
if (auto data = createTile(dataTileID, parameters)) {
it = tileDataMap.emplace(dataTileID, std::move(data)).first;
}
}
}
tiles = algorithm::updateRenderables<Tile>(tileDataMap, idealTiles, *info, overscaledZoom);
for (auto& pair : tiles) {
retain.emplace(pair.second.data.id);
}
if (type != SourceType::Raster && type != SourceType::Annotations && cache.getSize() == 0) {
size_t conservativeCacheSize =
((float)parameters.transformState.getWidth() / util::tileSize) *
((float)parameters.transformState.getHeight() / util::tileSize) *
(parameters.transformState.getMaxZoom() - parameters.transformState.getMinZoom() + 1) *
0.5;
cache.setSize(conservativeCacheSize);
}
// Remove stale data tiles from the active set of tiles.
// This goes through the (sorted!) tileDataMap and retain set in lockstep and removes items from
// tileDataMap that don't have the corresponding key in the retain set.
auto dataIt = tileDataMap.begin();
auto retainIt = retain.begin();
while (dataIt != tileDataMap.end()) {
if (retainIt == retain.end() || dataIt->first < *retainIt) {
cache.add(dataIt->first, std::move(dataIt->second));
tileDataMap.erase(dataIt++);
} else {
if (!(*retainIt < dataIt->first)) {
++dataIt;
}
++retainIt;
}
}
for (auto& pair : tileDataMap) {
const auto& dataTileID = pair.first;
auto tileData = pair.second.get();
if (parameters.shouldReparsePartialTiles &&
tileData->getState() == TileData::State::partial) {
auto callback = std::bind(&Source::tileLoadingCallback, this, dataTileID,
std::placeholders::_1, false);
if (!tileData->parsePending(callback)) {
allTilesUpdated = false;
}
} else {
tileData->redoPlacement({ parameters.transformState.getAngle(),
parameters.transformState.getPitch(),
parameters.debugOptions & MapDebugOptions::Collision },
[this]() { observer->onPlacementRedone(); });
}
}
updated = parameters.animationTime;
return allTilesUpdated;
}
static Point<int16_t> coordinateToTilePoint(const UnwrappedTileID& tileID, const Point<double>& p) {
auto zoomedCoord = TileCoordinate { p, 0 }.zoomTo(tileID.canonical.z);
return {
int16_t(util::clamp<int64_t>((zoomedCoord.p.x - tileID.canonical.x - tileID.wrap * std::pow(2, tileID.canonical.z)) * util::EXTENT,
std::numeric_limits<int16_t>::min(),
std::numeric_limits<int16_t>::max())),
int16_t(util::clamp<int64_t>((zoomedCoord.p.y - tileID.canonical.y) * util::EXTENT,
std::numeric_limits<int16_t>::min(),
std::numeric_limits<int16_t>::max()))
};
}
std::unordered_map<std::string, std::vector<Feature>> Source::queryRenderedFeatures(const StyleQueryParameters& parameters) const {
LineString<double> queryGeometry;
for (const auto& p : parameters.geometry) {
queryGeometry.push_back(TileCoordinate::fromScreenCoordinate(
parameters.transformState, 0, { p.x, parameters.transformState.getHeight() - p.y }).p);
}
mapbox::geometry::box<double> box = mapbox::geometry::envelope(queryGeometry);
std::unordered_map<std::string, std::vector<Feature>> result;
for (const auto& tilePtr : tiles) {
const Tile& tile = tilePtr.second;
Point<int16_t> tileSpaceBoundsMin = coordinateToTilePoint(tile.id, box.min);
Point<int16_t> tileSpaceBoundsMax = coordinateToTilePoint(tile.id, box.max);
if (tileSpaceBoundsMin.x >= util::EXTENT || tileSpaceBoundsMin.y >= util::EXTENT ||
tileSpaceBoundsMax.x < 0 || tileSpaceBoundsMax.y < 0) continue;
GeometryCoordinates tileSpaceQueryGeometry;
for (const auto& c : queryGeometry) {
tileSpaceQueryGeometry.push_back(coordinateToTilePoint(tile.id, c));
}
tile.data.queryRenderedFeatures(result,
{ tileSpaceQueryGeometry },
parameters.transformState.getAngle(),
util::tileSize * tile.data.id.overscaleFactor(),
std::pow(2, parameters.transformState.getZoom() - tile.data.id.overscaledZ),
parameters.layerIDs);
}
return result;
}
void Source::setCacheSize(size_t size) {
cache.setSize(size);
}
void Source::onLowMemory() {
cache.clear();
}
void Source::setObserver(Observer* observer_) {
observer = observer_;
}
void Source::tileLoadingCallback(const OverscaledTileID& tileID,
std::exception_ptr error,
bool isNewTile) {
auto it = tileDataMap.find(tileID);
if (it == tileDataMap.end()) {
return;
}
auto& tileData = it->second;
if (!tileData) {
return;
}
if (error) {
observer->onTileError(*this, tileID, error);
return;
}
tileData->redoPlacement([this]() {
observer->onPlacementRedone();
});
observer->onTileLoaded(*this, tileID, isNewTile);
}
void Source::dumpDebugLogs() const {
Log::Info(Event::General, "Source::id: %s", id.c_str());
Log::Info(Event::General, "Source::loaded: %d", loaded);
for (const auto& pair : tileDataMap) {
auto& tileData = pair.second;
tileData->dumpDebugLogs();
}
}
} // namespace mbgl
|