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

[DatasSource] maxOverzoomLevel #507

Open
wants to merge 1 commit into
base: develop
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
1 change: 1 addition & 0 deletions all/modules/datasources/TileDataSource.i
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

%attribute(carto::TileDataSource, int, MinZoom, getMinZoom)
%attribute(carto::TileDataSource, int, MaxZoom, getMaxZoom)
%attribute(carto::TileDataSource, int, MaxOverzoomLevel, getMaxOverzoomLevel, setMaxOverzoomLevel)
%attributeval(carto::TileDataSource, carto::MapBounds, DataExtent, getDataExtent)
!attributestring_polymorphic(carto::TileDataSource, projections.Projection, Projection, getProjection)
%ignore carto::TileDataSource::OnChangeListener;
Expand Down
10 changes: 8 additions & 2 deletions all/native/datasources/MBTilesTileDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,21 @@ namespace carto {
}
return *_cachedDataExtent;
}

std::shared_ptr<TileData> MBTilesTileDataSource::loadTile(const MapTile& mapTile) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
Log::Infof("MBTilesTileDataSource::loadTile: Loading %s", mapTile.toString().c_str());
if (!_database) {
Log::Errorf("MBTilesTileDataSource::loadTile: Failed to load %s: Couldn't connect to the database", mapTile.toString().c_str());
return std::shared_ptr<TileData>();
}


