diff --git a/src/exactextract.cpp b/src/exactextract.cpp index 8dc19ddf..d282f1fe 100644 --- a/src/exactextract.cpp +++ b/src/exactextract.cpp @@ -149,6 +149,9 @@ main(int argc, char** argv) proc->set_max_cells_in_memory(max_cells_in_memory); proc->show_progress(progress); + if (progress) { + proc->set_progress_fn(exactextract::cli_progress); + } proc->process(); writer->finish(); diff --git a/src/feature_sequential_processor.cpp b/src/feature_sequential_processor.cpp index 9fb33b4b..5f5ebfad 100644 --- a/src/feature_sequential_processor.cpp +++ b/src/feature_sequential_processor.cpp @@ -12,6 +12,7 @@ // limitations under the License. #include +#include #include #include "box.h" @@ -22,6 +23,35 @@ namespace exactextract { +std::string +FeatureSequentialProcessor::progress_message(const Feature& f) +{ + if (m_include_cols.empty()) { + return "."; + } + + const std::string& field = m_include_cols.front(); + + try { + std::stringstream ss; + ss << "Processing "; + const auto& value = f.get(field); + if (const std::string* sval = std::get_if(&value)) { + ss << *sval; + } else if (const std::int32_t* ival = std::get_if(&value)) { + ss << *ival; + } else if (const double* dval = std::get_if(&value)) { + ss << *dval; + } else { + ss << "."; + } + + return ss.str(); + } catch (const std::exception&) { + return "."; + } +} + void FeatureSequentialProcessor::process() { @@ -30,7 +60,9 @@ FeatureSequentialProcessor::process() auto geom = f_in.geometry(); - progress(f_in, m_include_cols.empty() ? "." : m_include_cols.front()); + if (m_show_progress) { + progress(progress_message(f_in)); + } Box feature_bbox = exactextract::geos_get_box(m_geos_context, geom); @@ -79,8 +111,6 @@ FeatureSequentialProcessor::process() } else { m_reg.update_stats(f_in, *op, *coverage, values_map[op->values]); } - - progress(); } } } diff --git a/src/feature_sequential_processor.h b/src/feature_sequential_processor.h index 63ddcccb..3f2f57f8 100644 --- a/src/feature_sequential_processor.h +++ b/src/feature_sequential_processor.h @@ -11,8 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef EXACTEXTRACT_FEATURE_SEQUENTIAL_PROCESSOR_H -#define EXACTEXTRACT_FEATURE_SEQUENTIAL_PROCESSOR_H +#pragma once #include "processor.h" @@ -29,7 +28,8 @@ class FeatureSequentialProcessor : public Processor using Processor::Processor; void process() override; + + private: + std::string progress_message(const Feature& f); }; } - -#endif diff --git a/src/processor.h b/src/processor.h index 08aa9d40..48c4db3b 100644 --- a/src/processor.h +++ b/src/processor.h @@ -93,6 +93,11 @@ class Processor m_show_progress = val; } + void set_progress_fn(std::function fn) + { + m_progress_fn = fn; + } + void write_result(const Feature& f_in) { auto f_out = m_output.create_feature(); @@ -110,38 +115,18 @@ class Processor } protected: - template - void progress(const T& name) const + void progress(std::string_view message) const { - if (m_show_progress) - std::cout << std::endl - << "Processing " << name << std::flush; - } + if (!m_show_progress) { + return; + } - void progress(const Feature& f, const std::string& field) const - { - if (m_show_progress) { - std::cout << std::endl - << "Processing "; - const auto& value = f.get(field); - if (const std::string* sval = std::get_if(&value)) { - std::cout << *sval; - } else if (const std::int32_t* ival = std::get_if(&value)) { - std::cout << *ival; - } else if (const double* dval = std::get_if(&value)) { - std::cout << *dval; - } else { - std::cout << "."; - } - - std::cout << std::flush; + if (m_progress_fn) { + (m_progress_fn)(message); + return; } - } - void progress() const - { - if (m_show_progress) - std::cout << "." << std::flush; + std::cout << "." << std::flush; } StatsRegistry m_reg; @@ -160,5 +145,7 @@ class Processor std::vector m_include_cols; size_t m_max_cells_in_memory = 1000000L; + + std::function m_progress_fn; }; -} \ No newline at end of file +} diff --git a/src/raster_sequential_processor.cpp b/src/raster_sequential_processor.cpp index c6d9a0f8..cd5d431f 100644 --- a/src/raster_sequential_processor.cpp +++ b/src/raster_sequential_processor.cpp @@ -112,12 +112,14 @@ RasterSequentialProcessor::process() } else { m_reg.update_stats(*f, *op, *coverage, *values); } - - progress(); } } - progress(subgrid.extent()); + if (m_show_progress) { + std::stringstream ss; + ss << subgrid.extent(); + progress(ss.str()); + } } for (const auto& f_in : m_features) { diff --git a/src/utils_cli.cpp b/src/utils_cli.cpp index 850a9ee8..12ff87d3 100644 --- a/src/utils_cli.cpp +++ b/src/utils_cli.cpp @@ -74,4 +74,12 @@ load_gdal_rasters(const std::vector& descriptors) return raster_sources; } -} \ No newline at end of file + +void +cli_progress(std::string_view message) +{ + std::cout << message << std::endl + << std::flush; +} + +} diff --git a/src/utils_cli.h b/src/utils_cli.h index da4f2b99..e73b8127 100644 --- a/src/utils_cli.h +++ b/src/utils_cli.h @@ -14,9 +14,14 @@ #pragma once #include "utils.h" +#include namespace exactextract { + std::vector> load_gdal_rasters(const std::vector& descriptors); +void +cli_progress(std::string_view message); + }