Skip to content

Commit

Permalink
XYZ tile sources validated before use, giving useful diagnostic log i…
Browse files Browse the repository at this point in the history
…f image load fails
  • Loading branch information
Matthew Reid committed Jun 11, 2022
1 parent 60b6cfb commit f475a60
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ void addDefaultFactories(JsonTileSourceFactoryRegistry& registry)
xyzConfig.yOrigin = readOptionalOrDefault(json, "yTileOriginAtBottom", false) ? XyzTileSourceConfig::YOrigin::Bottom : XyzTileSourceConfig::YOrigin::Top;
xyzConfig.apiKey = apiKey;
xyzConfig.levelRange = readLevelRange(json);
return std::make_shared<XyzTileSource>(xyzConfig);
auto source = std::make_shared<XyzTileSource>(xyzConfig);
source->validate();
return source;
}));

registry.addFactory("bing", wrapAll(registry, [keys] (const nlohmann::json& json) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ MapboxElevationTileSource::MapboxElevationTileSource(const MapboxElevationTileSo
xyzConfig.yOrigin = XyzTileSourceConfig::YOrigin::Top;
xyzConfig.apiKey = config.apiKey;
m_source = std::make_unique<XyzTileSource>(xyzConfig);
m_source->validate();
}

osg::ref_ptr<osg::Image> MapboxElevationTileSource::createImage(const QuadTreeTileKey& key, std::function<bool()> cancelSupplier) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "XyzTileSource.h"

#include "SkyboltVis/OsgImageHelpers.h"

#include <osgDB/ReadFile>
#include <boost/algorithm/string/replace.hpp>
#include <boost/log/trivial.hpp>

using namespace skybolt;

Expand All @@ -23,12 +24,28 @@ XyzTileSource::XyzTileSource(const XyzTileSourceConfig& config) :
{
}

bool XyzTileSource::validate() const
{
// Validate the loader by loading level 0 image
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(toUrl(QuadTreeTileKey()));
if (!image)
{
BOOST_LOG_TRIVIAL(error) << "Could not load image from XyzTileSource with URL template '" << mUrlTemplate << ".";
}
return image != nullptr;
}

static int flipY(int y, int level)
{
return (1 << level) - y - 1;
}

osg::ref_ptr<osg::Image> XyzTileSource::createImage(const QuadTreeTileKey& key, std::function<bool()> cancelSupplier) const
{
return readImageWithoutWarnings(toUrl(key));
}

std::string XyzTileSource::toUrl(const skybolt::QuadTreeTileKey& key) const
{
int y = (mYOrigin == XyzTileSourceConfig::YOrigin::Top) ? key.y : flipY(key.y, key.level);

Expand All @@ -37,9 +54,7 @@ osg::ref_ptr<osg::Image> XyzTileSource::createImage(const QuadTreeTileKey& key,
boost::replace_all(url, "{y}", std::to_string(y));
boost::replace_all(url, "{z}", std::to_string(key.level));
boost::replace_all(url, "{key}", mApiKey);

osg::ref_ptr<osg::Image> image = readImageWithoutWarnings(url);
return image;
return url;
}

} // namespace vis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ class XyzTileSource : public TileSourceWithMinMaxLevel
public:
XyzTileSource(const XyzTileSourceConfig& config);

//! @return true if images can be loaded from URL
bool validate() const;

osg::ref_ptr<osg::Image> createImage(const skybolt::QuadTreeTileKey& key, std::function<bool()> cancelSupplier) const override;

private:
std::string toUrl(const skybolt::QuadTreeTileKey& key) const;

private:
std::string mUrlTemplate;
XyzTileSourceConfig::YOrigin mYOrigin;
Expand Down

0 comments on commit f475a60

Please sign in to comment.