Skip to content

Commit

Permalink
Operation: Add stats for location of min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jan 11, 2024
1 parent e9275f4 commit bbd7c71
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 42 deletions.
4 changes: 4 additions & 0 deletions python/tests/test_exact_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def make_rect(xmin, ymin, xmax, ymax, id=None, properties=None):
np.array([2.5, 2.5, 2.5, 1.5, 1.5, 1.5, 0.5, 0.5, 0.5], dtype=np.float64),
),
("cell_id", np.array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=np.int64)),
("min_center_x", 0.5),
("min_center_y", 2.5),
("max_center_x", 2.5),
("max_center_y", 0.5),
],
)
def test_basic_stats(stat, expected, output_format):
Expand Down
12 changes: 12 additions & 0 deletions src/operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ Operation::set_result(const StatsRegistry::RasterStatsVariant& stats, Feature& f
std::visit([&f_out, this](const auto& x, const auto& m) { f_out.set(m_field_names[0], x.min().value_or(m)); },
stats,
m_missing);
} else if (stat == "min_center_x") {
std::visit([&f_out, this](const auto& x) { f_out.set(m_field_names[0], x.min_xy().value_or(std::make_pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())).first); },
stats);
} else if (stat == "min_center_y") {
std::visit([&f_out, this](const auto& x) { f_out.set(m_field_names[0], x.min_xy().value_or(std::make_pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())).second); },
stats);
} else if (stat == "max") {
std::visit([&f_out, this](const auto& x, const auto& m) { f_out.set(m_field_names[0], x.max().value_or(m)); },
stats,
m_missing);
} else if (stat == "max_center_x") {
std::visit([&f_out, this](const auto& x) { f_out.set(m_field_names[0], x.max_xy().value_or(std::make_pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())).first); },
stats);
} else if (stat == "max_center_y") {
std::visit([&f_out, this](const auto& x) { f_out.set(m_field_names[0], x.max_xy().value_or(std::make_pair(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN())).second); },
stats);
} else if (stat == "majority" || stat == "mode") {
std::visit(
[&f_out, this](const auto& x, const auto& m) { f_out.set(m_field_names[0], x.mode().value_or(m)); },
Expand Down
2 changes: 1 addition & 1 deletion src/operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Operation

bool requries_stored_locations() const
{
return stat == "center_x" || stat == "center_y" || stat == "cell_id";
return stat == "center_x" || stat == "center_y" || stat == "cell_id" || stat == "min_center_x" || stat == "min_center_y" || stat == "max_center_x" || stat == "max_center_y";
}

/// Returns a newly constructed `Grid` representing the common grid between
Expand Down
Loading

0 comments on commit bbd7c71

Please sign in to comment.