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

Vector tiles: Add test reproducing a StackOverflowError #75543

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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 @@ -49,12 +49,15 @@ public class FeatureFactory {
private final Envelope tileEnvelope;
private final Envelope clipEnvelope;

private static final double MERCATOR_BOUDS = 20037508.34;

public FeatureFactory(int z, int x, int y, int extent) {
final Rectangle r = SphericalMercatorUtils.recToSphericalMercator(GeoTileUtils.toBoundingBox(x, y, z));
this.tileEnvelope = new Envelope(r.getMinX(), r.getMaxX(), r.getMinY(), r.getMaxY());
this.clipEnvelope = new Envelope(tileEnvelope);
this.clipEnvelope.expandBy(tileEnvelope.getWidth() * 0.1d, tileEnvelope.getHeight() * 0.1d);
this.builder = new JTSGeometryBuilder(geomFactory);
final double pixelPrecision = 2 * MERCATOR_BOUDS / ((1L << z) * extent);
this.builder = new JTSGeometryBuilder(geomFactory, pixelPrecision);
// TODO: Not sure what is the difference between extent and tile size?
this.layerParams = new MvtLayerParams(extent, extent);
}
Expand All @@ -79,8 +82,10 @@ public MvtLayerProps getLayerProps() {
private static class JTSGeometryBuilder implements GeometryVisitor<org.locationtech.jts.geom.Geometry, IllegalArgumentException> {

private final GeometryFactory geomFactory;
double pixelPrecision;

JTSGeometryBuilder(GeometryFactory geomFactory) {
JTSGeometryBuilder(GeometryFactory geomFactory, double pixelPrecision) {
this.pixelPrecision = pixelPrecision;
this.geomFactory = geomFactory;
}

Expand Down Expand Up @@ -163,6 +168,9 @@ public org.locationtech.jts.geom.Geometry visit(MultiPolygon multiPolygon) throw

private org.locationtech.jts.geom.Polygon buildPolygon(Polygon polygon) {
final org.locationtech.jts.geom.LinearRing outerShell = buildLinearRing(polygon.getPolygon());
if (isValidPolygon(outerShell) == false) {
return geomFactory.createPolygon(); // empty polygon
}
if (polygon.getNumberOfHoles() == 0) {
return geomFactory.createPolygon(outerShell);
}
Expand All @@ -173,6 +181,12 @@ private org.locationtech.jts.geom.Polygon buildPolygon(Polygon polygon) {
return geomFactory.createPolygon(outerShell, holes);
}

private boolean isValidPolygon(org.locationtech.jts.geom.LinearRing outerShell) {
final Envelope envelope = outerShell.getEnvelopeInternal();
final double polMinimumSize = pixelPrecision * outerShell.getNumPoints() / 1000;
return envelope.getMaxX() - envelope.getMinX() >= polMinimumSize && envelope.getMaxY() - envelope.getMinY() >= polMinimumSize;
}

private org.locationtech.jts.geom.LinearRing buildLinearRing(LinearRing ring) throws RuntimeException {
final Coordinate[] coordinates = new Coordinate[ring.length()];
for (int i = 0; i < ring.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@
import org.elasticsearch.geometry.Point;
import org.elasticsearch.geometry.Polygon;
import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.geometry.utils.StandardValidator;
import org.elasticsearch.geometry.utils.WellKnownText;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils;
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;

public class FeatureFactoryTests extends ESTestCase {

Expand Down Expand Up @@ -146,4 +153,15 @@ private MultiPolygon buildMultiPolygon(Rectangle r) {
private GeometryCollection<Geometry> buildGeometryCollection(Rectangle r) {
return new GeometryCollection<>(List.of(buildPolygon(r), buildLine(r)));
}

public void testStackOverflowError() throws Exception {
final InputStream is = new GZIPInputStream(getClass().getResourceAsStream("polygon.wkt.gz"));
final BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
final Geometry geometry = WellKnownText.fromWKT(StandardValidator.instance(true), true, reader.readLine());
// just make sure we don't blow
for (int i = 0; i < 10; i++) {
final FeatureFactory builder = new FeatureFactory(0, 0, 0, randomIntBetween(128, 8012));
builder.getFeatures(geometry, new UserDataIgnoreConverter());
}
}
}
Binary file not shown.