Skip to content

Commit

Permalink
Processor: Allow client to specify progress callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jun 27, 2024
1 parent 5cfeff2 commit bf1f347
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
3 changes: 3 additions & 0 deletions src/exactextract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 153 in src/exactextract.cpp

View check run for this annotation

Codecov / codecov/patch

src/exactextract.cpp#L153

Added line #L153 was not covered by tests
}

proc->process();
writer->finish();
Expand Down
36 changes: 33 additions & 3 deletions src/feature_sequential_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

#include <set>
#include <sstream>
#include <string>

#include "box.h"
Expand All @@ -22,6 +23,35 @@

namespace exactextract {

std::string
FeatureSequentialProcessor::progress_message(const Feature& f)

Check warning on line 27 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L27

Added line #L27 was not covered by tests
{
if (m_include_cols.empty()) {
return ".";

Check warning on line 30 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L30

Added line #L30 was not covered by tests
}

const std::string& field = m_include_cols.front();

Check warning on line 33 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L33

Added line #L33 was not covered by tests

try {
std::stringstream ss;
ss << "Processing ";
const auto& value = f.get(field);

Check warning on line 38 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L36-L38

Added lines #L36 - L38 were not covered by tests
if (const std::string* sval = std::get_if<std::string>(&value)) {
ss << *sval;

Check warning on line 40 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L40

Added line #L40 was not covered by tests
} else if (const std::int32_t* ival = std::get_if<std::int32_t>(&value)) {
ss << *ival;

Check warning on line 42 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L42

Added line #L42 was not covered by tests
} else if (const double* dval = std::get_if<double>(&value)) {
ss << *dval;

Check warning on line 44 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L44

Added line #L44 was not covered by tests
} else {
ss << ".";

Check warning on line 46 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L46

Added line #L46 was not covered by tests
}

return ss.str();
} catch (const std::exception&) {
return ".";

Check warning on line 51 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L49-L51

Added lines #L49 - L51 were not covered by tests
}
}

void
FeatureSequentialProcessor::process()
{
Expand All @@ -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));

Check warning on line 64 in src/feature_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/feature_sequential_processor.cpp#L64

Added line #L64 was not covered by tests
}

Box feature_bbox = exactextract::geos_get_box(m_geos_context, geom);

Expand Down Expand Up @@ -79,8 +111,6 @@ FeatureSequentialProcessor::process()
} else {
m_reg.update_stats(f_in, *op, *coverage, values_map[op->values]);
}

progress();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/feature_sequential_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -29,7 +28,8 @@ class FeatureSequentialProcessor : public Processor
using Processor::Processor;

void process() override;

private:
std::string progress_message(const Feature& f);
};
}

#endif
45 changes: 16 additions & 29 deletions src/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Processor
m_show_progress = val;
}

void set_progress_fn(std::function<void(std::string_view)> fn)

Check warning on line 96 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L96

Added line #L96 was not covered by tests
{
m_progress_fn = fn;

Check warning on line 98 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L98

Added line #L98 was not covered by tests
}

void write_result(const Feature& f_in)
{
auto f_out = m_output.create_feature();
Expand All @@ -110,38 +115,18 @@ class Processor
}

protected:
template<typename T>
void progress(const T& name) const
void progress(std::string_view message) const

Check warning on line 118 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L118

Added line #L118 was not covered by tests
{
if (m_show_progress)
std::cout << std::endl
<< "Processing " << name << std::flush;
}
if (!m_show_progress) {
return;

Check warning on line 121 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L120-L121

Added lines #L120 - L121 were not covered by tests
}

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<std::string>(&value)) {
std::cout << *sval;
} else if (const std::int32_t* ival = std::get_if<std::int32_t>(&value)) {
std::cout << *ival;
} else if (const double* dval = std::get_if<double>(&value)) {
std::cout << *dval;
} else {
std::cout << ".";
}

std::cout << std::flush;
if (m_progress_fn) {
(m_progress_fn)(message);
return;

Check warning on line 126 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L124-L126

Added lines #L124 - L126 were not covered by tests
}
}

void progress() const
{
if (m_show_progress)
std::cout << "." << std::flush;
std::cout << "." << std::flush;

Check warning on line 129 in src/processor.h

View check run for this annotation

Codecov / codecov/patch

src/processor.h#L129

Added line #L129 was not covered by tests
}

StatsRegistry m_reg;
Expand All @@ -160,5 +145,7 @@ class Processor
std::vector<std::string> m_include_cols;

size_t m_max_cells_in_memory = 1000000L;

std::function<void(std::string_view)> m_progress_fn;
};
}
}
8 changes: 5 additions & 3 deletions src/raster_sequential_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check warning on line 121 in src/raster_sequential_processor.cpp

View check run for this annotation

Codecov / codecov/patch

src/raster_sequential_processor.cpp#L119-L121

Added lines #L119 - L121 were not covered by tests
}
}

for (const auto& f_in : m_features) {
Expand Down
10 changes: 9 additions & 1 deletion src/utils_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ load_gdal_rasters(const std::vector<std::string>& descriptors)

return raster_sources;
}
}

void
cli_progress(std::string_view message)

Check warning on line 79 in src/utils_cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/utils_cli.cpp#L79

Added line #L79 was not covered by tests
{
std::cout << message << std::endl
<< std::flush;

Check warning on line 82 in src/utils_cli.cpp

View check run for this annotation

Codecov / codecov/patch

src/utils_cli.cpp#L81-L82

Added lines #L81 - L82 were not covered by tests
}

}
5 changes: 5 additions & 0 deletions src/utils_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
#pragma once

#include "utils.h"
#include <string>

namespace exactextract {

std::vector<std::unique_ptr<RasterSource>>
load_gdal_rasters(const std::vector<std::string>& descriptors);

void
cli_progress(std::string_view message);

}

0 comments on commit bf1f347

Please sign in to comment.