Skip to content

Commit

Permalink
Build triangle tree from geo_shape indexed fields (#50012)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase authored Dec 12, 2019
1 parent cabcfe5 commit 6f5919a
Show file tree
Hide file tree
Showing 19 changed files with 563 additions and 875 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@

package org.elasticsearch.common.geo;

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;
import org.elasticsearch.geometry.LinearRing;
import org.elasticsearch.geometry.MultiLine;
import org.elasticsearch.geometry.MultiPoint;
import org.elasticsearch.geometry.MultiPolygon;
import org.elasticsearch.geometry.Point;
import org.elasticsearch.geometry.Polygon;
import org.elasticsearch.geometry.Rectangle;

/**
* This class keeps a running Kahan-sum of coordinates
* that are to be averaged in {@link TriangleTreeWriter} for use
Expand All @@ -32,12 +45,13 @@ public class CentroidCalculator {
private double sumY;
private int count;

public CentroidCalculator() {
public CentroidCalculator(Geometry geometry) {
this.sumX = 0.0;
this.compX = 0.0;
this.sumY = 0.0;
this.compY = 0.0;
this.count = 0;
geometry.visit(new CentroidCalculatorVisitor(this));
}

/**
Expand All @@ -47,7 +61,7 @@ public CentroidCalculator() {
* @param x the x-coordinate of the point
* @param y the y-coordinate of the point
*/
public void addCoordinate(double x, double y) {
private void addCoordinate(double x, double y) {
double correctedX = x - compX;
double newSumX = sumX + correctedX;
compX = (newSumX - sumX) - correctedX;
Expand All @@ -69,7 +83,7 @@ public void addCoordinate(double x, double y) {
*
* @param otherCalculator the other centroid calculator to add from
*/
void addFrom(CentroidCalculator otherCalculator) {
public void addFrom(CentroidCalculator otherCalculator) {
addCoordinate(otherCalculator.sumX, otherCalculator.sumY);
// adjust count
count += otherCalculator.count - 1;
Expand All @@ -88,4 +102,88 @@ public double getX() {
public double getY() {
return sumY / count;
}

private static class CentroidCalculatorVisitor implements GeometryVisitor<Void, IllegalArgumentException> {

private final CentroidCalculator calculator;

private CentroidCalculatorVisitor(CentroidCalculator calculator) {
this.calculator = calculator;
}

@Override
public Void visit(Circle circle) {
calculator.addCoordinate(circle.getX(), circle.getY());
return null;
}

@Override
public Void visit(GeometryCollection<?> collection) {
for (Geometry shape : collection) {
shape.visit(this);
}
return null;
}

@Override
public Void visit(Line line) {

for (int i = 0; i < line.length(); i++) {
calculator.addCoordinate(line.getX(i), line.getY(i));
}
return null;
}

@Override
public Void visit(LinearRing ring) {
for (int i = 0; i < ring.length() - 1; i++) {
calculator.addCoordinate(ring.getX(i), ring.getY(i));
}
return null;
}

@Override
public Void visit(MultiLine multiLine) {
for (Line line : multiLine) {
visit(line);
}
return null;
}

@Override
public Void visit(MultiPoint multiPoint) {
for (Point point : multiPoint) {
visit(point);
}
return null;
}

@Override
public Void visit(MultiPolygon multiPolygon) {
for (Polygon polygon : multiPolygon) {
visit(polygon);
}
return null;
}

@Override
public Void visit(Point point) {
calculator.addCoordinate(point.getX(), point.getY());
return null;
}

@Override
public Void visit(Polygon polygon) {
return visit(polygon.getPolygon());
}

@Override
public Void visit(Rectangle rectangle) {
calculator.addCoordinate(rectangle.getMinX(), rectangle.getMinY());
calculator.addCoordinate(rectangle.getMinX(), rectangle.getMaxY());
calculator.addCoordinate(rectangle.getMaxX(), rectangle.getMinY());
calculator.addCoordinate(rectangle.getMaxX(), rectangle.getMaxY());
return null;
}
}
}
51 changes: 23 additions & 28 deletions server/src/main/java/org/elasticsearch/common/geo/Extent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@
*/
package org.elasticsearch.common.geo;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;

import java.io.IOException;
import java.util.Objects;

/**
* Object representing the extent of a geometry object within a {@link ShapeTreeWriter}.
* Object representing the extent of a geometry object within a {@link TriangleTreeWriter}.
*/
public class Extent implements Writeable {
static final int WRITEABLE_SIZE_IN_BYTES = 24;
public class Extent {

public int top;
public int bottom;
Expand All @@ -47,7 +41,7 @@ public Extent() {
this.posRight = Integer.MIN_VALUE;
}

public Extent(int top, int bottom, int negLeft, int negRight, int posLeft, int posRight) {
private Extent(int top, int bottom, int negLeft, int negRight, int posLeft, int posRight) {
this.top = top;
this.bottom = bottom;
this.negLeft = negLeft;
Expand All @@ -56,10 +50,6 @@ public Extent(int top, int bottom, int negLeft, int negRight, int posLeft, int p
this.posRight = posRight;
}

Extent(StreamInput input) throws IOException {
this(input.readInt(), input.readInt(), input.readInt(), input.readInt(), input.readInt(), input.readInt());
}

public void reset(int top, int bottom, int negLeft, int negRight, int posLeft, int posRight) {
this.top = top;
this.bottom = bottom;
Expand Down Expand Up @@ -88,9 +78,10 @@ public void addRectangle(int bottomLeftX, int bottomLeftY, int topRightX, int to
this.negRight = Math.max(this.negRight, topRightX);
} else if (bottomLeftX < 0) {
this.negLeft = Math.min(this.negLeft, bottomLeftX);
this.negRight = Math.max(this.negRight, bottomLeftX);
this.posLeft = Math.min(this.posLeft, topRightX);
this.posRight = Math.max(this.posRight, topRightX);
// this signal the extent cannot be wrapped around the dateline
this.negRight = 0;
this.posLeft = 0;
} else {
this.posLeft = Math.min(this.posLeft, bottomLeftX);
this.posRight = Math.max(this.posRight, topRightX);
Expand Down Expand Up @@ -131,8 +122,11 @@ static Extent fromPoints(int bottomLeftX, int bottomLeftY, int topRightX, int to
negLeft = bottomLeftX;
negRight = topRightX;
} else if (bottomLeftX < 0) {
negLeft = negRight = bottomLeftX;
posLeft = posRight = topRightX;
negLeft = bottomLeftX;
posRight = topRightX;
// this signal the extent cannot be wrapped around the dateline
negRight = 0;
posLeft = 0;
} else {
posLeft = bottomLeftX;
posRight = topRightX;
Expand Down Expand Up @@ -168,17 +162,6 @@ public int maxX() {
return Math.max(negRight, posRight);
}


@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeInt(top);
out.writeInt(bottom);
out.writeInt(negLeft);
out.writeInt(negRight);
out.writeInt(posLeft);
out.writeInt(posRight);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -196,4 +179,16 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(top, bottom, negLeft, negRight, posLeft, posRight);
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder("[");
builder.append("top = " + top + ", ");
builder.append("bottom = " + bottom + ", ");
builder.append("negLeft = " + negLeft + ", ");
builder.append("negRight = " + negRight + ", ");
builder.append("posLeft = " + posLeft + ", ");
builder.append("posRight = " + posRight + "]");
return builder.toString();
}
}

This file was deleted.

Loading

0 comments on commit 6f5919a

Please sign in to comment.