Implement and use factory pattern for TileProvider instantiation

This commit is contained in:
Erik Broberg
2016-06-29 13:47:11 -04:00
parent 5896d7eb71
commit 1d856151ed
10 changed files with 232 additions and 26 deletions
@@ -23,6 +23,7 @@
****************************************************************************************/
#include <modules/globebrowsing/tile/tileprovidermanager.h>
#include <modules/globebrowsing/tile/tileproviderfactory.h>
#include <ghoul/logging/logmanager.h>
@@ -100,17 +101,18 @@ namespace openspace {
texDict.getValue("Name", name);
texDict.getValue("FilePath", path);
std::string type = "LRUCaching"; // if type is unspecified
texDict.getValue("Type", type);
std::shared_ptr<TileProvider> tileProvider = TileProviderFactory::ref()->create(type, path, initData);
if (tileProvider == nullptr) {
continue;
}
bool enabled = false; // defaults to false if unspecified
texDict.getValue("Enabled", enabled);
std::shared_ptr<TileProvider> tileProvider;
try {
tileProvider = initProvider(path, initData);
}
catch (const ghoul::RuntimeError& e) {
LERROR(e.message);
continue;
}
dest.push_back({ name, tileProvider, enabled });
}
}
@@ -28,13 +28,13 @@
#include <modules/globebrowsing/tile/temporaltileprovider.h>
#include <modules/globebrowsing/tile/tileprovider.h>
#include <modules/globebrowsing/tile/layeredtextures.h>
#include <modules/globebrowsing/other/threadpool.h>
#include <ghoul/misc/dictionary.h>
#include <memory>
#include <vector>
#include <string>
@@ -25,6 +25,7 @@
#include <modules/globebrowsing/geometry/geodetic2.h>
#include <modules/globebrowsing/tile/temporaltileprovider.h>
#include <modules/globebrowsing/tile/tileproviderfactory.h>
#include <modules/globebrowsing/chunk/chunkindex.h>
@@ -151,7 +152,7 @@ namespace openspace {
}
std::shared_ptr<CachingTileProvider> TemporalTileProvider::getTileProvider(Time t) {
std::shared_ptr<TileProvider> TemporalTileProvider::getTileProvider(Time t) {
Time tCopy(t);
if (_timeQuantizer.quantize(tCopy)) {
TimeKey timekey = _timeFormat->stringify(tCopy);
@@ -167,31 +168,23 @@ namespace openspace {
}
std::shared_ptr<CachingTileProvider> TemporalTileProvider::getTileProvider(TimeKey timekey) {
std::shared_ptr<TileProvider> TemporalTileProvider::getTileProvider(TimeKey timekey) {
auto it = _tileProviderMap.find(timekey);
if (it != _tileProviderMap.end()) {
return it->second;
}
else {
auto tileProvider = initTileProvider(timekey);
_tileProviderMap[timekey] = tileProvider;
return tileProvider;
}
}
std::shared_ptr<CachingTileProvider> TemporalTileProvider::initTileProvider(TimeKey timekey) {
std::shared_ptr<TileProvider> TemporalTileProvider::initTileProvider(TimeKey timekey) {
std::string gdalDatasetXml = getGdalDatasetXML(timekey);
auto tileDataset = std::make_shared<TileDataset>(gdalDatasetXml,
_tileProviderInitData.minimumPixelSize, _tileProviderInitData.preprocessTiles);
auto threadPool = std::make_shared<ThreadPool>(_tileProviderInitData.threads);
auto tileReader = std::make_shared<AsyncTileDataProvider>(tileDataset, threadPool);
auto tileCache = std::make_shared<TileCache>(_tileProviderInitData.cacheSize);
auto tileProvider = std::make_shared<CachingTileProvider>(tileReader, tileCache,
_tileProviderInitData.framesUntilRequestQueueFlush);
return tileProvider;
return TileProviderFactory::ref()->create("LRUCaching", gdalDatasetXml, _tileProviderInitData);
}
std::string TemporalTileProvider::getGdalDatasetXML(Time t) {
@@ -131,8 +131,8 @@ namespace openspace {
typedef std::string TimeKey;
std::shared_ptr<CachingTileProvider> getTileProvider(Time t = Time::ref());
std::shared_ptr<CachingTileProvider> getTileProvider(TimeKey timekey);
std::shared_ptr<TileProvider> getTileProvider(Time t = Time::ref());
std::shared_ptr<TileProvider> getTileProvider(TimeKey timekey);
private:
@@ -143,7 +143,7 @@ namespace openspace {
std::string getGdalDatasetXML(TimeKey key);
std::shared_ptr<CachingTileProvider> initTileProvider(TimeKey timekey);
std::shared_ptr<TileProvider> initTileProvider(TimeKey timekey);
std::string consumeTemporalMetaData(const std::string &xml);
std::string getXMLValue(CPLXMLNode*, const std::string& key, const std::string& defaultVal);
@@ -155,7 +155,7 @@ namespace openspace {
const std::string _datasetFile;
std::string _gdalXmlTemplate;
std::unordered_map<TimeKey, std::shared_ptr<CachingTileProvider> > _tileProviderMap;
std::unordered_map<TimeKey, std::shared_ptr<TileProvider> > _tileProviderMap;
TileProviderInitData _tileProviderInitData;
std::shared_ptr<TileProvider> _currentTileProvider;
@@ -51,6 +51,44 @@ namespace openspace {
const Tile Tile::TileUnavailable = {nullptr, nullptr, Tile::Status::Unavailable };
SingleImagePrivoder::SingleImagePrivoder(const std::string& imagePath) {
auto texture = ghoul::io::TextureReader::ref().loadTexture(imagePath);
_tile = std::make_shared<Tile>();
_tile->texture = std::move(texture);
_tile->status = _tile->texture != nullptr ? Tile::Status::OK : Tile::Status::IOError;
_tile->preprocessData = nullptr;
}
Tile SingleImagePrivoder::getTile(const ChunkIndex& chunkIndex) {
return *_tile;
}
Tile::Status SingleImagePrivoder::getTileStatus(const ChunkIndex& index) {
return _tile->status;
}
TileDepthTransform SingleImagePrivoder::depthTransform() {
TileDepthTransform transform;
transform.depthOffset = 0.0f;
transform.depthScale = 1.0f;
return transform;
}
void SingleImagePrivoder::update() {
// nothing to be done
}
int SingleImagePrivoder::maxLevel() {
return 1337; // unlimited
}
CachingTileProvider::CachingTileProvider(std::shared_ptr<AsyncTileDataProvider> tileReader,
std::shared_ptr<TileCache> tileCache,
int framesUntilFlushRequestQueue)
+15
View File
@@ -78,6 +78,21 @@ namespace openspace {
typedef LRUCache<ChunkHashKey, Tile> TileCache;
class SingleImagePrivoder : public TileProvider {
public:
SingleImagePrivoder(const std::string& imagePath);
virtual ~SingleImagePrivoder() { }
virtual Tile getTile(const ChunkIndex& chunkIndex);
virtual Tile::Status getTileStatus(const ChunkIndex& index);
virtual TileDepthTransform depthTransform();
virtual void update();
virtual int maxLevel();
private:
std::shared_ptr<Tile> _tile;
};
/**
Provides tiles through GDAL datasets which can be defined with xml files
for example for wms.
@@ -0,0 +1,92 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2016 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/globebrowsing/tile/tileproviderfactory.h>
#include <ghoul/logging/logmanager.h>
#include "cpl_minixml.h"
namespace {
const std::string _loggerCat = "TileProviderFactory";
}
namespace openspace {
std::shared_ptr<TileProviderFactory> TileProviderFactory::_ref = nullptr;
TileProviderFactory::TileProviderFactory() {
initialize();
}
std::shared_ptr<TileProviderFactory> TileProviderFactory::ref() {
if (_ref == nullptr) {
// Need to explicitly use new here, since constructor is private
TileProviderFactory* ptr = new TileProviderFactory();
_ref = std::shared_ptr<TileProviderFactory>(ptr);
}
return _ref;
}
std::shared_ptr<TileProvider> TileProviderFactory::create(const std::string& type,
const std::string& desc, const TileProviderInitData& initData)
{
auto concreteFactoryIterator = _factoryMap.find(type);
if (concreteFactoryIterator == _factoryMap.end()) {
LERROR("Unknown type: " << type);
return nullptr;
}
return concreteFactoryIterator->second(desc, initData);
}
void TileProviderFactory::initialize() {
_factoryMap.insert({"LRUCaching", [](const std::string& desc, const TileProviderInitData& initData) {
auto tileDataset = std::make_shared<TileDataset>(desc, initData.minimumPixelSize, initData.preprocessTiles);
auto threadPool = std::make_shared<ThreadPool>(1);
auto tileReader = std::make_shared<AsyncTileDataProvider>(tileDataset, threadPool);
auto tileCache = std::make_shared<TileCache>(initData.cacheSize);
auto tileProvider = std::make_shared<CachingTileProvider>(tileReader, tileCache, initData.framesUntilRequestQueueFlush);
return tileProvider;
}});
_factoryMap.insert({ "Temporal", [](const std::string& file, const TileProviderInitData& initData) {
CPLXMLNode * node = CPLParseXMLFile(file.c_str());
if (!node) {
throw ghoul::RuntimeError("Unable to parse file:\n" + file);
}
if (std::string(node->pszValue) == "OpenSpaceTemporalGDALDataset") {
auto tileProvider = std::make_shared<TemporalTileProvider>(file, initData);
return tileProvider;
}
}});
}
} // namespace openspace
@@ -0,0 +1,62 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2016 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __TILE_PROVIDER_FACTORY_H__
#define __TILE_PROVIDER_FACTORY_H__
#include <modules/globebrowsing/tile/temporaltileprovider.h>
#include <modules/globebrowsing/tile/tileprovider.h>
#include <ghoul/misc/dictionary.h>
#include <memory>
#include <string>
#include <functional>
namespace openspace {
class TileProviderFactory {
public:
static std::shared_ptr<TileProviderFactory> ref();
std::shared_ptr<TileProvider> create(const std::string& type, const std::string& desc, const TileProviderInitData& initData);
private:
TileProviderFactory();
void initialize();
typedef std::function<std::shared_ptr<TileProvider>(const std::string&, const TileProviderInitData&)> ConcreteFactory;
std::unordered_map<std::string, ConcreteFactory> _factoryMap;
static std::shared_ptr<TileProviderFactory> _ref;
};
} // namespace openspace
#endif // __TILE_PROVIDER_FACTORY_H__