Skip to content

Commit

Permalink
wire up significant_digits option for WKT
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Oct 29, 2024
1 parent 1a8c8c0 commit b868526
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
18 changes: 17 additions & 1 deletion src/s2geography/geoarrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,17 @@ class WriterImpl {
int code = GeoArrowArrayWriterInitFromSchema(&writer_, schema);
ThrowNotOk(code);

GeoArrowSchemaView schema_view;
code = GeoArrowSchemaViewInit(&schema_view, schema, &error_);
ThrowNotOk(code);
type_ = schema_view.type;

InitCommon();
}

void Init(GeoArrowType type, const ExportOptions& options) {
options_ = options;
type_ = type;

int code = GeoArrowArrayWriterInitFromType(&writer_, type);
ThrowNotOk(code);
Expand All @@ -709,8 +715,17 @@ class WriterImpl {
}

void InitCommon() {
int code;

if (type_ == GEOARROW_TYPE_WKT || type_ == GEOARROW_TYPE_LARGE_WKT) {
code = GeoArrowArrayWriterSetPrecision(&writer_, options_.significant_digits());
ThrowNotOk(code);
code = GeoArrowArrayWriterSetFlatMultipoint(&writer_, false);
ThrowNotOk(code);
}

visitor_.error = &error_;
int code = GeoArrowArrayWriterInitVisitor(&writer_, &visitor_);
code = GeoArrowArrayWriterInitVisitor(&writer_, &visitor_);
ThrowNotOk(code);

// Currently we always visit single coordinate pairs one by one, so set
Expand All @@ -731,6 +746,7 @@ class WriterImpl {

private:
ExportOptions options_;
GeoArrowType type_;
GeoArrowArrayWriter writer_;
GeoArrowVisitor visitor_;
GeoArrowCoordView coords_view_;
Expand Down
2 changes: 1 addition & 1 deletion src/s2geography/geoarrow.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ExportOptions : public TessellationOptions {
public:
ExportOptions()
: TessellationOptions(),
significant_digits_(6) {}
significant_digits_(16) {}
int significant_digits() const { return significant_digits_; }
void set_significant_digits(int significant_digits) { significant_digits_ = significant_digits; }

Expand Down
23 changes: 11 additions & 12 deletions src/s2geography/wkt-writer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@

using namespace s2geography;

// TEST(WKTWriter, SignificantDigits) {
// WKTReader reader;
// // Lat/lon is converted to XYZ here for the internals, so
// // we need to pick a value that will roundtrip with 16 digits of precision
// auto geog = reader.read_feature("POINT (0 3.333333333333334)");

// WKTWriter writer_default;
// EXPECT_EQ(writer_default.write_feature(*geog), "POINT (0 3.333333333333334)");
TEST(WKTWriter, SignificantDigits) {
WKTReader reader;
// Lat/lon is converted to XYZ here for the internals, so
// we need to pick a value that will roundtrip with 16 digits of precision
auto geog = reader.read_feature("POINT (0 3.333333333333334)");

// WKTWriter writer_6digits(6);
// EXPECT_EQ(writer_6digits.write_feature(*geog), "POINT (0 3.33333)");
// WKTWriter writer_default;
// EXPECT_EQ(writer_default.write_feature(*geog), "POINT (0 3.3333333333333344)");

// }
WKTWriter writer_6digits(6);
EXPECT_EQ(writer_6digits.write_feature(*geog), "POINT (0 3.333333)");
}

static std::string
wktRoundTrip(const std::string &wktIn) {
WKTReader reader;
WKTWriter writer;
WKTWriter writer(2);
auto geog = reader.read_feature(wktIn);
return writer.write_feature(*geog);
}
Expand Down
9 changes: 4 additions & 5 deletions src/vendored/geoarrow/geoarrow.c
Original file line number Diff line number Diff line change
Expand Up @@ -9089,11 +9089,10 @@ GeoArrowErrorCode GeoArrowWKTWriterInit(struct GeoArrowWKTWriter* writer) {
ArrowBitmapInit(&private->validity);
ArrowBufferInit(&private->offsets);
ArrowBufferInit(&private->values);
// TODO should be changed upstream to make this configurable
writer->precision = 6;
private->precision = 6;
writer->use_flat_multipoint = 0;
private->use_flat_multipoint = 0;
writer->precision = 16;
private->precision = 16;
writer->use_flat_multipoint = 1;
private->use_flat_multipoint = 1;
writer->max_element_size_bytes = -1;
private->max_element_size_bytes = -1;
writer->private_data = private;
Expand Down

0 comments on commit b868526

Please sign in to comment.