diff --git a/include/geos/geom/CompoundCurve.h b/include/geos/geom/CompoundCurve.h index 1dbdf0f230..211a4aaa46 100644 --- a/include/geos/geom/CompoundCurve.h +++ b/include/geos/geom/CompoundCurve.h @@ -86,6 +86,8 @@ class GEOS_DLL CompoundCurve : public Curve { std::unique_ptr reverse() const; + void validateConstruction() const; + protected: /// Construct a CompoundCurve, taking ownership of the /// provided CoordinateSequence diff --git a/src/geom/CompoundCurve.cpp b/src/geom/CompoundCurve.cpp index 2175c7ed2b..740e7c5bce 100644 --- a/src/geom/CompoundCurve.cpp +++ b/src/geom/CompoundCurve.cpp @@ -12,6 +12,8 @@ * **********************************************************************/ +#include + #include #include #include @@ -25,7 +27,9 @@ CompoundCurve::CompoundCurve(std::vector>&& p_curve const GeometryFactory& gf) : Curve(gf), curves(std::move(p_curves)), - envelope(computeEnvelopeInternal()) {} + envelope(computeEnvelopeInternal()) { + validateConstruction(); +} CompoundCurve::CompoundCurve(const CompoundCurve& other) : Curve(other), @@ -307,5 +311,24 @@ CompoundCurve::reverseImpl() const return getFactory()->createCompoundCurve(std::move(reversed)).release(); } +void +CompoundCurve::validateConstruction() const +{ + for (std::size_t i = 1; i < curves.size(); i++) { + const CoordinateXY& end = curves[i-1]->getCoordinatesRO()->back(); + const CoordinateXY& start = curves[i]->getCoordinatesRO()->front(); + + if (start != end) { + std::ostringstream ss; + + ss << "Sections of CompoundCurve are not contiguous: " << + "curve " << (i-1) << " ends at " << end << + " ; curve " << i << " begins at " << start; + + throw util::IllegalArgumentException(ss.str()); + } + } +} + } } diff --git a/tests/unit/geom/CompoundCurveTest.cpp b/tests/unit/geom/CompoundCurveTest.cpp index a430842fa7..958cf109e1 100644 --- a/tests/unit/geom/CompoundCurveTest.cpp +++ b/tests/unit/geom/CompoundCurveTest.cpp @@ -11,8 +11,10 @@ #include #include +using geos::geom::CompoundCurve; using geos::geom::CoordinateXY; using geos::geom::CoordinateSequence; +using geos::geom::SimpleCurve; namespace tut { // Common data used by tests @@ -21,11 +23,11 @@ struct test_compoundcurve_data { geos::geom::GeometryFactory::Ptr factory_ = geos::geom::GeometryFactory::create(); geos::io::WKTReader wktreader_; - std::unique_ptr cc_; + std::unique_ptr cc_; test_compoundcurve_data() { - std::vector> curves; + std::vector> curves; curves.emplace_back(factory_->createCircularString({ CoordinateXY(0, 0), @@ -312,6 +314,17 @@ void object::test<8>() ensure_equals(tcsf.args[3].second, 0u); } +template<> +template<> +void object::test<9>() +{ + std::vector> curves; + + curves.push_back(wktreader_.read("LINESTRING (0 0, 1 2)")); + curves.push_back(wktreader_.read("CIRCULARSTRING (2 1, 3 3, 4 1)")); + + ensure_THROW(factory_->createCompoundCurve(std::move(curves)), geos::util::IllegalArgumentException); +} } diff --git a/tests/unit/io/WKTWriterTest.cpp b/tests/unit/io/WKTWriterTest.cpp index 8618f8be03..4622e9b988 100644 --- a/tests/unit/io/WKTWriterTest.cpp +++ b/tests/unit/io/WKTWriterTest.cpp @@ -763,9 +763,9 @@ void object::test<22>() geom = wktreader.read("CIRCULARSTRING (0 0, 1 1, 2 0)"); ensure_equals(wktwriter.writeFormatted(geom.get()), "CIRCULARSTRING (0 0, 1 1, 2 0)"); - geom = wktreader.read("COMPOUNDCURVE((0 10, 0 5), CIRCULARSTRING (0 0, 1 1, 2 0), (2 0, 3 0))"); + geom = wktreader.read("COMPOUNDCURVE((0 10, 0 5), CIRCULARSTRING (0 5, 1 1, 2 0), (2 0, 3 0))"); ensure_equals(wktwriter.writeFormatted(geom.get()), "COMPOUNDCURVE ((0 10, 0 5), \n" - " CIRCULARSTRING (0 0, 1 1, 2 0), \n" + " CIRCULARSTRING (0 5, 1 1, 2 0), \n" " (2 0, 3 0))"); geom = wktreader.read("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1), (3 3, 3 4, 4 4, 4 3, 3 3))"); @@ -786,9 +786,9 @@ void object::test<22>() " (2 2, 3 3), \n" " (4 4, 5 5))"); - geom = wktreader.read("MULTICURVE ((0 0, 1 1), COMPOUNDCURVE ((2 2, 3 3), CIRCULARSTRING (4 4, 5 5, 6 4), (6 4, 7 4)), (100 100, 200 200))"); + geom = wktreader.read("MULTICURVE ((0 0, 1 1), COMPOUNDCURVE ((2 2, 4 4), CIRCULARSTRING (4 4, 5 5, 6 4), (6 4, 7 4)), (100 100, 200 200))"); ensure_equals(wktwriter.writeFormatted(geom.get()), "MULTICURVE ((0 0, 1 1), \n" - " COMPOUNDCURVE ((2 2, 3 3), \n" + " COMPOUNDCURVE ((2 2, 4 4), \n" " CIRCULARSTRING (4 4, 5 5, 6 4), \n" " (6 4, 7 4)), \n" " (100 100, 200 200))");