Skip to content

Commit

Permalink
Fix PrecisionReducer bug in CoordinateSequence.add
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed Sep 21, 2023
1 parent bd633c6 commit b00e7d3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/geom/CoordinateSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,22 @@ CoordinateSequence::add(const CoordinateSequence& cs)
add(cs, 0, cs.size() - 1);
}

void
CoordinateSequence::add(const CoordinateSequence& cs, std::size_t from, std::size_t to, bool allowRepeated)
{
if (allowRepeated) {
add(cs, from, to);
return;
}
for (size_t i = from; i <= to; i++) {
if (size() == 0 || ! cs.getAt<CoordinateXY>(i).equals2D( back<CoordinateXY>())) {
add(cs, i, i);
}
}
}

/*
//-- this code has a bug in it (fails to add last coordinate in some cases)
void
CoordinateSequence::add(const CoordinateSequence& cs, std::size_t from, std::size_t to, bool allowRepeated)
{
Expand Down Expand Up @@ -217,6 +233,7 @@ CoordinateSequence::add(const CoordinateSequence& cs, std::size_t from, std::siz
add(cs, first, to);
}
}
//*/

void
CoordinateSequence::add(const CoordinateSequence& cs, bool allowRepeated) {
Expand Down
4 changes: 4 additions & 0 deletions src/noding/SegmentNodeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#if GEOS_DEBUG
#include <iostream>
#endif
#include <iomanip>

//
using namespace geos::geom;
Expand Down Expand Up @@ -312,8 +313,11 @@ SegmentNodeList::getSplitCoordinates()
void
SegmentNodeList::addEdgeCoordinates(const SegmentNode* ei0, const SegmentNode* ei1, CoordinateSequence& coordList) const {
auto pts = createSplitEdgePts(ei0, ei1);
//std::cout << std::endl << std::setprecision(16) << "addEdgeCoordinates: " << *pts << std::endl;
// Append pts to coordList
coordList.add(*pts, false);
//std::cout << "updated coord list: " << std::setprecision(16) << coordList << std::endl;

}


Expand Down
17 changes: 13 additions & 4 deletions tests/unit/operation/overlayng/PrecisionReducerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ struct test_precisionreducer_data {
WKTWriter w;

void
checkReduce(const std::string& wkt, double gridSize, const std::string& wkt_expected)
checkReduce(const std::string& wkt, double scale, const std::string& wkt_expected)
{
std::unique_ptr<Geometry> geom = r.read(wkt);
std::unique_ptr<Geometry> expected = r.read(wkt_expected);
PrecisionModel pm(1.0/gridSize);
PrecisionModel pm(scale);
std::unique_ptr<Geometry> result = PrecisionReducer::reducePrecision(geom.get(), &pm);
ensure_equals_geometry(result.get(), expected.get());
}
Expand Down Expand Up @@ -142,7 +142,7 @@ template<>
void object::test<11> ()
{
checkReduce("LINESTRING(-3 6, 9 1)",
2, "LINESTRING (-2 6, 10 2)");
0.5, "LINESTRING (-2 6, 10 2)");
}

// testCollapsedLine
Expand Down Expand Up @@ -170,9 +170,18 @@ void object::test<14> ()
{
checkReduce("POLYGON ((2 1, 3 1, 3 2, 2 1))",
// checkReduce("POLYGON ((0 0, 4 0, 4 4, 0 4, 0 0), (1 1, 1 2, 2 1, 1 1), (1 2, 1 3, 2 3, 1 2), (2 3, 3 3, 3 2, 2 3))",
1.0/10, "POLYGON ((2 1, 3 1, 3 2, 2 1))");
10, "POLYGON ((2 1, 3 1, 3 2, 2 1))");
}

// see https://github.com/libgeos/geos/issues/811
template<>
template<>
void object::test<15> ()
{
checkReduce("POLYGON ((127.117461568 34.562519572, 127.117483252 34.5624884690001, 127.117603304 34.562319127, 127.117607152 34.562312309, 127.117607012 34.562312359, 127.117254733 34.5621607510001, 127.117746661 34.5620659730001, 127.117603496 34.5623196400001, 127.117484065 34.562488982, 127.117462315 34.562520066, 127.117245225 34.562385186, 127.117461568 34.562519572))",
100000,
"POLYGON ((127.11775 34.56207, 127.11725 34.56216, 127.11761 34.56231, 127.11775 34.56207))");
}


} // namespace tut

0 comments on commit b00e7d3

Please sign in to comment.