Skip to content

Commit

Permalink
Fix line->getPoint(n) error with m coordinates GH-1191
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Nov 15, 2024
1 parent 286f189 commit 1ee3b0b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/geom/SimpleCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,18 @@ SimpleCurve::getPointN(std::size_t n) const
{
assert(getFactory());
assert(points.get());
return std::unique_ptr<Point>(getFactory()->createPoint(points->getAt(n)));

std::unique_ptr<Point> point;
if (hasM() || hasZ()) {
CoordinateXYZM c;
points->getAt(n, c);
return getFactory()->createPoint(c);
}
else {
CoordinateXY c;
points->getAt(n, c);
return getFactory()->createPoint(c);
}
}

std::unique_ptr<Point>
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/geom/LineStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct test_linestring_data {
geos::geom::PrecisionModel pm_;
geos::geom::GeometryFactory::Ptr factory_;
geos::io::WKTReader reader_;
geos::io::WKTWriter writer_;

std::unique_ptr<geos::geom::LineString> empty_line_;
std::unique_ptr<geos::geom::LineString> line_;
Expand Down Expand Up @@ -588,5 +589,50 @@ void object::test<32>
ensure(!line_->hasDimension(geos::geom::Dimension::A));
}

// https://github.com/libgeos/geos/issues/1191
// line->getPoint(n) loses M dimension
template<>
template<>
void object::test<33>
()
{
auto geom = reader_.read("LINESTRING M (0 1 2, 10 11 12, 20 21 22)");
ensure(geom != nullptr);
geos::geom::LineString *line = static_cast<LineString*>(geom.get());
ensure_equals(line->getCoordinateDimension(), 3);
auto pt = line->getPointN(2);
auto out = writer_.write(*pt);
ensure_equals(out, "POINT M (20 21 22)");
}

template<>
template<>
void object::test<34>
()
{
auto geom = reader_.read("LINESTRING Z (0 1 2, 10 11 12, 20 21 22)");
ensure(geom != nullptr);
geos::geom::LineString *line = static_cast<LineString*>(geom.get());
ensure_equals(line->getCoordinateDimension(), 3);
auto pt = line->getPointN(2);
auto out = writer_.write(*pt);
ensure_equals(out, "POINT Z (20 21 22)");
}

template<>
template<>
void object::test<35>
()
{
auto geom = reader_.read("LINESTRING ZM (0 1 2 3, 10 11 12 13, 20 21 22 23)");
ensure(geom != nullptr);
geos::geom::LineString *line = static_cast<LineString*>(geom.get());
ensure_equals(line->getCoordinateDimension(), 4);
auto pt = line->getPointN(2);
auto out = writer_.write(*pt);
ensure_equals(out, "POINT ZM (20 21 22 23)");
}


} // namespace tut

0 comments on commit 1ee3b0b

Please sign in to comment.