Add singletone class RasterCache to handle global cache data.
This commit is contained in:
parent
f36707d1fb
commit
542c3ba872
36
include/extractor/raster_source.hpp
Normal file → Executable file
36
include/extractor/raster_source.hpp
Normal file → Executable file
@ -126,14 +126,7 @@ class RasterSource
|
||||
class RasterContainer
|
||||
{
|
||||
public:
|
||||
RasterContainer() { ++count; }
|
||||
~RasterContainer() {
|
||||
--count;
|
||||
if (0 == count) {
|
||||
LoadedSources.clear();
|
||||
LoadedSourcePaths.clear();
|
||||
}
|
||||
}
|
||||
RasterContainer() = default;
|
||||
|
||||
int LoadRasterSource(const std::string &path_string,
|
||||
double xmin,
|
||||
@ -148,9 +141,30 @@ class RasterContainer
|
||||
RasterDatum GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat);
|
||||
|
||||
private:
|
||||
static std::vector<RasterSource> LoadedSources;
|
||||
static std::unordered_map<std::string, int> LoadedSourcePaths;
|
||||
static int count;
|
||||
};
|
||||
|
||||
// << singletone >> RasterCache
|
||||
class RasterCache
|
||||
{
|
||||
public:
|
||||
// class method to get the instance
|
||||
static RasterCache& getInstance() {
|
||||
if (NULL == g_instance) {
|
||||
g_instance = new RasterCache();
|
||||
}
|
||||
return *g_instance;
|
||||
}
|
||||
// get reference of cache
|
||||
std::vector<RasterSource>& getLoadedSources() { return LoadedSources; }
|
||||
std::unordered_map<std::string, int>& getLoadedSourcePaths() { return LoadedSourcePaths; }
|
||||
private:
|
||||
// constructor
|
||||
RasterCache() = default;
|
||||
// member
|
||||
std::vector<RasterSource> LoadedSources;
|
||||
std::unordered_map<std::string, int> LoadedSourcePaths;
|
||||
// the instance
|
||||
static RasterCache *g_instance;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -78,11 +78,6 @@ RasterDatum RasterSource::GetRasterInterpolate(const int lon, const int lat) con
|
||||
raster_data(right, bottom) * (fromLeft * fromTop))};
|
||||
}
|
||||
|
||||
// static member of Raster Container
|
||||
std::vector<RasterSource> RasterContainer::LoadedSources;
|
||||
std::unordered_map<std::string, int> RasterContainer::LoadedSourcePaths;
|
||||
int RasterContainer::count = 0;
|
||||
|
||||
// Load raster source into memory
|
||||
int RasterContainer::LoadRasterSource(const std::string &path_string,
|
||||
double xmin,
|
||||
@ -97,15 +92,15 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
|
||||
const auto _ymin = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymin}));
|
||||
const auto _ymax = static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{ymax}));
|
||||
|
||||
const auto itr = LoadedSourcePaths.find(path_string);
|
||||
if (itr != LoadedSourcePaths.end())
|
||||
const auto itr = RasterCache::getInstance().getLoadedSourcePaths().find(path_string);
|
||||
if (itr != RasterCache::getInstance().getLoadedSourcePaths().end())
|
||||
{
|
||||
util::Log() << "[source loader] Already loaded source '" << path_string << "' at source_id "
|
||||
<< itr->second;
|
||||
return itr->second;
|
||||
}
|
||||
|
||||
int source_id = static_cast<int>(LoadedSources.size());
|
||||
int source_id = static_cast<int>(RasterCache::getInstance().getLoadedSources().size());
|
||||
|
||||
util::Log() << "[source loader] Loading from " << path_string << " ... ";
|
||||
TIMER_START(loading_source);
|
||||
@ -121,8 +116,8 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
|
||||
|
||||
RasterSource source{std::move(rasterData), ncols, nrows, _xmin, _xmax, _ymin, _ymax};
|
||||
TIMER_STOP(loading_source);
|
||||
LoadedSourcePaths.emplace(path_string, source_id);
|
||||
LoadedSources.push_back(std::move(source));
|
||||
RasterCache::getInstance().getLoadedSourcePaths().emplace(path_string, source_id);
|
||||
RasterCache::getInstance().getLoadedSources().push_back(std::move(source));
|
||||
|
||||
util::Log() << "[source loader] ok, after " << TIMER_SEC(loading_source) << "s";
|
||||
|
||||
@ -132,10 +127,10 @@ int RasterContainer::LoadRasterSource(const std::string &path_string,
|
||||
// External function for looking up nearest data point from a specified source
|
||||
RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, double lon, double lat)
|
||||
{
|
||||
if (LoadedSources.size() < source_id + 1)
|
||||
if (RasterCache::getInstance().getLoadedSources().size() < source_id + 1)
|
||||
{
|
||||
throw util::exception("Attempted to access source " + std::to_string(source_id) +
|
||||
", but there are only " + std::to_string(LoadedSources.size()) +
|
||||
", but there are only " + std::to_string(RasterCache::getInstance().getLoadedSources().size()) +
|
||||
" loaded" + SOURCE_REF);
|
||||
}
|
||||
|
||||
@ -144,7 +139,7 @@ RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, dou
|
||||
BOOST_ASSERT(lon < 180);
|
||||
BOOST_ASSERT(lon > -180);
|
||||
|
||||
const auto &found = LoadedSources[source_id];
|
||||
const auto &found = RasterCache::getInstance().getLoadedSources()[source_id];
|
||||
return found.GetRasterData(static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})),
|
||||
static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat})));
|
||||
}
|
||||
@ -153,10 +148,10 @@ RasterDatum RasterContainer::GetRasterDataFromSource(unsigned int source_id, dou
|
||||
RasterDatum
|
||||
RasterContainer::GetRasterInterpolateFromSource(unsigned int source_id, double lon, double lat)
|
||||
{
|
||||
if (LoadedSources.size() < source_id + 1)
|
||||
if (RasterCache::getInstance().getLoadedSources().size() < source_id + 1)
|
||||
{
|
||||
throw util::exception("Attempted to access source " + std::to_string(source_id) +
|
||||
", but there are only " + std::to_string(LoadedSources.size()) +
|
||||
", but there are only " + std::to_string(RasterCache::getInstance().getLoadedSources().size()) +
|
||||
" loaded" + SOURCE_REF);
|
||||
}
|
||||
|
||||
@ -165,10 +160,12 @@ RasterContainer::GetRasterInterpolateFromSource(unsigned int source_id, double l
|
||||
BOOST_ASSERT(lon < 180);
|
||||
BOOST_ASSERT(lon > -180);
|
||||
|
||||
const auto &found = LoadedSources[source_id];
|
||||
const auto &found = RasterCache::getInstance().getLoadedSources()[source_id];
|
||||
return found.GetRasterInterpolate(
|
||||
static_cast<std::int32_t>(util::toFixed(util::FloatLongitude{lon})),
|
||||
static_cast<std::int32_t>(util::toFixed(util::FloatLatitude{lat})));
|
||||
}
|
||||
|
||||
RasterCache *RasterCache::g_instance = NULL;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user