From 43790b1fc05154b403b105a690c8239f8e7ef4ae Mon Sep 17 00:00:00 2001 From: Benoit Bovy Date: Tue, 15 Oct 2024 21:47:09 +0200 Subject: [PATCH] fix nested geography collection --- src/geography.cpp | 19 +++++++++++++++++-- tests/test_creation.py | 10 +++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/geography.cpp b/src/geography.cpp index 7287c8a..f2e37b3 100644 --- a/src/geography.cpp +++ b/src/geography.cpp @@ -34,6 +34,8 @@ py::detail::type_info *PyObjectGeography::geography_tinfo = nullptr; */ // TODO: May be worth moving this upstream as a `s2geog::Geography::clone()` virtual function +std::unique_ptr clone_s2geography(const s2geog::Geography &geog); + std::unique_ptr clone_s2geography(const s2geog::Geography &geog, GeographyType geog_type) { std::unique_ptr new_geog_ptr; @@ -68,15 +70,28 @@ std::unique_ptr clone_s2geography(const s2geog::Geography &ge features_copy.reserve(features.size()); for (const auto &feature_ptr : features) { - features_copy.push_back(clone_s2geography(*feature_ptr, geog_type)); + features_copy.push_back(clone_s2geography(*feature_ptr)); } - new_geog_ptr = std::make_unique(std::move(features_copy)); } return new_geog_ptr; } +std::unique_ptr clone_s2geography(const s2geog::Geography &geog) { + if (const auto *ptr = dynamic_cast(&geog); ptr) { + return clone_s2geography(geog, GeographyType::Point); + } else if (const auto *ptr = dynamic_cast(&geog); ptr) { + return clone_s2geography(geog, GeographyType::LineString); + } else if (const auto *ptr = dynamic_cast(&geog); ptr) { + return clone_s2geography(geog, GeographyType::Polygon); + } else if (const auto *ptr = dynamic_cast(&geog); ptr) { + return clone_s2geography(geog, GeographyType::GeographyCollection); + } else { + throw py::type_error("unknown geography type"); + } +} + /* ** Geography implementation */ diff --git a/tests/test_creation.py b/tests/test_creation.py index 8276452..2cbb909 100644 --- a/tests/test_creation.py +++ b/tests/test_creation.py @@ -307,9 +307,9 @@ def test_collection() -> None: assert [o.nshape for o in objs] == [1, 1, 1] # test nested collection - # coll2 = spherely.geography_collection(objs + [coll]) + coll2 = spherely.geography_collection(objs + [coll]) - # assert repr(coll2).count("POINT") == 2 - # assert repr(coll2).count("LINESTRING") == 2 - # assert repr(coll2).count("POLYGON") == 2 - # assert repr(coll2).count("GEOMETRYCOLLECTION") == 2 + assert repr(coll2).count("POINT") == 2 + assert repr(coll2).count("LINESTRING") == 2 + assert repr(coll2).count("POLYGON") == 2 + assert repr(coll2).count("GEOMETRYCOLLECTION") == 2