Skip to content

Commit

Permalink
Add ability to delete cache files from tiler
Browse files Browse the repository at this point in the history
- To allow the system to reset after subsequent runs, it can be useful
to have the system automatically delete cache tiles when starting a new
run.
- This is added as a command line option to allow enabling/disabling of
the behavior
- This is preparation for allowing a job to resume (sans old keypoints)
after a crash
  • Loading branch information
zthorson committed Aug 17, 2021
1 parent cf6a492 commit 00d50fc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
10 changes: 7 additions & 3 deletions modules/realm_ortho/include/realm_ortho/tile_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class TileCache : public WorkerThreadBase
void flushAll();
void loadAll();

void deleteCache();
void deleteCache(std::string layer);

private:

bool m_has_init_directories;
Expand All @@ -64,6 +67,7 @@ class TileCache : public WorkerThreadBase

std::mutex m_mutex_cache;
std::map<int, CacheElementGrid> m_cache;
std::mutex m_mutex_file_write;

std::mutex m_mutex_do_update;
bool m_do_update;
Expand All @@ -78,10 +82,10 @@ class TileCache : public WorkerThreadBase

void reset() override;

void load(const CacheElement::Ptr &element) const;
void write(const CacheElement::Ptr &element) const;
void load(const CacheElement::Ptr &element);
void write(const CacheElement::Ptr &element);

void flush(const CacheElement::Ptr &element) const;
void flush(const CacheElement::Ptr &element);

bool isCached(const CacheElement::Ptr &element) const;

Expand Down
3 changes: 0 additions & 3 deletions modules/realm_ortho/src/map_tiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ std::map<int, MapTiler::TiledMap> MapTiler::createTiles(const CvGridMap::Ptr &ma
// Therefore first identify how many tiles we have to split our map into by computing the tile indices
cv::Rect2i tile_bounds_idx = computeTileBounds(roi, zoom_level);

LOG_F(INFO, "SENTERA: Tile bounds idx: %d, %d", tile_bounds_idx.x, tile_bounds_idx.y);

// With the tile indices we can compute the exact region of interest in the geographic frame in meters
cv::Rect2d tile_bounds_meters = computeTileBoundsMeters(tile_bounds_idx, zoom_level);
LOG_F(INFO, "SENTERA: Tile bounds meters: %f, %f %f x %f", tile_bounds_meters.x, tile_bounds_meters.y, tile_bounds_meters.width, tile_bounds_meters.height);

// Because our map is not yet guaranteed to have exactly the size of the tile region, we have to perform padding to
// to fit exactly the tile map boundaries
Expand Down
29 changes: 26 additions & 3 deletions modules/realm_ortho/src/tile_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,30 @@ void TileCache::loadAll()
}
}

void TileCache::load(const CacheElement::Ptr &element) const
void TileCache::deleteCache()
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
// Remove all cache items
flushAll();
m_has_init_directories = false;
auto files = io::getFileList(m_dir_toplevel, "");
for (auto & file : files) {
if (!file.empty()) io::removeFileOrDirectory(file);
}
}

void TileCache::deleteCache(std::string layer)
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
// Attempt to remove the specific layer name
flushAll();
m_has_init_directories = false;
io::removeFileOrDirectory(m_dir_toplevel + "/" + layer);
}

void TileCache::load(const CacheElement::Ptr &element)
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
for (const auto &meta : element->layer_meta)
{
std::string filename = m_dir_toplevel + "/"
Expand Down Expand Up @@ -303,8 +325,9 @@ void TileCache::load(const CacheElement::Ptr &element) const
}
}

void TileCache::write(const CacheElement::Ptr &element) const
void TileCache::write(const CacheElement::Ptr &element)
{
std::lock_guard<std::mutex> lock(m_mutex_file_write);
for (const auto &meta : element->layer_meta)
{
cv::Mat data = element->tile->data()->get(meta.name);
Expand Down Expand Up @@ -341,7 +364,7 @@ void TileCache::write(const CacheElement::Ptr &element) const
}
}

void TileCache::flush(const CacheElement::Ptr &element) const
void TileCache::flush(const CacheElement::Ptr &element)
{
if (!element->was_written)
write(element);
Expand Down
1 change: 1 addition & 0 deletions modules/realm_stages/include/realm_stages/stage_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class TileingSettings : public StageSettings
add("tms_tiles", Parameter_t<int>{1, "Generate output tiles using TMS standard. If false, Google/OSM standard is used."});
add("min_zoom", Parameter_t<int>{11, "The minimum tile zoom to generate for the output tiles."});
add("max_zoom", Parameter_t<int>{20, "The maximum tile zoom to generate for the output tiles. (Set to -1 for maximum zoom based on GSD)"});
add("delete_cache_on_init", Parameter_t<int>{0, "If there are leftover cache items in the cache folder, delete them before starting the stage."});
}
};

Expand Down
6 changes: 6 additions & 0 deletions modules/realm_stages/include/realm_stages/tileing.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Tileing : public StageBase
bool process() override;
void saveAll();

void deleteCache();
void deleteCache(std::string layer);

private:
std::deque<Frame::Ptr> m_buffer;
std::mutex m_mutex_buffer;
Expand All @@ -78,6 +81,9 @@ class Tileing : public StageBase
/// The maximum zoom to generate. May not be generated if GSD isn't sufficient
int m_max_tile_zoom;

/// Indicates we should wipe the cache directory when starting or resetting the stage
bool m_delete_cache_on_init;

/// The directory to store the output map tiles in, defaults to log directory
std::string m_cache_path;

Expand Down
17 changes: 17 additions & 0 deletions modules/realm_stages/src/tileing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Tileing::Tileing(const StageSettings::Ptr &stage_set, double rate)
m_generate_tms_tiles((*stage_set)["tms_tiles"].toInt() > 0),
m_min_tile_zoom((*stage_set)["min_zoom"].toInt()),
m_max_tile_zoom((*stage_set)["max_zoom"].toInt()),
m_delete_cache_on_init((*stage_set)["delete_cache_on_init"].toInt() > 0),
m_utm_reference(nullptr),
m_map_tiler(nullptr),
m_tile_cache(nullptr),
Expand Down Expand Up @@ -246,6 +247,19 @@ bool Tileing::process()
return has_processed;
}

void Tileing::deleteCache() {
if (m_tile_cache)
{
m_tile_cache->deleteCache();
}
}

void Tileing::deleteCache(std::string layer) {
if (m_tile_cache) {
m_tile_cache->deleteCache(layer);
}
}

Tile::Ptr Tileing::merge(const Tile::Ptr &t1, const Tile::Ptr &t2)
{
if (t2->data()->empty())
Expand Down Expand Up @@ -390,6 +404,9 @@ void Tileing::initStageCallback()
{
m_map_tiler = std::make_shared<MapTiler>(true, m_generate_tms_tiles);
m_tile_cache = std::make_shared<TileCache>("tile_cache", 500, m_cache_path, false);
if (m_delete_cache_on_init) {
deleteCache();
}
m_tile_cache->start();
}
}
Expand Down

0 comments on commit 00d50fc

Please sign in to comment.