Skip to content

Commit

Permalink
Simpler wrapper around SpatialEnvelopeVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Nov 18, 2024
1 parent 8e76a4b commit 90869f3
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.xpack.esql.core.util;

import org.elasticsearch.geometry.Circle;
import org.elasticsearch.geometry.Geometry;
import org.elasticsearch.geometry.GeometryCollection;
import org.elasticsearch.geometry.GeometryVisitor;
import org.elasticsearch.geometry.Line;
Expand All @@ -19,13 +20,23 @@
import org.elasticsearch.geometry.Polygon;
import org.elasticsearch.geometry.Rectangle;

import java.util.Optional;

public class SpatialEnvelopeVisitor implements GeometryVisitor<Boolean, RuntimeException> {

private double minX = Double.POSITIVE_INFINITY;
private double minY = Double.POSITIVE_INFINITY;
private double maxX = Double.NEGATIVE_INFINITY;
private double maxY = Double.NEGATIVE_INFINITY;

public static Optional<Rectangle> visit(Geometry geometry) {
var visitor = new SpatialEnvelopeVisitor();
if (geometry.visit(visitor)) {
return Optional.of(visitor.getResult());
}
return Optional.empty();
}

public Rectangle getResult() {
return new Rectangle(minX, maxX, maxY, minY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getX();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMaxX();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMaxX();
}
throw new IllegalArgumentException("Cannot determine envelope of geometry");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getX();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMinX();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMinX();
}
throw new IllegalArgumentException("Cannot determine envelope of geometry");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getY();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMaxY();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMaxY();
}
throw new IllegalArgumentException("Cannot determine envelope of geometry");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getY();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMinY();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMinY();
}
throw new IllegalArgumentException("Cannot determine envelope of geometry");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getX();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMaxX();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMaxX();
}
throw new IllegalArgumentException("Geometry is empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getX();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMinX();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMinX();
}
throw new IllegalArgumentException("Geometry is empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getY();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMaxY();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMaxY();
}
throw new IllegalArgumentException("Geometry is empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) {
if (geometry instanceof Point point) {
return point.getY();
}
var envelope = new SpatialEnvelopeVisitor();
if (geometry.visit(envelope)) {
return envelope.getResult().getMinY();
var envelope = SpatialEnvelopeVisitor.visit(geometry);
if (envelope.isPresent()) {
return envelope.get().getMinY();
}
throw new IllegalArgumentException("Geometry is empty");
}
Expand Down

0 comments on commit 90869f3

Please sign in to comment.