From 8d2ca4bf560b58431f1e09770d9f8f2fc85a2bde Mon Sep 17 00:00:00 2001 From: Dewey Dunnington Date: Tue, 8 Oct 2024 22:32:37 -0500 Subject: [PATCH] add new class methods --- src/s2geography/geoarrow.cc | 85 ++++++++++++++++++++++++-------- src/s2geography/geoarrow.h | 27 +++++++--- src/s2geography/geoarrow_test.cc | 15 ++++-- src/s2geography/wkt-reader.cc | 2 +- src/s2geography/wkt-reader.h | 4 +- 5 files changed, 98 insertions(+), 35 deletions(-) diff --git a/src/s2geography/geoarrow.cc b/src/s2geography/geoarrow.cc index 72c4859..ba1fb27 100644 --- a/src/s2geography/geoarrow.cc +++ b/src/s2geography/geoarrow.cc @@ -63,7 +63,7 @@ static inline int8_t GeoArrowBitmapReaderNextIsNull( class Constructor { public: - Constructor(const ImportOptions& options) : options_(options) { + Constructor(const ImportExportOptions& options) : options_(options) { if (options.projection() != nullptr) { this->tessellator_ = absl::make_unique( options.projection(), options.tessellate_tolerance()); @@ -121,7 +121,7 @@ class Constructor { protected: std::vector input_points_; std::vector points_; - ImportOptions options_; + ImportExportOptions options_; std::unique_ptr tessellator_; void finish_points() { @@ -234,7 +234,7 @@ class Constructor { class PointConstructor : public Constructor { public: - PointConstructor(const ImportOptions& options) : Constructor(options) {} + PointConstructor(const ImportExportOptions& options) : Constructor(options) {} GeoArrowErrorCode geom_start(GeoArrowGeometryType geometry_type, int64_t size) override { @@ -297,7 +297,8 @@ class PointConstructor : public Constructor { class PolylineConstructor : public Constructor { public: - PolylineConstructor(const ImportOptions& options) : Constructor(options) {} + PolylineConstructor(const ImportExportOptions& options) + : Constructor(options) {} GeoArrowErrorCode geom_start(GeoArrowGeometryType geometry_type, int64_t size) override { @@ -357,7 +358,8 @@ class PolylineConstructor : public Constructor { class PolygonConstructor : public Constructor { public: - PolygonConstructor(const ImportOptions& options) : Constructor(options) {} + PolygonConstructor(const ImportExportOptions& options) + : Constructor(options) {} GeoArrowErrorCode ring_start(int64_t size) override { input_points_.clear(); @@ -424,7 +426,7 @@ class PolygonConstructor : public Constructor { class CollectionConstructor : public Constructor { public: - CollectionConstructor(const ImportOptions& options) + CollectionConstructor(const ImportExportOptions& options) : Constructor(options), point_constructor_(options), polyline_constructor_(options), @@ -523,7 +525,7 @@ class CollectionConstructor : public Constructor { class FeatureConstructor : public CollectionConstructor { public: - FeatureConstructor(const ImportOptions& options) + FeatureConstructor(const ImportExportOptions& options) : CollectionConstructor(options) {} void SetOutput(std::vector>* out) { out_ = out; } @@ -591,33 +593,35 @@ class ReaderImpl { } } - void Init(const ArrowSchema* schema, const ImportOptions& options) { + void Init(const ArrowSchema* schema, const ImportExportOptions& options) { options_ = options; int code = GeoArrowArrayViewInitFromSchema(&array_view_, schema, &error_); ThrowNotOk(code); - InitCommon(); + code = InitCommon(); + ThrowNotOk(code); } - void Init(GeoArrowType type, const ImportOptions& options) { + void Init(GeoArrowType type, const ImportExportOptions& options) { options_ = options; int code = GeoArrowArrayViewInitFromType(&array_view_, type); ThrowNotOk(code); - InitCommon(); + code = InitCommon(); + ThrowNotOk(code); } - void InitCommon() { + GeoArrowErrorCode InitCommon() { constructor_ = absl::make_unique(options_); constructor_->InitVisitor(&visitor_); visitor_.error = &error_; if (array_view_.schema_view.type == GEOARROW_TYPE_WKT) { - GeoArrowWKTReaderInit(&wkt_reader_); - } - - if (array_view_.schema_view.type == GEOARROW_TYPE_WKB) { - GeoArrowWKBReaderInit(&wkb_reader_); + return GeoArrowWKTReaderInit(&wkt_reader_); + } else if (array_view_.schema_view.type == GEOARROW_TYPE_WKB) { + return GeoArrowWKBReaderInit(&wkb_reader_); + } else { + return GEOARROW_OK; } } @@ -648,7 +652,7 @@ class ReaderImpl { } private: - ImportOptions options_; + ImportExportOptions options_; std::unique_ptr constructor_; GeoArrowArrayView array_view_; GeoArrowWKTReader wkt_reader_; @@ -717,13 +721,14 @@ class ReaderImpl { Reader::Reader() : impl_(new ReaderImpl()) {} -Reader::~Reader() { impl_.reset(); } +Reader::~Reader() {} -void Reader::Init(const ArrowSchema* schema, const ImportOptions& options) { +void Reader::Init(const ArrowSchema* schema, + const ImportExportOptions& options) { impl_->Init(schema, options); } -void Reader::Init(InputType input_type, const ImportOptions& options) { +void Reader::Init(InputType input_type, const ImportExportOptions& options) { switch (input_type) { case InputType::kWKT: impl_->Init(GEOARROW_TYPE_WKT, options); @@ -742,6 +747,44 @@ void Reader::ReadGeography(const ArrowArray* array, int64_t offset, impl_->ReadGeography(array, offset, length, out); } +class WriterImpl { + public: + WriterImpl() { builder_.private_data = nullptr; } + + ~WriterImpl() { + if (builder_.private_data != nullptr) { + GeoArrowBuilderReset(&builder_); + } + } + + GeoArrowErrorCode Init(const ArrowSchema* schema, + const ImportExportOptions& options) { + options_ = options; + + GEOARROW_RETURN_NOT_OK( + GeoArrowBuilderInitFromSchema(&builder_, schema, &error_)); + + return GEOARROW_OK; + } + + private: + ImportExportOptions options_; + GeoArrowBuilder builder_; + GeoArrowError error_; + + void ThrowNotOk(int code) { + if (code != GEOARROW_OK) { + throw Exception(error_.message); + } + } +}; + +Writer::Writer() : impl_(new WriterImpl()) {} + +Writer::~Writer() {} + +void Writer::Init(const ArrowSchema* schema, + const ImportExportOptions& options) {} } // namespace geoarrow } // namespace s2geography diff --git a/src/s2geography/geoarrow.h b/src/s2geography/geoarrow.h index cef0010..8eca303 100644 --- a/src/s2geography/geoarrow.h +++ b/src/s2geography/geoarrow.h @@ -18,10 +18,10 @@ const char* version(); S2::Projection* lnglat(); -/// \brief Options used to build Geography objects from GeoArrow arrays -class ImportOptions { +/// \brief Options used to import/export Geography objects from/to GeoArrow arrays +class ImportExportOptions { public: - ImportOptions() + ImportExportOptions() : oriented_(false), check_(true), projection_(lnglat()), @@ -52,11 +52,11 @@ class Reader { Reader(); ~Reader(); - void Init(const ArrowSchema* schema) { Init(schema, ImportOptions()); } + void Init(const ArrowSchema* schema) { Init(schema, ImportExportOptions()); } - void Init(const ArrowSchema* schema, const ImportOptions& options); + void Init(const ArrowSchema* schema, const ImportExportOptions& options); - void Init(InputType input_type, const ImportOptions& options); + void Init(InputType input_type, const ImportExportOptions& options); void ReadGeography(const ArrowArray* array, int64_t offset, int64_t length, std::vector>* out); @@ -65,6 +65,21 @@ class Reader { std::unique_ptr impl_; }; +class WriterImpl; + +class Writer { + public: + enum class OutputType { kWKT, kWKB }; + + Writer(); + ~Writer(); + + void Init(const ArrowSchema* schema, const ImportExportOptions& options); + + private: + std::unique_ptr impl_; +}; + } // namespace geoarrow } // namespace s2geography diff --git a/src/s2geography/geoarrow_test.cc b/src/s2geography/geoarrow_test.cc index ebc7543..d0d7d57 100644 --- a/src/s2geography/geoarrow_test.cc +++ b/src/s2geography/geoarrow_test.cc @@ -92,7 +92,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKTPoint) { InitArrayWKT(array.get(), {"POINT (0 1)", {}}); - reader.Init(Reader::InputType::kWKT, s2geography::geoarrow::ImportOptions()); + reader.Init(Reader::InputType::kWKT, + s2geography::geoarrow::ImportExportOptions()); reader.ReadGeography(array.get(), 0, array->length, &result); EXPECT_EQ(result[0]->dimension(), 0); ASSERT_EQ(result.size(), 2); @@ -106,7 +107,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKBPoint) { nanoarrow::UniqueArray array; std::vector> result; - reader.Init(Reader::InputType::kWKB, s2geography::geoarrow::ImportOptions()); + reader.Init(Reader::InputType::kWKB, + s2geography::geoarrow::ImportExportOptions()); InitArrayWKB(array.get(), {{0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0x40}, @@ -146,7 +148,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKTLinestring) { InitArrayWKT(array.get(), {"LINESTRING (0 1, 2 3)"}); - reader.Init(Reader::InputType::kWKT, s2geography::geoarrow::ImportOptions()); + reader.Init(Reader::InputType::kWKT, + s2geography::geoarrow::ImportExportOptions()); reader.ReadGeography(array.get(), 0, array->length, &result); EXPECT_EQ(result[0]->dimension(), 1); ASSERT_EQ(result.size(), 1); @@ -163,7 +166,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKTPolygon) { InitArrayWKT(array.get(), {"POLYGON ((0 0, 1 0, 0 1, 0 0))"}); - reader.Init(Reader::InputType::kWKT, s2geography::geoarrow::ImportOptions()); + reader.Init(Reader::InputType::kWKT, + s2geography::geoarrow::ImportExportOptions()); reader.ReadGeography(array.get(), 0, array->length, &result); EXPECT_EQ(result[0]->dimension(), 2); ASSERT_EQ(result.size(), 1); @@ -185,7 +189,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKTCollection) { InitArrayWKT(array.get(), {"GEOMETRYCOLLECTION (POINT (0 1), LINESTRING (0 1, 2 3))"}); - reader.Init(Reader::InputType::kWKT, s2geography::geoarrow::ImportOptions()); + reader.Init(Reader::InputType::kWKT, + s2geography::geoarrow::ImportExportOptions()); reader.ReadGeography(array.get(), 0, array->length, &result); EXPECT_EQ(result[0]->dimension(), -1); ASSERT_EQ(result.size(), 1); diff --git a/src/s2geography/wkt-reader.cc b/src/s2geography/wkt-reader.cc index d3112fa..31dfe6b 100644 --- a/src/s2geography/wkt-reader.cc +++ b/src/s2geography/wkt-reader.cc @@ -10,7 +10,7 @@ namespace s2geography { -WKTReader::WKTReader(const geoarrow::ImportOptions& options) { +WKTReader::WKTReader(const geoarrow::ImportExportOptions& options) { reader_ = absl::make_unique(); reader_->Init(geoarrow::Reader::InputType::kWKT, options); } diff --git a/src/s2geography/wkt-reader.h b/src/s2geography/wkt-reader.h index f3c5d19..72cf876 100644 --- a/src/s2geography/wkt-reader.h +++ b/src/s2geography/wkt-reader.h @@ -8,8 +8,8 @@ namespace s2geography { class WKTReader { public: - WKTReader() : WKTReader(geoarrow::ImportOptions()) {} - WKTReader(const geoarrow::ImportOptions& options); + WKTReader() : WKTReader(geoarrow::ImportExportOptions()) {} + WKTReader(const geoarrow::ImportExportOptions& options); std::unique_ptr read_feature(const char* text, int64_t size); std::unique_ptr read_feature(const char* text); std::unique_ptr read_feature(const std::string& str);