Skip to content

Commit

Permalink
Fix LineString->getPoint(n) for M geometries (closes GH-1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Nov 15, 2024
1 parent 0402c86 commit bf8a953
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- DouglasPeuckerLineSimplifier, avoid crash with Point input and NaN tolerance (GH-1078, Dan Baston)
- GEOSLineSubstring, avoid crash with NaN length fraction (GH-1077, Dan Baston)
- MinimumClearance, avoid crash on NaN inputs (GH-1079, Dan Baston)
- Fix LineString->getPoint(n) for M geometries (GH-1191, @hsieyuan)


## Changes in 3.12.2
2024-06-05
Expand Down
11 changes: 10 additions & 1 deletion src/geom/LineString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,16 @@ LineString::getPointN(std::size_t n) const
{
assert(getFactory());
assert(points.get());
return std::unique_ptr<Point>(getFactory()->createPoint(points->getAt(n)));
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
20 changes: 20 additions & 0 deletions tests/unit/geom/LineStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/util/GEOSException.h>
#include <geos/util/IllegalArgumentException.h>
#include <geos/constants.h>
Expand All @@ -35,6 +36,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 +590,23 @@ 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)");
}


} // namespace tut

0 comments on commit bf8a953

Please # to comment.