Skip to content

Commit

Permalink
fix nested geography collection
Browse files Browse the repository at this point in the history
  • Loading branch information
benbovy committed Oct 15, 2024
1 parent 587f41c commit 43790b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/geography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<s2geog::Geography> clone_s2geography(const s2geog::Geography &geog);

std::unique_ptr<s2geog::Geography> clone_s2geography(const s2geog::Geography &geog,
GeographyType geog_type) {
std::unique_ptr<s2geog::Geography> new_geog_ptr;
Expand Down Expand Up @@ -68,15 +70,28 @@ std::unique_ptr<s2geog::Geography> 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<s2geog::GeographyCollection>(std::move(features_copy));
}

return new_geog_ptr;
}

std::unique_ptr<s2geog::Geography> clone_s2geography(const s2geog::Geography &geog) {
if (const auto *ptr = dynamic_cast<const s2geog::PointGeography *>(&geog); ptr) {
return clone_s2geography(geog, GeographyType::Point);
} else if (const auto *ptr = dynamic_cast<const s2geog::PolylineGeography *>(&geog); ptr) {
return clone_s2geography(geog, GeographyType::LineString);
} else if (const auto *ptr = dynamic_cast<const s2geog::PolygonGeography *>(&geog); ptr) {
return clone_s2geography(geog, GeographyType::Polygon);
} else if (const auto *ptr = dynamic_cast<const s2geog::GeographyCollection *>(&geog); ptr) {
return clone_s2geography(geog, GeographyType::GeographyCollection);
} else {
throw py::type_error("unknown geography type");
}
}

/*
** Geography implementation
*/
Expand Down
10 changes: 5 additions & 5 deletions tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43790b1

Please sign in to comment.