Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy geo code from AggregationResultUtils #77702

Merged
merged 6 commits into from
Sep 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Numbers;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.builders.LineStringBuilder;
import org.elasticsearch.common.geo.builders.PointBuilder;
import org.elasticsearch.common.geo.builders.PolygonBuilder;
import org.elasticsearch.common.geo.parsers.ShapeParser;

import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.search.aggregations.Aggregation;
Expand Down Expand Up @@ -82,6 +79,12 @@ public final class AggregationResultUtils {
BUCKET_KEY_EXTRACTOR_MAP = Collections.unmodifiableMap(tempMap);
}

private static final String FIELD_TYPE = "type";
private static final String FIELD_COORDINATES = "coordinates";
private static final String POINT = "point";
private static final String LINESTRING = "linestring";
private static final String POLYGON = "polygon";

/**
* Extracts aggregation results from a composite aggregation and puts it into a map.
*
Expand Down Expand Up @@ -413,29 +416,29 @@ public Object value(Aggregation agg, Map<String, String> fieldTypeMap, String lo
final Map<String, Object> geoShape = new HashMap<>();
// If the two geo_points are equal, it is a point
if (aggregation.topLeft().equals(aggregation.bottomRight())) {
geoShape.put(ShapeParser.FIELD_TYPE.getPreferredName(), PointBuilder.TYPE.shapeName());
geoShape.put(FIELD_TYPE, POINT);
geoShape.put(
ShapeParser.FIELD_COORDINATES.getPreferredName(),
FIELD_COORDINATES,
Arrays.asList(aggregation.topLeft().getLon(), aggregation.bottomRight().getLat())
);
// If only the lat or the lon of the two geo_points are equal, than we know it should be a line
} else if (Double.compare(aggregation.topLeft().getLat(), aggregation.bottomRight().getLat()) == 0
|| Double.compare(aggregation.topLeft().getLon(), aggregation.bottomRight().getLon()) == 0) {
geoShape.put(ShapeParser.FIELD_TYPE.getPreferredName(), LineStringBuilder.TYPE.shapeName());
geoShape.put(FIELD_TYPE, LINESTRING);
geoShape.put(
ShapeParser.FIELD_COORDINATES.getPreferredName(),
FIELD_COORDINATES,
Arrays.asList(
new Double[] { aggregation.topLeft().getLon(), aggregation.topLeft().getLat() },
new Double[] { aggregation.bottomRight().getLon(), aggregation.bottomRight().getLat() }
)
);
} else {
// neither points are equal, we have a polygon that is a square
geoShape.put(ShapeParser.FIELD_TYPE.getPreferredName(), PolygonBuilder.TYPE.shapeName());
geoShape.put(FIELD_TYPE, POLYGON);
final GeoPoint tl = aggregation.topLeft();
final GeoPoint br = aggregation.bottomRight();
geoShape.put(
ShapeParser.FIELD_COORDINATES.getPreferredName(),
FIELD_COORDINATES,
Collections.singletonList(
Arrays.asList(
new Double[] { tl.getLon(), tl.getLat() },
Expand Down Expand Up @@ -468,9 +471,9 @@ public Object value(Object key, String type) {
assert key instanceof String;
Rectangle rectangle = GeoTileUtils.toBoundingBox(key.toString());
final Map<String, Object> geoShape = new HashMap<>();
geoShape.put(ShapeParser.FIELD_TYPE.getPreferredName(), PolygonBuilder.TYPE.shapeName());
geoShape.put(FIELD_TYPE, POLYGON);
geoShape.put(
ShapeParser.FIELD_COORDINATES.getPreferredName(),
FIELD_COORDINATES,
Collections.singletonList(
Arrays.asList(
new Double[] { rectangle.getMaxLon(), rectangle.getMinLat() },
Expand Down