Skip to content

Commit

Permalink
fix: GeographyCollection::Decode recount number of shapes (#55)
Browse files Browse the repository at this point in the history
Closes #54.
  • Loading branch information
benbovy authored Dec 8, 2024
1 parent 9ebe016 commit c7a0197
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/s2geography/geography.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ void GeographyCollection::Decode(Decoder* decoder, const EncodeTag& tag) {
for (uint32_t i = 0; i < n_features; i++) {
features_.push_back(Geography::DecodeTagged(decoder));
}

CountShapes();
}

namespace {
Expand Down
12 changes: 8 additions & 4 deletions src/s2geography/geography.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ class GeographyCollection : public Geography {
: Geography(GeographyKind::GEOGRAPHY_COLLECTION),
features_(std::move(features)),
total_shapes_(0) {
for (const auto& feature : features_) {
num_shapes_.push_back(feature->num_shapes());
total_shapes_ += feature->num_shapes();
}
CountShapes();
}

int num_shapes() const;
Expand All @@ -235,6 +232,13 @@ class GeographyCollection : public Geography {
std::vector<std::unique_ptr<Geography>> features_;
std::vector<int> num_shapes_;
int total_shapes_;

inline void CountShapes() {
for (const auto& feature : features_) {
num_shapes_.push_back(feature->num_shapes());
total_shapes_ += feature->num_shapes();
}
}
};

// A Geography with a MutableS2ShapeIndex as the underlying data.
Expand Down
4 changes: 4 additions & 0 deletions src/s2geography/geography_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,16 @@ TEST(Geography, EncodedGeographyCollection) {
std::vector<std::unique_ptr<Geography>> child_geogs;
child_geogs.emplace_back(child_geog.release());
GeographyCollection geog(std::move(child_geogs));
ASSERT_EQ(geog.num_shapes(), 1);
ASSERT_EQ(geog.dimension(), 0);
geog.EncodeTagged(&encoder, EncodeOptions());

Decoder decoder(encoder.base(), encoder.length());
auto roundtrip = Geography::DecodeTagged(&decoder);
ASSERT_EQ(roundtrip->kind(), GeographyKind::GEOGRAPHY_COLLECTION);
ASSERT_THAT(*roundtrip, WktEquals6("GEOMETRYCOLLECTION (POINT (-64 45))"));
ASSERT_EQ(roundtrip->num_shapes(), 1);
ASSERT_EQ(roundtrip->dimension(), 0);

auto roundtrip_typed =
reinterpret_cast<GeographyCollection*>(roundtrip.get());
Expand Down

0 comments on commit c7a0197

Please sign in to comment.