if (getMaxOverzoomLevel() >= 0 && mapTile.getZoom() > getMaxZoomWithOverzoom()) {
// we explicitly return an empty tile to not draw overzoom
auto tileData = std::make_shared<TileData>(std::shared_ptr<BinaryData>());
tileData->setIsOverZoom(true);
return tileData;
}
try {
// Make the query and check for database error
sqlite3pp::query query(*_database, "SELECT tile_data FROM tiles WHERE zoom_level=:zoom AND tile_column=:x AND tile_row=:y");
Expand Down
2 changes: 1 addition & 1 deletion all/native/datasources/MBTilesTileDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace carto {
virtual MapBounds getDataExtent() const;

virtual std::shared_ptr<TileData> loadTile(const MapTile& mapTile);

private:
static std::unique_ptr<sqlite3pp::database> OpenDatabase(const std::string& path);

Expand Down
34 changes: 29 additions & 5 deletions all/native/datasources/TileDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ namespace carto {
return _maxZoom;
}

int TileDataSource::getMaxZoomWithOverzoom() const {
if (_maxOverzoomLevel >= 0) {
return getMaxZoom() + _maxOverzoomLevel;
}
return getMaxZoom();
}

int TileDataSource::getMaxOverzoomLevel() const {
return _maxOverzoomLevel;
}
bool TileDataSource::isMaxOverzoomLevelSet() const {
return _maxOverzoomLevel >= 0;
}

void TileDataSource::setMaxOverzoomLevel(int overzoomLevel) {
{
std::lock_guard<std::mutex> lock(_mutex);
_maxOverzoomLevel = overzoomLevel;
}
notifyTilesChanged(false);
}

MapBounds TileDataSource::getDataExtent() const {
return _projection->getBounds();
}
Expand All @@ -32,7 +54,7 @@ namespace carto {
void TileDataSource::notifyTilesChanged(bool removeTiles) {
std::vector<std::shared_ptr<OnChangeListener> > onChangeListeners;
{
std::lock_guard<std::mutex> lock(_onChangeListenersMutex);
std::lock_guard<std::mutex> lock(_mutex);
onChangeListeners = _onChangeListeners;
}
for (const std::shared_ptr<OnChangeListener>& listener : onChangeListeners) {
Expand All @@ -41,30 +63,32 @@ namespace carto {
}

void TileDataSource::registerOnChangeListener(const std::shared_ptr<OnChangeListener>& listener) {
std::lock_guard<std::mutex> lock(_onChangeListenersMutex);
std::lock_guard<std::mutex> lock(_mutex);
_onChangeListeners.push_back(listener);
}

void TileDataSource::unregisterOnChangeListener(const std::shared_ptr<OnChangeListener>& listener) {
std::lock_guard<std::mutex> lock(_onChangeListenersMutex);
std::lock_guard<std::mutex> lock(_mutex);
_onChangeListeners.erase(std::remove(_onChangeListeners.begin(), _onChangeListeners.end(), listener), _onChangeListeners.end());
}

TileDataSource::TileDataSource() :
_minZoom(0),
_maxZoom(Const::MAX_SUPPORTED_ZOOM_LEVEL),
_maxOverzoomLevel(-1),
_projection(std::make_shared<EPSG3857>()),
_onChangeListeners(),
_onChangeListenersMutex()
_mutex()
{
}

TileDataSource::TileDataSource(int minZoom, int maxZoom) :
_minZoom(std::max(0, minZoom)),
_maxZoom(std::min(static_cast<int>(Const::MAX_SUPPORTED_ZOOM_LEVEL), maxZoom)),
_maxOverzoomLevel(-1),
_projection(std::make_shared<EPSG3857>()),
_onChangeListeners(),
_onChangeListenersMutex()
_mutex()
{
}

Expand Down
27 changes: 26 additions & 1 deletion all/native/datasources/TileDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ namespace carto {
*/
virtual int getMaxZoom() const;


/**
* Returns the maximum zoom level supported by this data source + getMaxOverzoomLevel if >= 0.
* @return The maximum zoom level supported (exclusive).
*/
virtual int getMaxZoomWithOverzoom() const;

/**
* Sets the maximum overzoom level for this datasource.
* If a tile for the given zoom level Z is not available, SDK will try to use tiles with zoom levels Z-1, ..., Z-MaxOverzoomLevel.
* The default is -1 (disabled).
* @param overzoomLevel The new maximum overzoom value.
*/
void setMaxOverzoomLevel(int overzoomLevel);

/**
* Gets the current maximum overzoom level for this datasource.
* Over it the datasource will not be "drawn"
* @return The current maximum overzoom level for this datasource.
*/
int getMaxOverzoomLevel() const;

bool isMaxOverzoomLevelSet() const;

/**
* Returns the extent of the tiles in this data source.
* The bounds are in coordinate system of the projection of the data source.
Expand Down Expand Up @@ -117,11 +141,12 @@ namespace carto {

std::atomic<int> _minZoom;
std::atomic<int> _maxZoom;
std::atomic<int> _maxOverzoomLevel;
const std::shared_ptr<Projection> _projection;

private:
std::vector<std::shared_ptr<OnChangeListener> > _onChangeListeners;
mutable std::mutex _onChangeListenersMutex;
mutable std::mutex _mutex;
};

}
Expand Down
12 changes: 11 additions & 1 deletion all/native/datasources/components/TileData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace carto {

TileData::TileData(const std::shared_ptr<BinaryData>& data) :
_data(data), _expirationTime(), _replaceWithParent(false), _mutex()
_data(data), _expirationTime(), _replaceWithParent(false), _overzoom(false), _mutex()
{
}

Expand Down Expand Up @@ -42,6 +42,16 @@ namespace carto {
_replaceWithParent = flag;
}

bool TileData::isOverZoom() const {
std::lock_guard<std::mutex> lock(_mutex);
return _overzoom;
}

void TileData::setIsOverZoom(bool flag) {
std::lock_guard<std::mutex> lock(_mutex);
_overzoom = flag;
}

const std::shared_ptr<BinaryData>& TileData::getData() const {
return _data;
}
Expand Down
12 changes: 12 additions & 0 deletions all/native/datasources/components/TileData.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ namespace carto {
* @param flag True when the tile should be replaced with the parent, false otherwise.
*/
void setReplaceWithParent(bool flag);

/**
* Returns true if the tile data source marked this as over zoom.
* @return True if the tile should not be drawn. False otherwise.
*/
bool isOverZoom() const;
/**
* Set the parent overzoom flag.
* @return True if the tile should not be drawn. False otherwise.
*/
void setIsOverZoom(bool flag);

/**
* Returns tile data as binary data.
Expand All @@ -59,6 +70,7 @@ namespace carto {
const std::shared_ptr<BinaryData> _data;
std::shared_ptr<std::chrono::steady_clock::time_point> _expirationTime;
bool _replaceWithParent;
bool _overzoom;
mutable std::mutex _mutex;
};

Expand Down
5 changes: 5 additions & 0 deletions all/native/layers/RasterTileLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ namespace carto {
if (tileData->isReplaceWithParent()) {
continue;
}
if(tileData->isOverZoom()) {
// we need to invalidate cache tiles to make sure we dont draw over
layer->_preloadingCache.remove(_tileId);
layer->_visibleCache.remove(_tileId);
}

if (isCanceled()) {
break;
Expand Down
5 changes: 5 additions & 0 deletions all/native/layers/VectorTileLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ namespace carto {
if (tileData->isReplaceWithParent()) {
continue;
}
if(tileData->isOverZoom()) {
// we need to invalidate cache tiles to make sure we dont draw over
layer->_preloadingCache.remove(_tileId);
layer->_visibleCache.remove(_tileId);
}

if (isCanceled()) {
break;
Expand Down