Skip to content

Commit

Permalink
Make FeatureFactory tests more resilient (elastic#75405)
Browse files Browse the repository at this point in the history
making sure the spatial relationships between the tile and the generated points is honour.
  • Loading branch information
iverase authored and ywangd committed Jul 30, 2021
1 parent fdb8a97 commit 324330c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public byte[] points(List<Point> multiPoint) throws IOException {
multiPoint.sort(Comparator.comparingDouble(Point::getLon).thenComparingDouble(Point::getLat));
final int[] commands = new int[2 * multiPoint.size() + 1];
int pos = 1, prevLon = 0, prevLat = 0, numPoints = 0;
for (int i = 0; i < multiPoint.size(); i++) {
final Point point = multiPoint.get(i);
for (Point point : multiPoint) {
final int posLon = lon(point.getLon());
if (posLon > extent || posLon < 0) {
continue;
Expand All @@ -77,7 +76,8 @@ public byte[] points(List<Point> multiPoint) throws IOException {
if (posLat > extent || posLat < 0) {
continue;
}
if (i == 0 || posLon != prevLon || posLat != prevLat) {
// filter out repeated points
if (numPoints == 0 || posLon != prevLon || posLat != prevLat) {
commands[pos++] = BitUtil.zigZagEncode(posLon - prevLon);
commands[pos++] = BitUtil.zigZagEncode(posLat - prevLat);
prevLon = posLon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,40 @@

public class FeatureFactoryTests extends ESTestCase {

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75325")
public void testPoint() {
int z = randomIntBetween(1, 10);
int z = randomIntBetween(3, 10);
int x = randomIntBetween(0, (1 << z) - 1);
int y = randomIntBetween(0, (1 << z) - 1);
int extent = randomIntBetween(1 << 8, 1 << 14);
FeatureFactory builder = new FeatureFactory(z, x, y, extent);
Rectangle rectangle = GeoTileUtils.toBoundingBox(x, y, z);
{
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() > l || rectangle.getMaxY() < l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() > l || rectangle.getMaxX() < l, GeoTestUtil::nextLongitude);
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() >= l || rectangle.getMaxY() <= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() >= l || rectangle.getMaxX() <= l, GeoTestUtil::nextLongitude);
List<VectorTile.Tile.Feature> features = builder.getFeatures(new Point(lon, lat), new UserDataIgnoreConverter());
assertThat(features.size(), Matchers.equalTo(1));
VectorTile.Tile.Feature feature = features.get(0);
assertThat(feature.getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POINT));
}
{
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() <= l && rectangle.getMaxY() >= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() <= l && rectangle.getMaxX() >= l, GeoTestUtil::nextLongitude);
int xNew = randomValueOtherThanMany(v -> Math.abs(v - x) < 2, () -> randomIntBetween(0, (1 << z) - 1));
int yNew = randomValueOtherThanMany(v -> Math.abs(v - y) < 2, () -> randomIntBetween(0, (1 << z) - 1));
Rectangle rectangleNew = GeoTileUtils.toBoundingBox(xNew, yNew, z);
double lat = randomValueOtherThanMany(
(l) -> rectangleNew.getMinY() >= l || rectangleNew.getMaxY() <= l,
GeoTestUtil::nextLatitude
);
double lon = randomValueOtherThanMany(
(l) -> rectangleNew.getMinX() >= l || rectangleNew.getMaxX() <= l,
GeoTestUtil::nextLongitude
);
List<VectorTile.Tile.Feature> features = builder.getFeatures(new Point(lon, lat), new UserDataIgnoreConverter());
assertThat(features.size(), Matchers.equalTo(0));
}
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75325")
public void testMultiPoint() {
int z = randomIntBetween(1, 10);
int z = randomIntBetween(3, 10);
int x = randomIntBetween(0, (1 << z) - 1);
int y = randomIntBetween(0, (1 << z) - 1);
int extent = randomIntBetween(1 << 8, 1 << 14);
Expand All @@ -66,8 +73,8 @@ public void testMultiPoint() {
int numPoints = randomIntBetween(2, 10);
{
List<Point> points = new ArrayList<>();
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() > l || rectangle.getMaxY() < l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() > l || rectangle.getMaxX() < l, GeoTestUtil::nextLongitude);
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() >= l || rectangle.getMaxY() <= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() >= l || rectangle.getMaxX() <= l, GeoTestUtil::nextLongitude);
points.add(new Point(lon, lat));
for (int i = 0; i < numPoints - 1; i++) {
points.add(new Point(GeoTestUtil.nextLongitude(), GeoTestUtil.nextLatitude()));
Expand All @@ -78,14 +85,17 @@ public void testMultiPoint() {
assertThat(feature.getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POINT));
}
{
int xNew = randomValueOtherThanMany(v -> Math.abs(v - x) < 2, () -> randomIntBetween(0, (1 << z) - 1));
int yNew = randomValueOtherThanMany(v -> Math.abs(v - y) < 2, () -> randomIntBetween(0, (1 << z) - 1));
Rectangle rectangleNew = GeoTileUtils.toBoundingBox(xNew, yNew, z);
List<Point> points = new ArrayList<>();
for (int i = 0; i < numPoints; i++) {
double lat = randomValueOtherThanMany(
(l) -> rectangle.getMinY() <= l && rectangle.getMaxY() >= l,
(l) -> rectangleNew.getMinY() >= l || rectangleNew.getMaxY() <= l,
GeoTestUtil::nextLatitude
);
double lon = randomValueOtherThanMany(
(l) -> rectangle.getMinX() <= l && rectangle.getMaxX() >= l,
(l) -> rectangleNew.getMinX() >= l || rectangleNew.getMaxX() <= l,
GeoTestUtil::nextLongitude
);
points.add(new Point(lon, lat));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,36 @@

public class SimpleFeatureFactoryTests extends ESTestCase {

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75358")
public void testPoint() throws IOException {
int z = randomIntBetween(1, 10);
int z = randomIntBetween(3, 10);
int x = randomIntBetween(0, (1 << z) - 1);
int y = randomIntBetween(0, (1 << z) - 1);
int extent = randomIntBetween(1 << 8, 1 << 14);
SimpleFeatureFactory builder = new SimpleFeatureFactory(z, x, y, extent);
Rectangle rectangle = GeoTileUtils.toBoundingBox(x, y, z);
{
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() > l || rectangle.getMaxY() < l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() > l || rectangle.getMaxX() < l, GeoTestUtil::nextLongitude);
double lat = randomValueOtherThanMany(l -> rectangle.getMinY() >= l || rectangle.getMaxY() <= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany(l -> rectangle.getMinX() >= l || rectangle.getMaxX() <= l, GeoTestUtil::nextLongitude);
assertThat(builder.point(lon, lat).length, Matchers.greaterThan(0));
}
{
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() <= l && rectangle.getMaxY() >= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() <= l && rectangle.getMaxX() >= l, GeoTestUtil::nextLongitude);
int xNew = randomValueOtherThanMany(v -> Math.abs(v - x) < 2, () -> randomIntBetween(0, (1 << z) - 1));
int yNew = randomValueOtherThanMany(v -> Math.abs(v - y) < 2, () -> randomIntBetween(0, (1 << z) - 1));
Rectangle rectangleNew = GeoTileUtils.toBoundingBox(xNew, yNew, z);
double lat = randomValueOtherThanMany(
l -> rectangleNew.getMinY() >= l || rectangleNew.getMaxY() <= l,
GeoTestUtil::nextLatitude
);
double lon = randomValueOtherThanMany(
(l) -> rectangleNew.getMinX() >= l || rectangleNew.getMaxX() <= l,
GeoTestUtil::nextLongitude
);
assertThat(builder.point(lon, lat).length, Matchers.equalTo(0));
}
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75358")
public void testMultiPoint() throws IOException {
int z = randomIntBetween(1, 10);
int z = randomIntBetween(3, 10);
int x = randomIntBetween(0, (1 << z) - 1);
int y = randomIntBetween(0, (1 << z) - 1);
int extent = randomIntBetween(1 << 8, 1 << 14);
Expand All @@ -51,23 +58,26 @@ public void testMultiPoint() throws IOException {
int numPoints = randomIntBetween(2, 10);
{
List<Point> points = new ArrayList<>();
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() > l || rectangle.getMaxY() < l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() > l || rectangle.getMaxX() < l, GeoTestUtil::nextLongitude);
double lat = randomValueOtherThanMany((l) -> rectangle.getMinY() >= l || rectangle.getMaxY() <= l, GeoTestUtil::nextLatitude);
double lon = randomValueOtherThanMany((l) -> rectangle.getMinX() >= l || rectangle.getMaxX() <= l, GeoTestUtil::nextLongitude);
points.add(new Point(lon, lat));
for (int i = 0; i < numPoints - 1; i++) {
points.add(new Point(GeoTestUtil.nextLongitude(), GeoTestUtil.nextLatitude()));
}
assertThat(builder.points(points).length, Matchers.greaterThan(0));
}
{
int xNew = randomValueOtherThanMany(v -> Math.abs(v - x) < 2, () -> randomIntBetween(0, (1 << z) - 1));
int yNew = randomValueOtherThanMany(v -> Math.abs(v - y) < 2, () -> randomIntBetween(0, (1 << z) - 1));
Rectangle rectangleNew = GeoTileUtils.toBoundingBox(xNew, yNew, z);
List<Point> points = new ArrayList<>();
for (int i = 0; i < numPoints; i++) {
double lat = randomValueOtherThanMany(
(l) -> rectangle.getMinY() <= l && rectangle.getMaxY() >= l,
(l) -> rectangleNew.getMinY() >= l || rectangleNew.getMaxY() <= l,
GeoTestUtil::nextLatitude
);
double lon = randomValueOtherThanMany(
(l) -> rectangle.getMinX() <= l && rectangle.getMaxX() >= l,
(l) -> rectangleNew.getMinX() >= l || rectangleNew.getMaxX() <= l,
GeoTestUtil::nextLongitude
);
points.add(new Point(lon, lat));
Expand Down

0 comments on commit 324330c

Please sign in to comment.