Skip to content

GEO: Switch to using GeoTestUtil to generate random geo shapes #44635

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

Merged
Merged
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 @@ -28,10 +28,7 @@

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

abstract class BaseGeometryTestCase<T extends Geometry> extends AbstractWireTestCase<T> {

Expand Down Expand Up @@ -131,123 +128,4 @@ public Object visit(Rectangle rectangle) {
assertEquals("result", result);
}

public static double randomLat() {
return randomDoubleBetween(-90, 90, true);
}

public static double randomLon() {
return randomDoubleBetween(-180, 180, true);
}

public static Circle randomCircle(boolean hasAlt) {
if (hasAlt) {
return new Circle(randomDoubleBetween(-90, 90, true), randomDoubleBetween(-180, 180, true), randomDouble(),
randomDoubleBetween(0, 100, false));
} else {
return new Circle(randomDoubleBetween(-90, 90, true), randomDoubleBetween(-180, 180, true), randomDoubleBetween(0, 100, false));
}
}

public static Line randomLine() {
return randomLine(randomBoolean());
}

public static Line randomLine(boolean hasAlts) {
int size = randomIntBetween(2, 10);
double[] lats = new double[size];
double[] lons = new double[size];
double[] alts = hasAlts ? new double[size] : null;
for (int i = 0; i < size; i++) {
lats[i] = randomLat();
lons[i] = randomLon();
if (hasAlts) {
alts[i] = randomDouble();
}
}
if (hasAlts) {
return new Line(lats, lons, alts);
}
return new Line(lats, lons);
}

public static Point randomPoint() {
return randomPoint(randomBoolean());
}

public static Point randomPoint(boolean hasAlt) {
if (hasAlt) {
return new Point(randomLat(), randomLon(), randomDouble());
} else {
return new Point(randomLat(), randomLon());
}
}

public static LinearRing randomLinearRing(boolean hasAlt) {
int size = randomIntBetween(3, 10);
double[] lats = new double[size + 1];
double[] lons = new double[size + 1];
double[] alts;
if (hasAlt) {
alts = new double[size + 1];
} else {
alts = null;
}
for (int i = 0; i < size; i++) {
lats[i] = randomLat();
lons[i] = randomLon();
if (hasAlt) {
alts[i] = randomDouble();
}
}
lats[size] = lats[0];
lons[size] = lons[0];
if (hasAlt) {
alts[size] = alts[0];
return new LinearRing(lats, lons, alts);
} else {
return new LinearRing(lats, lons);
}
}

public static Polygon randomPolygon(boolean hasAlt) {
int size = randomIntBetween(0, 10);
List<LinearRing> holes = new ArrayList<>();
for (int i = 0; i < size; i++) {
holes.add(randomLinearRing(hasAlt));
}
if (holes.size() > 0) {
return new Polygon(randomLinearRing(hasAlt), holes);
} else {
return new Polygon(randomLinearRing(hasAlt));
}
}

public static Rectangle randomRectangle() {
double lat1 = randomLat();
double lat2 = randomLat();
double minLon = randomLon();
double maxLon = randomLon();
return new Rectangle(Math.min(lat1, lat2), Math.max(lat1, lat2), minLon, maxLon);
}

public static GeometryCollection<Geometry> randomGeometryCollection(boolean hasAlt) {
return randomGeometryCollection(0, hasAlt);
}

private static GeometryCollection<Geometry> randomGeometryCollection(int level, boolean hasAlt) {
int size = randomIntBetween(1, 10);
List<Geometry> shapes = new ArrayList<>();
for (int i = 0; i < size; i++) {
@SuppressWarnings("unchecked") Function<Boolean, Geometry> geometry = randomFrom(
BaseGeometryTestCase::randomCircle,
BaseGeometryTestCase::randomLine,
BaseGeometryTestCase::randomPoint,
BaseGeometryTestCase::randomPolygon,
hasAlt ? BaseGeometryTestCase::randomPoint : (b) -> randomRectangle(),
level < 3 ? (b) -> randomGeometryCollection(level + 1, b) : BaseGeometryTestCase::randomPoint // don't build too deep
);
shapes.add(geometry.apply(hasAlt));
}
return new GeometryCollection<>(shapes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.StandardValidator;
import org.elasticsearch.geo.utils.WellKnownText;
Expand All @@ -31,7 +32,7 @@
public class GeometryCollectionTests extends BaseGeometryTestCase<GeometryCollection<Geometry>> {
@Override
protected GeometryCollection<Geometry> createTestInstance(boolean hasAlt) {
return randomGeometryCollection(hasAlt);
return GeometryTestUtils.randomGeometryCollection(hasAlt);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.GeometryValidator;
import org.elasticsearch.geo.utils.StandardValidator;
Expand All @@ -30,7 +31,7 @@
public class LineTests extends BaseGeometryTestCase<Line> {
@Override
protected Line createTestInstance(boolean hasAlt) {
return randomLine(hasAlt);
return GeometryTestUtils.randomLine(hasAlt);
}

public void testBasicSerialization() throws IOException, ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.StandardValidator;
import org.elasticsearch.geo.utils.WellKnownText;
Expand All @@ -36,7 +37,7 @@ protected MultiLine createTestInstance(boolean hasAlt) {
int size = randomIntBetween(1, 10);
List<Line> arr = new ArrayList<Line>();
for (int i = 0; i < size; i++) {
arr.add(randomLine(hasAlt));
arr.add(GeometryTestUtils.randomLine(hasAlt));
}
return new MultiLine(arr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.StandardValidator;
import org.elasticsearch.geo.utils.WellKnownText;
Expand All @@ -37,7 +38,7 @@ protected MultiPoint createTestInstance(boolean hasAlt) {
int size = randomIntBetween(1, 10);
List<Point> arr = new ArrayList<>();
for (int i = 0; i < size; i++) {
arr.add(randomPoint(hasAlt));
arr.add(GeometryTestUtils.randomPoint(hasAlt));
}
return new MultiPoint(arr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.StandardValidator;
import org.elasticsearch.geo.utils.WellKnownText;
Expand All @@ -36,7 +37,7 @@ protected MultiPolygon createTestInstance(boolean hasAlt) {
int size = randomIntBetween(1, 10);
List<Polygon> arr = new ArrayList<>();
for (int i = 0; i < size; i++) {
arr.add(randomPolygon(hasAlt));
arr.add(GeometryTestUtils.randomPolygon(hasAlt));
}
return new MultiPolygon(arr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.GeometryValidator;
import org.elasticsearch.geo.utils.StandardValidator;
Expand All @@ -30,7 +31,7 @@
public class PointTests extends BaseGeometryTestCase<Point> {
@Override
protected Point createTestInstance(boolean hasAlt) {
return randomPoint(hasAlt);
return GeometryTestUtils.randomPoint(hasAlt);
}

public void testBasicSerialization() throws IOException, ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.StandardValidator;
import org.elasticsearch.geo.utils.WellKnownText;
Expand All @@ -30,7 +31,7 @@
public class PolygonTests extends BaseGeometryTestCase<Polygon> {
@Override
protected Polygon createTestInstance(boolean hasAlt) {
return randomPolygon(hasAlt);
return GeometryTestUtils.randomPolygon(hasAlt);
}

public void testBasicSerialization() throws IOException, ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.geo.geometry;

import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.utils.GeographyValidator;
import org.elasticsearch.geo.utils.GeometryValidator;
import org.elasticsearch.geo.utils.StandardValidator;
Expand All @@ -31,7 +32,7 @@ public class RectangleTests extends BaseGeometryTestCase<Rectangle> {
@Override
protected Rectangle createTestInstance(boolean hasAlt) {
assumeFalse("3rd dimension is not supported yet", hasAlt);
return randomRectangle();
return GeometryTestUtils.randomRectangle();
}

public void testBasicSerialization() throws IOException, ParseException {
Expand Down
Loading