Skip to content

Commit

Permalink
CompoundCurve: Add validateConstruction method (#1164)
Browse files Browse the repository at this point in the history
Resolves #1103
  • Loading branch information
dbaston authored Sep 24, 2024
1 parent 0d604cc commit bf2257a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/geos/geom/CompoundCurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class GEOS_DLL CompoundCurve : public Curve {

std::unique_ptr<CompoundCurve> reverse() const;

void validateConstruction() const;

protected:
/// Construct a CompoundCurve, taking ownership of the
/// provided CoordinateSequence
Expand Down
25 changes: 24 additions & 1 deletion src/geom/CompoundCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
**********************************************************************/

#include <sstream>

#include <geos/geom/CompoundCurve.h>
#include <geos/geom/CoordinateFilter.h>
#include <geos/geom/GeometryFactory.h>
Expand All @@ -25,7 +27,9 @@ CompoundCurve::CompoundCurve(std::vector<std::unique_ptr<SimpleCurve>>&& p_curve
const GeometryFactory& gf)
: Curve(gf),
curves(std::move(p_curves)),
envelope(computeEnvelopeInternal()) {}
envelope(computeEnvelopeInternal()) {
validateConstruction();
}

CompoundCurve::CompoundCurve(const CompoundCurve& other)
: Curve(other),
Expand Down Expand Up @@ -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<CoordinateXY>();
const CoordinateXY& start = curves[i]->getCoordinatesRO()->front<CoordinateXY>();

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());
}
}
}

}
}
17 changes: 15 additions & 2 deletions tests/unit/geom/CompoundCurveTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
#include <geos/io/WKTReader.h>
#include <geos/util.h>

using geos::geom::CompoundCurve;
using geos::geom::CoordinateXY;
using geos::geom::CoordinateSequence;
using geos::geom::SimpleCurve;

namespace tut {
// Common data used by tests
Expand All @@ -21,11 +23,11 @@ struct test_compoundcurve_data {
geos::geom::GeometryFactory::Ptr factory_ = geos::geom::GeometryFactory::create();
geos::io::WKTReader wktreader_;

std::unique_ptr<geos::geom::CompoundCurve> cc_;
std::unique_ptr<CompoundCurve> cc_;

test_compoundcurve_data()
{
std::vector<std::unique_ptr<geos::geom::SimpleCurve>> curves;
std::vector<std::unique_ptr<SimpleCurve>> curves;

curves.emplace_back(factory_->createCircularString({
CoordinateXY(0, 0),
Expand Down Expand Up @@ -312,6 +314,17 @@ void object::test<8>()
ensure_equals(tcsf.args[3].second, 0u);
}

template<>
template<>
void object::test<9>()
{
std::vector<std::unique_ptr<SimpleCurve>> curves;

curves.push_back(wktreader_.read<SimpleCurve>("LINESTRING (0 0, 1 2)"));
curves.push_back(wktreader_.read<SimpleCurve>("CIRCULARSTRING (2 1, 3 3, 4 1)"));

ensure_THROW(factory_->createCompoundCurve(std::move(curves)), geos::util::IllegalArgumentException);
}

}

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/io/WKTWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))");
Expand All @@ -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))");
Expand Down

0 comments on commit bf2257a

Please sign in to comment.