Skip to content

Commit

Permalink
Use max precision when serializing points to XContent
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Jan 2, 2024
1 parent 1e6e744 commit 8c5638e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package org.elasticsearch.common.geo;

import java.util.Locale;

/**
* To facilitate maximizing the use of common code between GeoPoint and projected CRS
* we introduced this ElasticPoint as an interface of commonality.
Expand Down Expand Up @@ -55,7 +53,8 @@ public String toString() {
}

public String toWKT() {
return String.format(Locale.ROOT, "POINT (%f %f)", x, y);
// Code designed to mimic WellKnownText.toWKT, with much less stack depth and object creation
return "POINT (" + x + " " + y + ")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ public Iterator<CheckedConsumer<Writer, IOException>> format(RestRequest request
private static String formatEsqlResultObject(Object obj) {
// TODO: It would be nicer to override GeoPoint.toString() but that has consequences
if (obj instanceof SpatialPoint point) {
// TODO: For doc-values, it is better to display as (%.7f %.7f), so see if we can know if this comes from doc-values
return point.toWKT();
// SpatialPoint.toWKT maintains max precision, while ´%f´ conveniently simplifies for display purposes
return String.format(Locale.ROOT, "POINT (%f %f)", point.getX(), point.getY());
}
return Objects.toString(obj, StringUtils.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.Writer;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Function;

Expand Down Expand Up @@ -133,8 +134,8 @@ private static void writePadding(int padding, Writer writer) throws IOException
private static String formatEsqlResultObject(Object obj) {
// TODO: It would be nicer to override GeoPoint.toString() but that has consequences
if (obj instanceof SpatialPoint point) {
// TODO: For doc-values, it is better to display as (%.7f %.7f), so see if we can know if this comes from doc-values
return point.toWKT();
// SpatialPoint.toWKT maintains max precision, while ´%f´ conveniently simplifies for display purposes
return String.format(Locale.ROOT, "POINT (%f %f)", point.getX(), point.getY());
}
return Objects.toString(obj);
}
Expand Down

0 comments on commit 8c5638e

Please sign in to comment.