Skip to content

Commit

Permalink
Processor: expose grid_compat_tol
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Aug 17, 2024
1 parent 25d20b4 commit 3b9c69b
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 12 deletions.
3 changes: 3 additions & 0 deletions python/src/exactextract/exact_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def exact_extract(
include_geom=False,
strategy: str = "feature-sequential",
max_cells_in_memory: int = 30000000,
grid_compat_tol: float = 1e-3,
output: str = "geojson",
output_options: Optional[Mapping] = None,
progress=False,
Expand Down Expand Up @@ -304,6 +305,7 @@ def exact_extract(
a cost of higher memory usage.
max_cells_in_memory: Indicates the maximum number of raster cells that should be
loaded into memory at a given time.
grid_compat_tol: require value and weight grids to align within ``grid_compat_tol`` times the smaller of the two grid resolutions
output: An :py:class:`OutputWriter` or one of the following strings:
- "geojson" (the default): return a list of GeoJSON-like features
Expand Down Expand Up @@ -350,6 +352,7 @@ def exact_extract(
if include_geom:
processor.add_geom()
processor.set_max_cells_in_memory(max_cells_in_memory)
processor.set_grid_compat_tol(grid_compat_tol)

if progress:
processor.show_progress(True)
Expand Down
1 change: 1 addition & 0 deletions python/src/pybindings/processor_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bind_processor(py::module& m)
.def("add_col", &Processor::include_col)
.def("add_geom", &Processor::include_geometry)
.def("process", &Processor::process)
.def("set_grid_compat_tol", &Processor::set_grid_compat_tol)
.def("set_max_cells_in_memory", &Processor::set_max_cells_in_memory, py::arg("n"))
.def("set_progress_fn", [](Processor& self, py::function fn) {
std::function<void(double, std::string_view)> wrapper = [fn](double frac, std::string_view message) {
Expand Down
6 changes: 6 additions & 0 deletions src/exactextract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ main(int argc, char** argv)
bool progress = false;
bool nested_output = false;
bool include_geom = false;
double grid_compat_tol = std::numeric_limits<double>::quiet_NaN();

app.add_option("-p,--polygons", poly_descriptor, "polygon dataset")->required(true);
app.add_option("-r,--raster", raster_descriptors, "raster dataset")->required(true);
Expand All @@ -72,6 +73,7 @@ main(int argc, char** argv)
app.add_flag("--nested-output", nested_output, "nested output");
app.add_option("--include-col", include_cols, "columns from input to include in output");
app.add_flag("--include-geom", include_geom, "include geometry in output");
app.add_flag("--grid-compat-tol", grid_compat_tol, "grid compatibility tolerance");

app.add_flag("--progress", progress);
app.set_config("--config");
Expand Down Expand Up @@ -147,6 +149,10 @@ main(int argc, char** argv)
proc->include_geometry();
}

if (!std::isnan(grid_compat_tol)) {
proc->set_grid_compat_tol(grid_compat_tol);
}

proc->set_max_cells_in_memory(max_cells_in_memory);
proc->show_progress(progress);
if (progress) {
Expand Down
2 changes: 1 addition & 1 deletion src/feature_sequential_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ FeatureSequentialProcessor::process()

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

auto grid = common_grid(m_operations.begin(), m_operations.end());
auto grid = common_grid(m_operations.begin(), m_operations.end(), m_grid_compat_tol);

if (feature_bbox.intersects(grid.extent())) {
// Crop grid to portion overlapping feature
Expand Down
16 changes: 9 additions & 7 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "box.h"

constexpr double DEFAULT_GRID_COMPAT_TOL = 1e-6;

namespace exactextract {
struct infinite_extent
{
Expand Down Expand Up @@ -274,7 +276,7 @@ class Grid
}

template<typename extent_tag2>
Grid<extent_tag> common_grid(const Grid<extent_tag2>& b, double tol = 1e-6) const
Grid<extent_tag> common_grid(const Grid<extent_tag2>& b, double tol = DEFAULT_GRID_COMPAT_TOL) const
{
if (!compatible_with(b, tol)) {
throw std::runtime_error("Incompatible extents.");
Expand Down Expand Up @@ -303,7 +305,7 @@ class Grid
}

template<typename extent_tag2>
Grid<extent_tag> overlapping_grid(const Grid<extent_tag2>& b, double tol = 1e-6) const
Grid<extent_tag> overlapping_grid(const Grid<extent_tag2>& b, double tol = DEFAULT_GRID_COMPAT_TOL) const
{
if (!compatible_with(b, tol)) {
throw std::runtime_error("Incompatible extents.");
Expand Down Expand Up @@ -368,19 +370,19 @@ subdivide(const Grid<bounded_extent>& grid, size_t max_size);

template<typename T>
Grid<bounded_extent>
common_grid(T begin, T end)
common_grid(T begin, T end, double tol = DEFAULT_GRID_COMPAT_TOL)
{
if (begin == end) {
return Grid<bounded_extent>::make_empty();
} else if (std::next(begin) == end) {
return (*begin)->grid();
return (*begin)->grid(tol);
}
return std::accumulate(
std::next(begin),
end,
(*begin)->grid(),
[](auto& acc, auto& op) {
return acc.common_grid(op->grid());
(*begin)->grid(tol),
[tol](auto& acc, auto& op) {
return acc.common_grid(op->grid(tol), tol);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,10 @@ Operation::intersects(const Box& box) const
}

Grid<bounded_extent>
Operation::grid() const
Operation::grid(double compat_tol) const
{
if (weighted()) {
return values->grid().common_grid(weights->grid());
return values->grid().common_grid(weights->grid(), compat_tol);
} else {
return values->grid();
}
Expand Down
2 changes: 1 addition & 1 deletion src/operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Operation

/// Returns a newly constructed `Grid` representing the common grid between
/// the value and weighting rasters
Grid<bounded_extent> grid() const;
Grid<bounded_extent> grid(double ncompat_tol) const;

/// Returns the type of the `Operation` result
virtual Feature::ValueType result_type() const = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class Processor
m_max_cells_in_memory = n;
}

void set_grid_compat_tol(double tol)
{
m_grid_compat_tol = tol;
}

void show_progress(bool val)
{
m_show_progress = val;
Expand Down Expand Up @@ -145,6 +150,7 @@ class Processor

std::vector<std::string> m_include_cols;

double m_grid_compat_tol = DEFAULT_GRID_COMPAT_TOL;
size_t m_max_cells_in_memory = 1000000L;

std::function<void(double, std::string_view)> m_progress_fn;
Expand Down
2 changes: 1 addition & 1 deletion src/raster_sequential_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ RasterSequentialProcessor::process()
read_features();
populate_index();

auto grid = common_grid(m_operations.begin(), m_operations.end());
auto grid = common_grid(m_operations.begin(), m_operations.end(), m_grid_compat_tol);

auto subgrids = subdivide(grid, m_max_cells_in_memory);
for (std::size_t i = 0; i < subgrids.size(); i++) {
Expand Down

0 comments on commit 3b9c69b

Please sign in to comment.