Skip to content

feat: Add iteration utilities for iterating over native arrays #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jan 5, 2025

Conversation

paleolimbot
Copy link
Contributor

@paleolimbot paleolimbot commented Jan 2, 2025

This PR adds minimal STL iterators that can be used to iterate over GeoArrow memory. The idea is to allow forward iteration from C++ (as opposed to visitor-style iteration from C or C++). An example from the tests:

    geoarrow::ArrayReader reader(type);
    reader.SetArray(&array);

    geoarrow::array::MultiPolygonArray<XY> native_array;
    ASSERT_EQ(native_array.Init(reader.View().array_view()), GEOARROW_OK);

    std::vector<std::vector<std::vector<std::vector<XY>>>> multipolygons;
    for (const auto& multipolygon : native_array.value) {
      std::vector<std::vector<std::vector<XY>>> polygons;
      for (const auto& polygon : multipolygon) {
        std::vector<std::vector<XY>> rings;
        for (const auto& ring : polygon) {
          std::vector<XY> coords;
          for (const auto& coord : ring) {
            coords.push_back(coord);
          }
          rings.push_back(std::move(coords));
        }
        polygons.push_back(std::move(rings));
      }
      multipolygons.push_back(std::move(polygons));
    }

I think the iterators need a few more overloads to be truly random access iterators, but the idea is there and they can be improved with time.

All of these are currently "just structs" intended for very low-level iteration (not necessary a pretty user-facing interface).

Comment on lines 204 to 225
geoarrow::ArrayReader reader(type);
reader.SetArray(&array);

geoarrow::array::MultiPolygonArray<XY> native_array;
ASSERT_EQ(native_array.Init(reader.View().array_view()), GEOARROW_OK);

std::vector<std::vector<std::vector<std::vector<XY>>>> multipolygons;
for (const auto& multipolygon : native_array.value) {
std::vector<std::vector<std::vector<XY>>> polygons;
for (const auto& polygon : multipolygon) {
std::vector<std::vector<XY>> rings;
for (const auto& ring : polygon) {
std::vector<XY> coords;
for (const auto& coord : ring) {
coords.push_back(coord);
}
rings.push_back(std::move(coords));
}
polygons.push_back(std::move(rings));
}
multipolygons.push_back(std::move(polygons));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benbovy I think this will get what we need for paleolimbot/s2geography#56 !

@paleolimbot paleolimbot marked this pull request as ready for review January 4, 2025 06:27
@paleolimbot paleolimbot merged commit b926c18 into geoarrow:main Jan 5, 2025
9 checks passed
@paleolimbot paleolimbot deleted the iteration-cxx branch January 5, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant