Skip to content

Commit 3f3a3e9

Browse files
authored
Simplify: Preserve dimensions of empty geometries (#1357)
* DouglasPeuckerSimplifier: Preserve dimension of empty geometries * TopologyPreservingSimplifier: Add test for preservation of empty geometry dims
1 parent eb6121a commit 3f3a3e9

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

include/geos/simplify/DouglasPeuckerSimplifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class GEOS_DLL DouglasPeuckerSimplifier {
6868
*/
6969
void setDistanceTolerance(double tolerance);
7070

71-
std::unique_ptr<geom::Geometry> getResultGeometry();
71+
std::unique_ptr<geom::Geometry> getResultGeometry() const;
7272

7373

7474
private:

src/simplify/DouglasPeuckerSimplifier.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ DouglasPeuckerSimplifier::setDistanceTolerance(double tol)
201201
}
202202

203203
Geometry::Ptr
204-
DouglasPeuckerSimplifier::getResultGeometry()
204+
DouglasPeuckerSimplifier::getResultGeometry() const
205205
{
206+
if (inputGeom->isEmpty()) {
207+
return inputGeom->clone();
208+
}
209+
206210
DPTransformer t(distanceTolerance);
207211
return t.transform(inputGeom);
208212

tests/unit/simplify/DouglasPeuckerSimplifierTest.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct test_dpsimp_data {
3737
{}
3838

3939
void
40-
checkDP(const std::string& wkt, double tolerance, const std::string& wkt_expected)
40+
checkDP(const std::string& wkt, double tolerance, const std::string& wkt_expected) const
4141
{
4242
GeomPtr g(wktreader.read(wkt));
4343
GeomPtr simplified = DouglasPeuckerSimplifier::simplify(g.get(), tolerance);
@@ -49,7 +49,7 @@ struct test_dpsimp_data {
4949
}
5050

5151
void
52-
checkDPNoChange(const std::string& wkt, double tolerance)
52+
checkDPNoChange(const std::string& wkt, double tolerance) const
5353
{
5454
checkDP(wkt, tolerance, wkt);
5555
}
@@ -382,4 +382,24 @@ void object::test<23>()
382382
"POLYGON ZM ((2 0 10 9, 2 2 15 11, 0 2 20 13, 0 0 25 15, 2 0 10 9))");
383383
}
384384

385+
template<>
386+
template<>
387+
void object::test<24>()
388+
{
389+
set_test_name("simplification preserves dimensions of empty geometries");
390+
391+
const std::vector<std::string> geomTypes{"POINT", "LINESTRING", "POLYGON"};
392+
const std::vector<std::string> dimensions{"", "Z", "M", "ZM"};
393+
const std::vector<std::string> mods{"", "MULTI"};
394+
395+
for (const auto& geomType : geomTypes) {
396+
for (const auto& dimension: dimensions) {
397+
for (const auto& mod: mods) {
398+
std::string wkt = mod + geomType + " " + dimension + " EMPTY";
399+
checkDP(wkt, 0, wkt);
400+
}
401+
}
402+
}
403+
}
404+
385405
} // namespace tut

tests/unit/simplify/TopologyPreservingSimplifierTest.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ struct test_tpsimp_data {
3434
}
3535

3636
void
37-
checkTPS(const std::string& wkt, double tolerance, const std::string& wkt_expected)
37+
checkTPS(const std::string& wkt, double tolerance, const std::string& wkt_expected) const
3838
{
3939
GeomPtr g(wktreader.read(wkt));
4040
GeomPtr simplified = TopologyPreservingSimplifier::simplify(g.get(), tolerance);
4141
GeomPtr exp(wktreader.read(wkt_expected));
42-
ensure_equals_geometry(exp.get(), simplified.get(),0.00001);
42+
ensure_equals_geometry_xyzm(exp.get(), simplified.get(),0.00001);
4343
ensure("Simplified geometry is invalid!", simplified->isValid());
4444
}
4545

4646
void
47-
checkTPSNoChange(const std::string& wkt, double tolerance)
47+
checkTPSNoChange(const std::string& wkt, double tolerance) const
4848
{
4949
checkTPS(wkt, tolerance, wkt);
5050
}
@@ -453,4 +453,24 @@ void object::test<35>()
453453
"MULTIPOLYGON (((689.300102 5733.615673, 689.46186 5733.617409, 689.458981 5733.646089, 689.300102 5733.615673)), ((689.488158 5733.746304, 689.23796 5733.680098, 689.253227 5733.613915, 689.467162 5733.67151, 689.679568 5733.588383, 689.488158 5733.746304)))");
454454
}
455455

456+
template<>
457+
template<>
458+
void object::test<36>()
459+
{
460+
set_test_name("simplification preserves dimensions of empty geometries");
461+
462+
const std::vector<std::string> geomTypes{"POINT", "LINESTRING", "POLYGON"};
463+
const std::vector<std::string> dimensions{"", "Z", "M", "ZM"};
464+
const std::vector<std::string> mods{"", "MULTI"};
465+
466+
for (const auto& geomType : geomTypes) {
467+
for (const auto& dimension: dimensions) {
468+
for (const auto& mod: mods) {
469+
std::string wkt = mod + geomType + " " + dimension + " EMPTY";
470+
checkTPSNoChange(wkt, 0);
471+
}
472+
}
473+
}
474+
}
475+
456476
} // namespace tut

0 commit comments

Comments
 (0)