Skip to content

Commit

Permalink
Fix VectorTileRestIT#testCentroidGridTypeOnPolygon
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Oct 18, 2023
1 parent cad7232 commit 0065898
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
14 changes: 14 additions & 0 deletions server/src/main/java/org/elasticsearch/common/geo/GeoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,5 +581,19 @@ public static double quantizeLat(double lat) {
return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(lat));
}

/**
* Transforms the provided longitude to the previous longitude in lucene quantize space.
*/
public static double quantizeLonDown(double lon) {
return GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(lon) - 1);
}

/**
* Transforms the provided latitude to the next latitude in lucene quantize space.
*/
public static double quantizeLatUp(double lat) {
return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(lat) + 1);
}

private GeoUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ public void testEncodingLuceneLonConsistency() {
Matchers.anyOf(equalTo(x + 1), equalTo(tiles - 1))
);
// next encoded value down belongs to the tile
assertThat(GeoTileUtils.getXTile(quantizeLonDown(rectangle.getMaxX()), tiles), equalTo(x));
assertThat(GeoTileUtils.getXTile(GeoUtils.quantizeLonDown(rectangle.getMaxX()), tiles), equalTo(x));
// min longitude belongs to the tile
assertThat(GeoTileUtils.getXTile(GeoUtils.quantizeLon(rectangle.getMinX()), tiles), equalTo(x));
if (x != 0) {
// next encoded value down belongs to the previous tile
assertThat(GeoTileUtils.getXTile(quantizeLonDown(rectangle.getMinX()), tiles), equalTo(x - 1));
assertThat(GeoTileUtils.getXTile(GeoUtils.quantizeLonDown(rectangle.getMinX()), tiles), equalTo(x - 1));
}
}
}
Expand All @@ -276,23 +276,16 @@ public void testEncodingLuceneLatConsistency() {
assertThat(GeoTileUtils.getYTile(GeoUtils.quantizeLat(rectangle.getMaxLat()), tiles), equalTo(y));
if (y != 0) {
// next encoded value up belongs to the previous tile
assertThat(GeoTileUtils.getYTile(quantizeLatUp(rectangle.getMaxLat()), tiles), equalTo(y - 1));
assertThat(GeoTileUtils.getYTile(GeoUtils.quantizeLatUp(rectangle.getMaxLat()), tiles), equalTo(y - 1));
}
// min latitude belongs to the next tile except the last one
assertThat(
GeoTileUtils.getYTile(GeoUtils.quantizeLat(rectangle.getMinLat()), tiles),
Matchers.anyOf(equalTo(y + 1), equalTo(tiles - 1))
);
// next encoded value up belongs to the tile
assertThat(GeoTileUtils.getYTile(quantizeLatUp(rectangle.getMinLat()), tiles), equalTo(y));
assertThat(GeoTileUtils.getYTile(GeoUtils.quantizeLatUp(rectangle.getMinLat()), tiles), equalTo(y));
}
}

private static double quantizeLonDown(double lon) {
return GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(lon) - 1);
}

private static double quantizeLatUp(double lat) {
return GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(lat) + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ public void testInvalidAggName() {
assertThat(ex.getMessage(), Matchers.containsString("Invalid aggregation name [_mvt_name]"));
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/101038")
public void testCentroidGridTypeOnPolygon() throws Exception {
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + (z + 2) + "/" + 4 * x + "/" + 4 * y);
mvtRequest.setJsonEntity("{\"size\" : 0, \"grid_type\": \"centroid\", \"grid_precision\": 2}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.io.IOException;
import java.util.Locale;

import static org.elasticsearch.common.geo.GeoUtils.quantizeLatUp;
import static org.elasticsearch.common.geo.GeoUtils.quantizeLonDown;

/**
* Enum containing the basic geometry types for serializing {@link InternalGeoGridBucket}
*/
Expand All @@ -42,8 +45,8 @@ public byte[] toFeature(GridAggregation gridAggregation, InternalGeoGridBucket b
throws IOException {
final Rectangle r = gridAggregation.toRectangle(key);
final InternalGeoCentroid centroid = bucket.getAggregations().get(RestVectorTileAction.CENTROID_AGG_NAME);
final double featureLon = Math.min(Math.max(centroid.centroid().getX(), r.getMinLon()), r.getMaxLon());
final double featureLat = Math.min(Math.max(centroid.centroid().getY(), r.getMinLat()), r.getMaxLat());
final double featureLon = Math.min(Math.max(centroid.centroid().getX(), r.getMinLon()), quantizeLonDown(r.getMaxLon()));
final double featureLat = Math.min(Math.max(centroid.centroid().getY(), quantizeLatUp(r.getMinLat())), r.getMaxLat());
return featureFactory.point(featureLon, featureLat);
}
};
Expand Down

0 comments on commit 0065898

Please sign in to comment.