Skip to content
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

feat: add scalar WKB reader/writer #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ add_library(s2geography
src/s2geography/geoarrow.cc
src/s2geography/wkt-reader.cc
src/s2geography/wkt-writer.cc
src/s2geography/wkb.cc
src/s2geography/op/cell.cc
src/s2geography/op/point.cc
# geoarrow sources
Expand Down Expand Up @@ -259,6 +260,7 @@ if(S2GEOGRAPHY_BUILD_TESTS)
add_executable(geoarrow_test src/s2geography/geoarrow_test.cc)
add_executable(geography_test src/s2geography/geography_test.cc)
add_executable(wkt_writer_test src/s2geography/wkt-writer_test.cc)
add_executable(wkb_test src/s2geography/wkb_test.cc)
add_executable(op_cell_test src/s2geography/op/cell_test.cc)

if (S2GEOGRAPHY_CODE_COVERAGE)
Expand All @@ -271,6 +273,7 @@ if(S2GEOGRAPHY_BUILD_TESTS)
target_link_libraries(geoarrow_test s2geography nanoarrow GTest::gtest_main GTest::gmock)
target_link_libraries(geography_test s2geography nanoarrow GTest::gtest_main)
target_link_libraries(wkt_writer_test s2geography GTest::gtest_main)
target_link_libraries(wkb_test s2geography GTest::gtest_main)
target_link_libraries(op_cell_test s2geography GTest::gtest_main)

target_include_directories(geoarrow_test PRIVATE src/vendored)
Expand All @@ -280,6 +283,7 @@ if(S2GEOGRAPHY_BUILD_TESTS)
gtest_discover_tests(geoarrow_test)
gtest_discover_tests(geography_test)
gtest_discover_tests(wkt_writer_test)
gtest_discover_tests(wkb_test)
gtest_discover_tests(op_cell_test)
endif()

Expand Down
1 change: 1 addition & 0 deletions src/s2geography.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
#include "s2geography/projections.h"
#include "s2geography/wkt-reader.h"
#include "s2geography/wkt-writer.h"
#include "s2geography/wkb.h"
67 changes: 67 additions & 0 deletions src/s2geography/wkb.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#include "s2geography/wkb.h"

#include <cstdlib>
#include <cstring>
#include <sstream>

#include "s2geography/geoarrow.h"
#include "s2geography/geography.h"

namespace s2geography {

WKBReader::WKBReader(const geoarrow::ImportOptions& options) {
reader_ = absl::make_unique<geoarrow::Reader>();
reader_->Init(geoarrow::Reader::InputType::kWKB, options);
}

std::unique_ptr<Geography> WKBReader::ReadFeature(const uint8_t* bytes,
int64_t size) {
if (size > std::numeric_limits<int32_t>::max()) {
throw Exception("Can't parse WKB greater than 2GB in size");
}

int32_t offsets[] = {0, static_cast<int32_t>(size)};
const void* buffers[] = {nullptr, offsets, bytes};
ArrowArray array;
array.length = 1;
array.null_count = 0;
array.offset = 0;

array.n_buffers = 3;
array.n_children = 0;
array.buffers = buffers;

array.children = nullptr;
array.dictionary = nullptr;
array.release = [](ArrowArray*) -> void {};
array.private_data = nullptr;

out_.clear();
reader_->ReadGeography(&array, 0, 1, &out_);
return std::move(out_[0]);
}

std::unique_ptr<Geography> WKBReader::ReadFeature(const std::string_view bytes) {
return ReadFeature(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size());
}

WKBWriter::WKBWriter(const geoarrow::ExportOptions& options) {
writer_ = absl::make_unique<geoarrow::Writer>();
writer_->Init(geoarrow::Writer::OutputType::kWKB, options);

}

std::string WKBWriter::WriteFeature(const Geography& geog) {
ArrowArray array;
writer_->WriteGeography(geog);
writer_->Finish(&array);

const auto offsets = static_cast<const int32_t*>(array.buffers[1]);
const auto data = static_cast<const uint8_t*>(array.buffers[2]);
std::string result(reinterpret_cast<const char*>(data), offsets[1]);

return result;
}

} // namespace s2geography
33 changes: 33 additions & 0 deletions src/s2geography/wkb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#pragma once

#include "s2geography/geoarrow.h"
#include "s2geography/geography.h"

namespace s2geography {

class WKBReader {
public:
WKBReader() : WKBReader(geoarrow::ImportOptions()) {}
WKBReader(const geoarrow::ImportOptions& options);

std::unique_ptr<Geography> ReadFeature(const uint8_t* bytes, int64_t size);
std::unique_ptr<Geography> ReadFeature(const std::string_view bytes);

private:
std::unique_ptr<geoarrow::Reader> reader_;
std::vector<std::unique_ptr<Geography>> out_;
};

class WKBWriter {
public:
WKBWriter() : WKBWriter(geoarrow::ExportOptions()) {}
WKBWriter(const geoarrow::ExportOptions& options);

std::string WriteFeature(const Geography& geog);

private:
std::unique_ptr<geoarrow::Writer> writer_;
};

} // namespace s2geography
75 changes: 75 additions & 0 deletions src/s2geography/wkb_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#include <gtest/gtest.h>

#include "s2geography.h"

using namespace s2geography;

void wkbRoundTrip(const std::string wktIn,
const std::vector<uint8_t> &wkbIn) {
WKTWriter wkt_writer(2);
WKBReader reader;
WKBWriter writer;

auto geog = reader.ReadFeature(wkbIn.data(), wkbIn.size());
auto wkbOut = writer.WriteFeature(*geog);
auto geogOut = reader.ReadFeature(wkbOut);
std::string wktOut = wkt_writer.write_feature(*geogOut);
EXPECT_EQ(wktOut, wktIn);
}

jorisvandenbossche marked this conversation as resolved.
Show resolved Hide resolved
TEST(WKBRoundtrip, Point) {
std::string wkt("POINT (30 10)");
std::vector<uint8_t> wkb({0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3e, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40});
wkbRoundTrip(wkt, wkb);
}

TEST(WKBRoundtrip, LineString) {
std::string wkt("LINESTRING (30 10, 12 42)");
std::vector<uint8_t> wkb(
{0x01, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3e, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x40});
wkbRoundTrip(wkt, wkb);
}

TEST(WKBRoundtrip, Polygon) {
std::string wkt(
"POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 "
"30))");
std::vector<uint8_t> wkb(
{0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x41, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x24, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x46,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x46, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x2e, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x34, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x41,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40, 0x04, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3e, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x41,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x41, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3e, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3e, 0x40});
wkbRoundTrip(wkt, wkb);
}

TEST(WKBRoundtrip, ExportOptions) {
s2geography::WKTReader reader;
auto geog = reader.read_feature("LINESTRING (-64 45, 0 45)");

WKBWriter writer;
auto wkbOut = writer.WriteFeature(*geog);

// smoke test for passing through options: with tessellation -> more coordinates
s2geography::geoarrow::ExportOptions options;
options.set_tessellate_tolerance(S1Angle::Radians(0.001));
WKBWriter writer2(options);
auto wkbOut2 = writer2.WriteFeature(*geog);

EXPECT_GT(wkbOut2.size(), wkbOut.size());
}
Loading