Skip to content

Commit 58b2243

Browse files
committed
fix tests for geo-centroid and revert to normalized lat/lon
1 parent 4b5466a commit 58b2243

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

server/src/main/java/org/elasticsearch/common/geo/CentroidCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public Void visit(LinearRing ring) {
169169
*/
170170
private void visitSignedRing(LinearRing ring, int sign) {
171171
for (int i = 0; i < ring.length() - 1; i++) {
172-
double weight = ring.getX(i) * ring.getY(i + 1) + ring.getY(i) * ring.getX(i + 1);
172+
double weight = ring.getX(i + 1) * ring.getY(i) - ring.getX(i) * ring.getY(i + 1);
173173
calculator.addCoordinate((ring.getX(i) + ring.getX(i + 1)) / 2, (ring.getY(i) + ring.getY(i + 1)) / 2, sign * weight);
174174
}
175175
}

server/src/main/java/org/elasticsearch/common/geo/TriangleTreeReader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* relations against the serialized triangle tree.
3434
*/
3535
public class TriangleTreeReader {
36-
private static final int CENTROID_HEADER_SIZE_IN_BYTES = 25; // x-coord (4), y-coord (4), shape-type (1), sum-weight (8)
36+
private static final int CENTROID_HEADER_SIZE_IN_BYTES = 17; // x-coord (4), y-coord (4), shape-type (1), sum-weight (8)
3737

3838
private final ByteArrayDataInput input;
3939
private final CoordinateEncoder coordinateEncoder;
@@ -77,26 +77,26 @@ public Extent getExtent() {
7777
/**
7878
* returns the X coordinate of the centroid.
7979
*/
80-
public double getWeightedCentroidX() {
80+
public double getUnweightedCentroidX() {
8181
input.setPosition(0);
82-
return Double.longBitsToDouble(input.readLong());
82+
return coordinateEncoder.decodeX(input.readInt());
8383
}
8484

8585
/**
8686
* returns the Y coordinate of the centroid.
8787
*/
88-
public double getWeightedCentroidY() {
89-
input.setPosition(8);
90-
return Double.longBitsToDouble(input.readLong());
88+
public double getUnweightedCentroidY() {
89+
input.setPosition(4);
90+
return coordinateEncoder.decodeY(input.readInt());
9191
}
9292

9393
public DimensionalShapeType getDimensionalShapeType() {
94-
input.setPosition(16);
94+
input.setPosition(8);
9595
return DimensionalShapeType.readFrom(input);
9696
}
9797

9898
public double getSumCentroidWeight() {
99-
input.setPosition(17);
99+
input.setPosition(9);
100100
return Double.longBitsToDouble(input.readLong());
101101
}
102102

server/src/main/java/org/elasticsearch/common/geo/TriangleTreeWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public TriangleTreeWriter(List<ShapeField.DecodedTriangle> triangles, Coordinate
4747

4848
/*** Serialize the interval tree in the provided data output */
4949
public void writeTo(ByteBuffersDataOutput out) throws IOException {
50-
out.writeLong(Double.doubleToLongBits(centroidCalculator.getX()));
51-
out.writeLong(Double.doubleToLongBits(centroidCalculator.getY()));
50+
out.writeInt(coordinateEncoder.encodeX(centroidCalculator.getX()));
51+
out.writeInt(coordinateEncoder.encodeY(centroidCalculator.getY()));
5252
centroidCalculator.getDimensionalShapeType().writeTo(out);
5353
out.writeLong(Double.doubleToLongBits(centroidCalculator.sumWeight()));
5454
out.writeInt(extent.top);

server/src/main/java/org/elasticsearch/index/fielddata/MultiGeoValues.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ public double weight() {
185185

186186
@Override
187187
public double lat() {
188-
return reader.getWeightedCentroidY();
188+
return reader.getUnweightedCentroidY();
189189
}
190190

191191
/**
192192
* @return the longitude of the centroid of the shape
193193
*/
194194
@Override
195195
public double lon() {
196-
return reader.getWeightedCentroidX();
196+
return reader.getUnweightedCentroidX();
197197
}
198198

199199
public static GeoShapeValue missing(String missing) {

server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void collect(int doc, long bucket) throws IOException {
8888
latCompensations = bigArrays.grow(latCompensations, bucket + 1);
8989
weightCompensations = bigArrays.grow(weightCompensations, bucket + 1);
9090
counts = bigArrays.grow(counts, bucket + 1);
91+
dimensionalShapeTypes = bigArrays.grow(dimensionalShapeTypes, bucket + 1);
9192

9293
if (values.advanceExact(doc)) {
9394
final int valueCount = values.docValueCount();
@@ -112,17 +113,19 @@ public void collect(int doc, long bucket) throws IOException {
112113
MultiGeoValues.GeoValue value = values.nextValue();
113114
int compares = DimensionalShapeType.COMPARATOR.compare(shapeType, value.dimensionalShapeType());
114115
if (compares < 0) {
115-
compensatedSumLat.reset(value.lat(), 0.0);
116-
compensatedSumLon.reset(value.lon(), 0.0);
117-
compensatedSumWeight.reset(value.weight(), 0.0);
116+
double coordinateWeight = value.weight();
117+
compensatedSumLat.reset(coordinateWeight * value.lat(), 0.0);
118+
compensatedSumLon.reset(coordinateWeight * value.lon(), 0.0);
119+
compensatedSumWeight.reset(coordinateWeight, 0.0);
118120
dimensionalShapeTypes.set(bucket, (byte) value.dimensionalShapeType().ordinal());
119121
} else if (compares == 0) {
122+
double coordinateWeight = value.weight();
120123
// weighted latitude
121-
compensatedSumLat.add(value.lat());
124+
compensatedSumLat.add(coordinateWeight * value.lat());
122125
// weighted longitude
123-
compensatedSumLon.add(value.lon());
126+
compensatedSumLon.add(coordinateWeight * value.lon());
124127
// weight
125-
compensatedSumWeight.add(value.weight());
128+
compensatedSumWeight.add(coordinateWeight);
126129
}
127130

128131
}

server/src/test/java/org/elasticsearch/common/geo/CentroidCalculatorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public void test() {
4444
System.arraycopy(y, 0, subY, 0, i + 1);
4545
Geometry geometry = new Line(subX, subY);
4646
calculator = new CentroidCalculator(geometry);
47-
assertThat(calculator.getX(), equalTo(xRunningAvg[i]));
48-
assertThat(calculator.getY(), equalTo(yRunningAvg[i]));
47+
assertEquals(xRunningAvg[i], calculator.getX(), 0.000001);
48+
assertEquals(yRunningAvg[i], calculator.getY(), 0.000001);
4949
}
5050
CentroidCalculator otherCalculator = new CentroidCalculator(new Point(0, 0));
5151
calculator.addFrom(otherCalculator);
52-
assertThat(calculator.getX(), equalTo(50.0));
53-
assertThat(calculator.getY(), equalTo(5.0));
52+
assertEquals(55.0, calculator.getX(), 0.0000001);
53+
assertEquals(5.5, calculator.getY(), 0.0000001);
5454
}
5555
}

server/src/test/java/org/elasticsearch/common/geo/TriangleTreeTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void testRectangleShape() throws IOException {
9898
// centroid is calculated using original double values but then loses precision as it is serialized as an integer
9999
int encodedCentroidX = GeoShapeCoordinateEncoder.INSTANCE.encodeX(((double) minX + maxX) / 2);
100100
int encodedCentroidY = GeoShapeCoordinateEncoder.INSTANCE.encodeY(((double) minY + maxY) / 2);
101-
assertEquals(GeoShapeCoordinateEncoder.INSTANCE.decodeX(encodedCentroidX), reader.getWeightedCentroidX(), 0.0000001);
102-
assertEquals(GeoShapeCoordinateEncoder.INSTANCE.decodeY(encodedCentroidY), reader.getWeightedCentroidY(), 0.0000001);
101+
assertEquals(GeoShapeCoordinateEncoder.INSTANCE.decodeX(encodedCentroidX), reader.getUnweightedCentroidX(), 0.0000001);
102+
assertEquals(GeoShapeCoordinateEncoder.INSTANCE.decodeY(encodedCentroidY), reader.getUnweightedCentroidY(), 0.0000001);
103103

104104
// box-query touches bottom-left corner
105105
assertRelation(GeoRelation.QUERY_CROSSES, reader, getExtentFromBox(minX - randomIntBetween(1, 180 + minX),

0 commit comments

Comments
 (0)