Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tile fallback #2120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/include/tangram/data/clientDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ class ClientDataSource : public TileSource {

void addPointFeature(Properties&& properties, LngLat coordinates);

void addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline);
uint64_t addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline);

void addPolygonFeature(Properties&& properties, PolygonBuilder
&& polygon);

void updatePolylineFeature(uint64_t id, const Coordinates& coordinates);

void updatePolylineFeature(uint64_t id, const Properties&& properties);

void removePolylineFeature(uint64_t id);

// Transform added feature data into tiles.
void generateTiles();

Expand Down
4 changes: 4 additions & 0 deletions core/include/tangram/data/tileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class TileSource : public std::enable_shared_from_this<TileSource> {
struct DataSource {
virtual ~DataSource() {}

virtual TileID getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) = 0;

virtual bool loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) = 0;

/* Stops any running I/O tasks pertaining to @_tile */
Expand Down Expand Up @@ -92,6 +94,8 @@ class TileSource : public std::enable_shared_from_this<TileSource> {
*/
virtual const char* mimeType() const;

TileID getFallbackTileID(const TileID& _tileID);

/* Fetches data for the map tile specified by @_tileID
*
* LoadTile starts an asynchronous I/O task to retrieve the data for a tile. When
Expand Down
69 changes: 66 additions & 3 deletions core/src/data/clientDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct ClientDataSource::Storage {
std::unique_ptr<geojsonvt::GeoJSONVT> tiles;
geometry::feature_collection<double> features;
std::vector<Properties> properties;
std::map<uint64_t, uint64_t> polylineIds;
};

struct ClientDataSource::PolylineBuilderData : mapbox::geometry::line_string<double> {
Expand Down Expand Up @@ -260,14 +261,21 @@ void ClientDataSource::addPointFeature(Properties&& properties, LngLat coordinat
m_store->properties.emplace_back(properties);
}

void ClientDataSource::addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline) {
uint64_t ClientDataSource::addPolylineFeature(Properties&& properties, PolylineBuilder&& polyline) {

std::lock_guard<std::mutex> lock(m_mutexStore);

uint64_t id = m_store->features.size();
uint64_t i = m_store->features.size();
uint64_t id = m_store->polylineIds.size();

properties.set("id", std::to_string(id));

auto geom = std::move(polyline.data);
m_store->features.emplace_back(*geom, id);
m_store->features.emplace_back(*geom, i);
m_store->properties.emplace_back(properties);
m_store->polylineIds.insert(std::pair<uint64_t, uint64_t>(id, i));

return id;
}

void ClientDataSource::addPolygonFeature(Properties&& properties, PolygonBuilder&& polygon) {
Expand All @@ -280,6 +288,61 @@ void ClientDataSource::addPolygonFeature(Properties&& properties, PolygonBuilder
m_store->properties.emplace_back(properties);
}

void ClientDataSource::updatePolylineFeature(uint64_t id, const Coordinates& coordinates) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

geometry::line_string<double> geom;
for (auto &p : coordinates) {
geom.emplace_back(p.longitude, p.latitude);
}

m_store->features[i] = mapbox::geometry::feature<double>(geom, i);
}
}

void ClientDataSource::updatePolylineFeature(uint64_t id, const Properties&& properties) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->properties.size()) {
uint64_t i = findIt->second;

for (int j = 0; j < properties.items().size(); ++j) {
m_store->properties[i].set(properties.items()[j].key,
properties.getString(properties.items()[j].key));
}
}
}

void ClientDataSource::removePolylineFeature(uint64_t id) {

std::lock_guard<std::mutex> lock(m_mutexStore);

std::map<uint64_t, uint64_t>::iterator findIt = m_store->polylineIds.find(id);

if (findIt != m_store->polylineIds.end() && findIt->second < m_store->features.size()) {
uint64_t i = findIt->second;

m_store->features.erase(std::next(m_store->features.begin(), i));
m_store->properties.erase(std::next(m_store->properties.begin(), i));

for (std::map<uint64_t, uint64_t>::iterator it = m_store->polylineIds.begin(); it != m_store->polylineIds.end(); ++it) {
if (it->second > i) {
it->second = it->second - 1;
m_store->features[it->second].id = it->second;
}
}
}
}

struct add_geometry {

static constexpr double extent = 4096.0;
Expand Down
96 changes: 80 additions & 16 deletions core/src/data/mbtilesDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ MBTilesDataSource::MBTilesDataSource(Platform& _platform, std::string _name, std
MBTilesDataSource::~MBTilesDataSource() {
}

TileID MBTilesDataSource::getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) {

TileID tileID(_tileID);

while (!hasTileData(tileID) && tileID.z > 0) {
tileID = tileID.getParent(_zoomBias);
}

if (tileID.z == _maxZoom) {
tileID = _tileID;
}
else {
tileID.s = _tileID.s;
}

return tileID;
}

bool MBTilesDataSource::loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) {

if (m_offlineMode) {
Expand Down Expand Up @@ -196,12 +214,12 @@ bool MBTilesDataSource::loadNextSource(std::shared_ptr<TileTask> _task, TileTask
if (m_cacheMode) {
m_worker->enqueue([this, _task](){

auto& task = static_cast<BinaryTileTask&>(*_task);
auto& task = static_cast<BinaryTileTask&>(*_task);

LOGW("store tile: %s, %d", _task->tileId().toString().c_str(), task.hasData());
LOGW("store tile: %s, %d", _task->tileId().toString().c_str(), task.hasData());

storeTileData(_task->tileId(), *task.rawTileData);
});
storeTileData(_task->tileId(), *task.rawTileData);
});
}

_cb.func(_task);
Expand Down Expand Up @@ -422,8 +440,50 @@ void MBTilesDataSource::initSchema(SQLite::Database& db, std::string _name, std:
}
}

bool MBTilesDataSource::hasTileData(const TileID& _tileId) {

TileID tileId = TileID(_tileId);
tileId.s = 0;

auto search = m_HasTileDataCache.find(tileId);

if (search != m_HasTileDataCache.end()) {
return search->second;
}

auto& stmt = m_queries->getTileData;
try {
// Google TMS to WMTS
// https://github.com/mapbox/node-mbtiles/blob/
// 4bbfaf991969ce01c31b95184c4f6d5485f717c3/lib/mbtiles.js#L149
int z = _tileId.z;
int y = (1 << z) - 1 - _tileId.y;

stmt.bind(1, z);
stmt.bind(2, _tileId.x);
stmt.bind(3, y);

if (stmt.executeStep()) {
stmt.reset();
m_HasTileDataCache[tileId] = true;
return true;
}

} catch (std::exception& e) {
LOGE("MBTiles SQLite get tile_data statement failed: %s", e.what());
}
try {
stmt.reset();
} catch (...) {}

m_HasTileDataCache[tileId] = false;
return false;
}

bool MBTilesDataSource::getTileData(const TileID& _tileId, std::vector<char>& _data) {

int largestLength = 0;

auto& stmt = m_queries->getTileData;
try {
// Google TMS to WMTS
Expand All @@ -441,20 +501,24 @@ bool MBTilesDataSource::getTileData(const TileID& _tileId, std::vector<char>& _d
const char* blob = (const char*) column.getBlob();
const int length = column.getBytes();

if ((m_schemaOptions.compression == Compression::undefined) ||
(m_schemaOptions.compression == Compression::deflate)) {

if (zlib::inflate(blob, length, _data) != 0) {
if (m_schemaOptions.compression == Compression::undefined) {
_data.resize(length);
memcpy(_data.data(), blob, length);
} else {
LOGW("Invalid deflate compression");
if (length > largestLength) {
if ((m_schemaOptions.compression == Compression::undefined) ||
(m_schemaOptions.compression == Compression::deflate)) {

if (zlib::inflate(blob, length, _data) != 0) {
if (m_schemaOptions.compression == Compression::undefined) {
_data.resize(length);
memcpy(_data.data(), blob, length);
} else {
LOGW("Invalid deflate compression");
}
}
} else {
_data.resize(length);
memcpy(_data.data(), blob, length);
}
} else {
_data.resize(length);
memcpy(_data.data(), blob, length);

largestLength = length;
}

stmt.reset();
Expand Down
7 changes: 7 additions & 0 deletions core/src/data/mbtilesDataSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include "data/tileSource.h"

namespace SQLite {
Expand All @@ -22,11 +23,14 @@ class MBTilesDataSource : public TileSource::DataSource {

~MBTilesDataSource();

TileID getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) override;

bool loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) override;

void clear() override {}

private:
bool hasTileData(const TileID& _tileId);
bool getTileData(const TileID& _tileId, std::vector<char>& _data);
void storeTileData(const TileID& _tileId, const std::vector<char>& _data);
bool loadNextSource(std::shared_ptr<TileTask> _task, TileTaskCb _cb);
Expand All @@ -52,6 +56,9 @@ class MBTilesDataSource : public TileSource::DataSource {
std::unique_ptr<MBTilesQueries> m_queries;
std::unique_ptr<AsyncWorker> m_worker;

// Cached has tile data
std::map<TileID, bool> m_HasTileDataCache;

// Platform reference
Platform& m_platform;

Expand Down
54 changes: 52 additions & 2 deletions core/src/data/memoryCacheDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ struct RawCache {

CacheMap m_cacheMap;
CacheList m_cacheList;
int m_usage = 0;
int m_maxUsage = 0;
size_t m_usage = 0;
size_t m_maxUsage = 0;

bool has(const TileID& _tileID) {

if (m_maxUsage <= 0) { return false; }

std::lock_guard<std::mutex> lock(m_mutex);

return m_cacheMap.find(_tileID) != m_cacheMap.end();
}

bool get(BinaryTileTask& _task) {

Expand Down Expand Up @@ -93,6 +102,10 @@ void MemoryCacheDataSource::setCacheSize(size_t _cacheSize) {
m_cache->m_maxUsage = _cacheSize;
}

bool MemoryCacheDataSource::hasCache(const TileID& _tileID) {
return m_cache->has(_tileID);
}

bool MemoryCacheDataSource::cacheGet(BinaryTileTask& _task) {
return m_cache->get(_task);
}
Expand All @@ -101,6 +114,43 @@ void MemoryCacheDataSource::cachePut(const TileID& _tileID, std::shared_ptr<std:
m_cache->put(_tileID, _rawDataRef);
}

TileID MemoryCacheDataSource::getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) {

TileID tileID(_tileID);
bool isCached = false;

while (!(isCached = hasCache(tileID)) && tileID.z > 0) {
tileID = tileID.getParent(_zoomBias);
}

if (tileID.z == _maxZoom) {
tileID = _tileID;
}
else {
tileID.s = _tileID.s;
}

if (next) {
TileID nextTileID = next->getFallbackTileID(_tileID, _maxZoom, _zoomBias);

if (nextTileID.z == _maxZoom) {
nextTileID = _tileID;
}
else {
nextTileID.s = _tileID.s;
}

if (isCached) {
return (tileID.z > nextTileID.z) ? tileID : nextTileID;
}
else {
return nextTileID;
}
}

return tileID;
}

bool MemoryCacheDataSource::loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) {

auto& task = static_cast<BinaryTileTask&>(*_task);
Expand Down
3 changes: 3 additions & 0 deletions core/src/data/memoryCacheDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class MemoryCacheDataSource : public TileSource::DataSource {
MemoryCacheDataSource();
~MemoryCacheDataSource();

TileID getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) override;

bool loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) override;

void clear() override;
Expand All @@ -20,6 +22,7 @@ class MemoryCacheDataSource : public TileSource::DataSource {
void setCacheSize(size_t _cacheSize);

private:
bool hasCache(const TileID& _tileID);
bool cacheGet(BinaryTileTask& _task);

void cachePut(const TileID& _tileID, std::shared_ptr<std::vector<char>> _rawDataRef);
Expand Down
2 changes: 2 additions & 0 deletions core/src/data/networkDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class NetworkDataSource : public TileSource::DataSource {
NetworkDataSource(Platform& _platform, const std::string& _urlTemplate,
std::vector<std::string>&& _urlSubdomains, bool _isTms);

TileID getFallbackTileID(const TileID& _tileID, int32_t _maxZoom, int32_t _zoomBias) override { return _tileID; }

bool loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) override;

void cancelLoadingTile(TileTask& _task) override;
Expand Down
Loading