Skip to content

Commit

Permalink
add new class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Oct 9, 2024
1 parent 829a0fe commit 8d2ca4b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 35 deletions.
85 changes: 64 additions & 21 deletions src/s2geography/geoarrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<S2EdgeTessellator>(
options.projection(), options.tessellate_tolerance());
Expand Down Expand Up @@ -121,7 +121,7 @@ class Constructor {
protected:
std::vector<S2Point> input_points_;
std::vector<S2Point> points_;
ImportOptions options_;
ImportExportOptions options_;
std::unique_ptr<S2EdgeTessellator> tessellator_;

void finish_points() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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<std::unique_ptr<Geography>>* out) { out_ = out; }
Expand Down Expand Up @@ -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<FeatureConstructor>(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;
}
}

Expand Down Expand Up @@ -648,7 +652,7 @@ class ReaderImpl {
}

private:
ImportOptions options_;
ImportExportOptions options_;
std::unique_ptr<FeatureConstructor> constructor_;
GeoArrowArrayView array_view_;
GeoArrowWKTReader wkt_reader_;
Expand Down Expand Up @@ -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);
Expand All @@ -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
27 changes: 21 additions & 6 deletions src/s2geography/geoarrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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<std::unique_ptr<Geography>>* out);
Expand All @@ -65,6 +65,21 @@ class Reader {
std::unique_ptr<ReaderImpl> 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<WriterImpl> impl_;
};

} // namespace geoarrow

} // namespace s2geography
15 changes: 10 additions & 5 deletions src/s2geography/geoarrow_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -106,7 +107,8 @@ TEST(GeoArrow, GeoArrowReaderReadWKBPoint) {
nanoarrow::UniqueArray array;
std::vector<std::unique_ptr<s2geography::Geography>> 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},
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/s2geography/wkt-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace s2geography {

WKTReader::WKTReader(const geoarrow::ImportOptions& options) {
WKTReader::WKTReader(const geoarrow::ImportExportOptions& options) {
reader_ = absl::make_unique<geoarrow::Reader>();
reader_->Init(geoarrow::Reader::InputType::kWKT, options);
}
Expand Down
4 changes: 2 additions & 2 deletions src/s2geography/wkt-reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Geography> read_feature(const char* text, int64_t size);
std::unique_ptr<Geography> read_feature(const char* text);
std::unique_ptr<Geography> read_feature(const std::string& str);
Expand Down

0 comments on commit 8d2ca4b

Please sign in to comment.