Merge pull request #336 from mapycz/fix-centroid-crash

[algorithms][centroid] Fix crash when multi-geometry contains empty geometry
This commit is contained in:
Barend Gehrels
2015-11-22 14:15:32 +01:00
4 changed files with 100 additions and 10 deletions

View File

@@ -175,6 +175,21 @@ void test_exceptions()
test_centroid_exception<bg::model::linestring<P> >();
test_centroid_exception<bg::model::polygon<P> >();
test_centroid_exception<bg::model::ring<P> >();
// Empty exterior ring
test_centroid_exception<bg::model::polygon<P> >(
"POLYGON((), ())");
test_centroid_exception<bg::model::polygon<P> >(
"POLYGON((), (0 0, 1 0, 1 1, 0 1, 0 0))");
}
template <typename P>
void test_empty()
{
// Empty interior ring
test_centroid<bg::model::polygon<P> >(
"POLYGON((0 0, 1 0, 1 1, 0 1, 0 0), ())",
0.5, 0.5);
}
void test_large_integers()
@@ -255,6 +270,7 @@ int test_main(int, char* [])
test_large_doubles();
test_exceptions<bg::model::d2::point_xy<double> >();
test_empty<bg::model::d2::point_xy<double> >();
return 0;
}

View File

@@ -115,6 +115,58 @@ void test_2d(bool is_integer = false)
424530.6059719588, 4527519.619367547);
}
template <typename P>
void test_exceptions()
{
using namespace bg::model;
typedef multi_polygon<polygon<P> > multi_polygon;
typedef multi_linestring<linestring<P> > multi_linestring;
// Empty multi-polygon
test_centroid_exception<multi_polygon>("MULTIPOLYGON()");
test_centroid_exception<multi_polygon>("MULTIPOLYGON(())");
test_centroid_exception<multi_polygon>("MULTIPOLYGON((), ())");
test_centroid_exception<multi_polygon>("MULTIPOLYGON((()), ())");
test_centroid_exception<multi_polygon>("MULTIPOLYGON(((), ()))");
// Empty multi-linestring
test_centroid_exception<multi_linestring>("MULTILINESTRING()");
test_centroid_exception<multi_linestring>("MULTILINESTRING(())");
test_centroid_exception<multi_linestring>("MULTILINESTRING((), ())");
}
template <typename P>
void test_empty()
{
using namespace bg::model;
typedef multi_polygon<polygon<P> > multi_polygon;
typedef multi_linestring<linestring<P> > multi_linestring;
// Multi-linestring with empty linestring
test_centroid<multi_linestring>(
"MULTILINESTRING((), (0 0))",
0.0, 0.0);
test_centroid<multi_linestring>(
"MULTILINESTRING((0 0, 1 0), ())",
0.5, 0.0);
// Multi-polygon with empty polygon
test_centroid<multi_polygon>(
"MULTIPOLYGON((()), ((0 0)))",
0.0, 0.0);
test_centroid<multi_polygon>(
"MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0)), (()))",
0.5, 0.5);
// Multi-polygon with empty interior ring
test_centroid<multi_polygon>(
"MULTIPOLYGON(((0 0, 1 0, 1 1, 0 1, 0 0), ()))",
0.5, 0.5);
test_centroid<multi_polygon>(
"MULTIPOLYGON((()), ((0 0, 1 0, 1 1, 0 1, 0 0), ()))",
0.5, 0.5);
}
int test_main(int, char* [])
@@ -129,5 +181,8 @@ int test_main(int, char* [])
test_2d<bg::model::d2::point_xy<ttmath_big> >();
#endif
test_exceptions<bg::model::d2::point_xy<double> >();
test_empty<bg::model::d2::point_xy<double> >();
return 0;
}

View File

@@ -126,5 +126,21 @@ void test_centroid_exception()
BOOST_CHECK_MESSAGE(false, "A centroid_exception should have been thrown" );
}
template <typename Geometry>
void test_centroid_exception(std::string const& wkt)
{
Geometry geometry;
bg::read_wkt(wkt, geometry);
try
{
typename bg::point_type<Geometry>::type c;
bg::centroid(geometry, c);
}
catch(bg::centroid_exception const& )
{
return;
}
BOOST_CHECK_MESSAGE(false, "A centroid_exception should have been thrown" );
}
#endif