Skip to content

Commit

Permalink
Fix segfault in SimplePointInAreaLocator caused by casting MultiPolyg…
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeerbhola committed Sep 2, 2020
1 parent 385ac9b commit 4a98497
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/algorithm/locate/SimplePointInAreaLocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ SimplePointInAreaLocator::locateInGeometry(const Coordinate& p, const Geometry*
}

if (geom->getNumGeometries() == 1) {
return locatePointInPolygon(p, dynamic_cast<const Polygon*>(geom->getGeometryN(0)));
} else {
for (size_t i = 0; i < geom->getNumGeometries(); i++) {
const Geometry* gi = geom->getGeometryN(i);

auto loc = locateInGeometry(p, gi);
if(loc != Location::EXTERIOR) {
return loc;
}
auto poly = dynamic_cast<const Polygon*>(geom->getGeometryN(0));
if (poly) {
return locatePointInPolygon(p, poly);
}
// Else it is a collection with a single element. Will be handled below.
}
for (size_t i = 0; i < geom->getNumGeometries(); i++) {
const Geometry* gi = geom->getGeometryN(i);
auto loc = locateInGeometry(p, gi);
if(loc != Location::EXTERIOR) {
return loc;
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/unit/algorithm/PointLocatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <tut/tut.hpp>
// geos
#include <geos/io/WKTReader.h>
#include <geos/algorithm/locate/SimplePointInAreaLocator.h>
#include <geos/algorithm/PointLocator.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/geom/GeometryFactory.h>
Expand Down Expand Up @@ -56,6 +57,14 @@ runPtLocator(Location expected, const Coordinate& pt,
ensure_equals(loc, expected);
}

void
runSimplePtLocator(Location expected, const Coordinate& pt,
const std::string& wkt)
{
GeomPtr geom(reader.read(wkt));
Location loc = geos::algorithm::locate::SimplePointInAreaLocator::locate(pt, geom.get());
ensure_equals(loc, expected);
}

//
// Test Cases
Expand Down Expand Up @@ -111,5 +120,15 @@ void object::test<5>
"MULTIPOINT ((1 1), (0 0))");
}

// 6 - TestPointLocator Point inside GeometryCollection containing MultiPolygon, using SimplePointInAreaLocator.
template<>
template<>
void object::test<6>
()
{
runSimplePtLocator(Location::INTERIOR, Coordinate(0, 0),
"GEOMETRYCOLLECTION (MULTIPOLYGON (((-1 -1, 1 -1, 1 1, -1 1, -1 -1))))");
}

} // namespace tut

0 comments on commit 4a98497

Please sign in to comment.