Skip to content

Commit

Permalink
feat: geoarrow export
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Oct 9, 2024
1 parent 829a0fe commit 2288f40
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 0 deletions.
244 changes: 244 additions & 0 deletions src/s2geography/geoarrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "s2geography/geoarrow.h"

#include <sstream>
#include <iostream>

#include "geoarrow/geoarrow.h"
#include "s2/s1angle.h"
Expand Down Expand Up @@ -742,6 +743,249 @@ void Reader::ReadGeography(const ArrowArray* array, int64_t offset,
impl_->ReadGeography(array, offset, length, out);
}



class WriterImpl {
public:
WriterImpl() {
error_.message[0] = '\0';
writer_.private_data = nullptr;
}

~WriterImpl() {
if (writer_.private_data != nullptr) {
GeoArrowArrayWriterReset(&writer_);
}
}

void Init(const ArrowSchema* schema, const ImportOptions& options) {
options_ = options;

int code = GeoArrowArrayWriterInitFromSchema(&writer_, schema);
ThrowNotOk(code);
InitCommon();
}

void Init(GeoArrowType type, const ImportOptions& options, struct ArrowSchema* out_schema) {
options_ = options;

int code = GeoArrowArrayWriterInitFromType(&writer_, type);
ThrowNotOk(code);

code = GeoArrowSchemaInitExtension(out_schema, type);
ThrowNotOk(code);

InitCommon();
}

void InitCommon() {
// constructor_ = absl::make_unique<FeatureConstructor>(options_);
// constructor_->InitVisitor(&visitor_);
// visitor_.error = &error_;

int code = GeoArrowArrayWriterInitVisitor(&writer_, &visitor_);
ThrowNotOk(code);
}

void WriteGeography(const Geography** geographies, size_t geographies_size,
struct ArrowArray* out){

for (size_t i = 0; i < geographies_size; i++) {
// if (geography == nullptr) {
// GEOARROW_RETURN_NOT_OK(visitor_.feat_start(&visitor_));
// GEOARROW_RETURN_NOT_OK(visitor_.null_feat(&visitor_));
// GEOARROW_RETURN_NOT_OK(visitor_.feat_end(&visitor_));
// } else {
VisitFeature(*geographies[i]);
// }
}
int code = GeoArrowArrayWriterFinish(&writer_, out, &error_);
ThrowNotOk(code);
}

private:
ImportOptions options_;
GeoArrowArrayWriter writer_;
GeoArrowVisitor visitor_;
GeoArrowCoordView coords_view_;
GeoArrowError error_;

int VisitPoints(const PointGeography& point) {
// coords_view_.n_coords = point.num_vertices();
// coords_view_.n_values = 3;
// coords_view_.values = point.data();
// return handler->coords(&coords_view_);
std::cout << "In VisitPoints" << "\n";
double coords[2];

if (point.Points().size() == 0) {
GEOARROW_RETURN_NOT_OK(visitor_.geom_start(&visitor_, GEOARROW_GEOMETRY_TYPE_POINT, GEOARROW_DIMENSIONS_XY));
GEOARROW_RETURN_NOT_OK(visitor_.geom_end(&visitor_));
} else if (point.Points().size() == 1) {
GEOARROW_RETURN_NOT_OK(visitor_.geom_start(&visitor_, GEOARROW_GEOMETRY_TYPE_POINT, GEOARROW_DIMENSIONS_XY));
S2LatLng ll(point.Points()[0]);
coords_view_.n_coords = 1;
coords_view_.n_values = 2;
coords_view_.coords_stride = 2;
coords[0] = ll.lng().degrees();
coords[1] = ll.lat().degrees();
coords_view_.values[0] = &coords[0];
coords_view_.values[1] = &coords[1];
// coords_view_.values = static_cast<const double*[4]>(coords);
// GEOARROW_COORD_VIEW_VALUE(&coords_view_, 0, 0) = ll.lng().degrees();
// GEOARROW_COORD_VIEW_VALUE(&coords_view_, 0, 1) = ll.lng().degrees();

GEOARROW_RETURN_NOT_OK(visitor_.coords(&visitor_, &coords_view_));
GEOARROW_RETURN_NOT_OK(visitor_.geom_end(&visitor_));
}
// } else {
// handler->new_geometry_type(util::GeometryType::MULTIPOINT);
// HANDLE_OR_RETURN(handler->geom_start(util::GeometryType::MULTIPOINT,
// geog.Points().size()));

// for (const S2Point& pt : geog.Points()) {
// handler->geom_start(util::GeometryType::POINT, 1);
// S2LatLng ll(pt);
// coords[0] = ll.lng().degrees();
// coords[1] = ll.lat().degrees();
// HANDLE_OR_RETURN(handler->coords(coords, 1, 2));
// HANDLE_OR_RETURN(handler->geom_end());
// }

// handler->geom_end();
// }
std::cout << "-- end of VisitPoints" << "\n";
return GEOARROW_OK;
}

int VisitFeature(const Geography& geog) {
std::cout << "In VisitFeature" << "\n";
GEOARROW_RETURN_NOT_OK(visitor_.feat_start(&visitor_));

auto child_point = dynamic_cast<const PointGeography*>(&geog);
if (child_point != nullptr) {
GEOARROW_RETURN_NOT_OK(VisitPoints(*child_point));
} else {
throw Exception("Unsupported Geography subclass");
// auto child_polyline = dynamic_cast<const PolylineGeography*>(&geog);
// if (child_polyline != nullptr) {
// HANDLE_OR_RETURN(handle_polylines(*child_polyline, handler));
// } else {
// auto child_polygon = dynamic_cast<const PolygonGeography*>(&geog);
// if (child_polygon != nullptr) {
// HANDLE_OR_RETURN(handle_polygon(*child_polygon, handler));
// } else {
// auto child_collection = dynamic_cast<const GeographyCollection*>(&geog);
// if (child_collection != nullptr) {
// HANDLE_OR_RETURN(handle_collection(*child_collection, handler));
// } else {
// throw Exception("Unsupported Geography subclass");
// }
// }
// }
}
std::cout << "-- end of VisitFeature" << "\n";
return GEOARROW_OK;
}

void ThrowNotOk(int code) {
if (code != GEOARROW_OK) {
throw Exception(error_.message);
}
}
};


