Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/geom-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,17 @@ void simplify(geometry_t *output, geometry_t const &geom, double tolerance)
return;
}

output->set<linestring_t>();
auto &ls = output->set<linestring_t>();
output->set_srid(geom.srid());

boost::geometry::simplify(geom.get<linestring_t>(),
output->get<linestring_t>(), tolerance);
boost::geometry::simplify(geom.get<linestring_t>(), ls, tolerance);

// Linestrings with less then 2 nodes are invalid. Older boost::geometry
// versions will generate a "line" with two identical points which the
// second check finds.
if (ls.size() < 2 || ls[0] == ls[1]) {
output->reset();
}
}

geometry_t simplify(geometry_t const &geom, double tolerance)
Expand Down
41 changes: 40 additions & 1 deletion tests/test-geom-linestrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ TEST_CASE("geom::simplify", "[NoDB]")

SECTION("large tolerance simplifies linestring")
{
auto const geom = geom::simplify(input, 2.0);
auto const geom = geom::simplify(input, 10.0);

REQUIRE(geom.is_linestring());
auto const &l = geom.get<geom::linestring_t>();
Expand All @@ -219,3 +219,42 @@ TEST_CASE("geom::simplify", "[NoDB]")
REQUIRE(l[1] == input.get<geom::linestring_t>()[5]);
}
}

TEST_CASE("geom::simplify of a loop", "[NoDB]")
{
geom::geometry_t const input{
geom::linestring_t{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0.1, 0.1}, {0, 0}}};

SECTION("small tolerance leaves linestring as is")
{
auto const geom = geom::simplify(input, 0.01);

REQUIRE(geom.is_linestring());
auto const &l = geom.get<geom::linestring_t>();
REQUIRE(l.size() == 6);
REQUIRE(l == input.get<geom::linestring_t>());
}

SECTION("medium tolerance simplifies linestring")
{
auto const geom = geom::simplify(input, 0.5);

REQUIRE(geom.is_linestring());
auto const &l = geom.get<geom::linestring_t>();
REQUIRE(l.size() == 5);

auto const &il = input.get<geom::linestring_t>();
REQUIRE(l[0] == il[0]);
REQUIRE(l[1] == il[1]);
REQUIRE(l[2] == il[2]);
REQUIRE(l[3] == il[3]);
REQUIRE(l[4] == il[5]);
}

SECTION("large tolerance breaks linestring, null geometry is returned")
{
auto const geom = geom::simplify(input, 10.0);

REQUIRE(geom.is_null());
}
}