Skip to content

Commit 159f834

Browse files
committed
Add support for area() on geometry collections
Simply add up the area of all member geometries.
1 parent 6c132c6 commit 159f834

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/geom-functions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ double area(geometry_t const &geom)
373373
for (auto const &polygon : geom.get<multipolygon_t>()) {
374374
total += get_polygon_area(polygon);
375375
}
376+
} else if (geom.is_collection()) {
377+
for (auto const &sgeom : geom.get<collection_t>()) {
378+
total += area(sgeom);
379+
}
376380
}
377381

378382
return std::abs(total);

tests/test-geom-collections.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ TEST_CASE("geometry collection with several geometries", "[NoDB]")
7272
REQUIRE(geometry_n(geom, 3) == geom::geometry_t{geom::point_t{2, 2}});
7373
}
7474

75+
TEST_CASE("geometry collection with polygon", "[NoDB]")
76+
{
77+
geom::geometry_t geom{geom::collection_t{}};
78+
auto &c = geom.get<geom::collection_t>();
79+
80+
c.add_geometry(geom::geometry_t{geom::point_t{1, 1}});
81+
c.add_geometry(geom::geometry_t{
82+
geom::polygon_t{geom::ring_t{{1, 1}, {1, 2}, {2, 2}, {2, 1}, {1, 1}}}});
83+
84+
REQUIRE(geometry_type(geom) == "GEOMETRYCOLLECTION");
85+
REQUIRE(num_geometries(geom) == 2);
86+
REQUIRE(area(geom) == Approx(1.0));
87+
REQUIRE(length(geom) == Approx(0.0));
88+
REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}});
89+
}
90+
7591
TEST_CASE("create_collection from OSM data", "[NoDB]")
7692
{
7793
test_buffer_t buffer;

0 commit comments

Comments
 (0)