Writer::Writer() : impl_(new WriterImpl()) {}

Writer::~Writer() { impl_.reset(); }

void Writer::Init(const ArrowSchema* schema, const ImportOptions& options) {
impl_->Init(schema, options);
}

void Writer::Init(OutputType output_type, const ImportOptions& options, struct ArrowSchema* out_schema) {
switch (output_type) {
case OutputType::kPoints:
impl_->Init(GEOARROW_TYPE_INTERLEAVED_POINT, options, out_schema);
break;
case OutputType::kWKT:
impl_->Init(GEOARROW_TYPE_WKT, options, out_schema);
break;
case OutputType::kWKB:
impl_->Init(GEOARROW_TYPE_WKB, options, out_schema);
break;
default:
throw Exception("Output type not supported");
}
}

void Writer::WriteGeography(const Geography** geographies, size_t geographies_size,
struct ArrowArray* out) {
impl_->WriteGeography(geographies, geographies_size, out);
}

// class ArrayBuilder {
// public:
// ArrayBuilder() : builder_(nullptr) {}

// ArrayBuilder(ArrayBuilder&& rhs) : builder_(rhs.builder_) { rhs.builder_ = nullptr; }

// ArrayBuilder(ArrayBuilder& rhs) = delete;

// ~ArrayBuilder() {
// if (builder_ != nullptr) {
// GeoArrowGEOSArrayBuilderDestroy(builder_);
// }
// }

// const char* GetLastError() {
// if (builder_ == nullptr) {
// return "";
// } else {
// return GeoArrowGEOSArrayBuilderGetLastError(builder_);
// }
// }

// GeoArrowGEOSErrorCode InitFromEncoding(GEOSContextHandle_t handle,
// GeoArrowGEOSEncoding encoding,
// int wkb_type = 0) {
// ArrowSchema tmp_schema;
// tmp_schema.release = nullptr;
// int result = GeoArrowGEOSMakeSchema(encoding, wkb_type, &tmp_schema);
// if (result != GEOARROW_GEOS_OK) {
// return result;
// }

// result = InitFromSchema(handle, &tmp_schema);
// tmp_schema.release(&tmp_schema);
// return result;
// }

// GeoArrowGEOSErrorCode InitFromSchema(GEOSContextHandle_t handle, ArrowSchema* schema) {
// if (builder_ != nullptr) {
// GeoArrowGEOSArrayBuilderDestroy(builder_);
// }

// return GeoArrowGEOSArrayBuilderCreate(handle, schema, &builder_);
// }

// GeoArrowGEOSErrorCode Append(const GEOSGeometry** geom, size_t geom_size,
// size_t* n_appended) {
// return GeoArrowGEOSArrayBuilderAppend(builder_, geom, geom_size, n_appended);
// }

// GeoArrowGEOSErrorCode Finish(struct ArrowArray* out) {
// return GeoArrowGEOSArrayBuilderFinish(builder_, out);
// }

// private:
// GeoArrowGEOSArrayBuilder* builder_;
// };




} // namespace geoarrow

} // namespace s2geography
29 changes: 29 additions & 0 deletions src/s2geography/geoarrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ class Reader {
std::unique_ptr<ReaderImpl> impl_;
};

class WriterImpl;

/// \brief Array writer for any GeoArrow extension array
///
/// This class is used to convert a vector of Geography objects into an ArrowArray
/// with geoarrow data (serialized or native).
class Writer {
public:
enum class OutputType { kPoints, kWKT, kWKB };
Writer();
~Writer();

void Init(const ArrowSchema* schema) { Init(schema, ImportOptions()); }

void Init(const ArrowSchema* schema, const ImportOptions& options);

void Init(OutputType output_type, struct ArrowSchema* out_schema) { Init(output_type, ImportOptions(), out_schema); }

void Init(OutputType output_type, const ImportOptions& options, struct ArrowSchema* out_schema);

// void WriteGeography(const Geography geographies, struct ArrowArray* out);

void WriteGeography(const Geography** geographies, size_t geographies_size,
struct ArrowArray* out);

private:
std::unique_ptr<WriterImpl> impl_;
};

} // namespace geoarrow

} // namespace s2geography

0 comments on commit 2288f40

Please sign in to comment.