Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-5714: TestBBoxStrategy work-around for Spatial4j Rect bug #85

David Wayne Smiley074456d3089d
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java b/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java
index 57c169678bc..fc2de0caa76 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/bbox/TestBBoxStrategy.java
@@ -25,8 +25,10 @@ import com.spatial4j.core.shape.Rectangle;
import com.spatial4j.core.shape.Shape;
import com.spatial4j.core.shape.impl.RectangleImpl;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.search.Query;
import org.apache.lucene.spatial.SpatialMatchConcern;
import org.apache.lucene.spatial.prefix.RandomSpatialOpStrategyTestCase;
+import org.apache.lucene.spatial.query.SpatialArgs;
import org.apache.lucene.spatial.query.SpatialOperation;
import org.apache.lucene.spatial.util.ShapeAreaValueSource;
import org.junit.Ignore;
@@ -53,19 +55,27 @@ public class TestBBoxStrategy extends RandomSpatialOpStrategyTestCase {
int worldHeight = (int) Math.round(world.getHeight());
int deltaTop = nextIntInclusive(worldHeight);
int deltaBottom = nextIntInclusive(worldHeight - deltaTop);
- if (ctx.isGeo() && (deltaLeft != 0 || deltaRight != 0)) {
- //if geo & doesn't world-wrap, we shift randomly to potentially cross dateline
- int shift = nextIntInclusive(360);
- return ctx.makeRectangle(
- DistanceUtils.normLonDEG(world.getMinX() + deltaLeft + shift),
- DistanceUtils.normLonDEG(world.getMaxX() - deltaRight + shift),
- world.getMinY() + deltaBottom, world.getMaxY() - deltaTop);
- } else {
- return ctx.makeRectangle(
- world.getMinX() + deltaLeft, world.getMaxX() - deltaRight,
- world.getMinY() + deltaBottom, world.getMaxY() - deltaTop);
- }
+ double rectMinX = world.getMinX() + deltaLeft;
+ double rectMaxX = world.getMaxX() - deltaRight;
+ if (ctx.isGeo()) {
+ int shift = 0;
+ if ((deltaLeft != 0 || deltaRight != 0)) {
+ //if geo & doesn't world-wrap, we shift randomly to potentially cross dateline
+ shift = nextIntInclusive(360);
+ }
+ rectMinX = DistanceUtils.normLonDEG(rectMinX + shift);
+ rectMaxX = DistanceUtils.normLonDEG(rectMaxX + shift);
+ if (rectMinX == 180 && rectMaxX == 180) {
+ // Work-around for https://github.com/spatial4j/spatial4j/issues/85
+ rectMinX = -180;
+ rectMaxX = -180;
+ }
+ }
+ return ctx.makeRectangle(
+ rectMinX,
+ rectMaxX,
+ world.getMinY() + deltaBottom, world.getMaxY() - deltaTop);
}
/** next int, inclusive, rounds to multiple of 10 if given evenly divisible. */
@@ -158,6 +168,33 @@ public class TestBBoxStrategy extends RandomSpatialOpStrategyTestCase {
ctx.makeRectangle(170, -170, -10, 10), true);
}
+ /** See https://github.com/spatial4j/spatial4j/issues/85 */
+ @Test
+ public void testAlongDatelineOppositeSign() throws IOException {
+ // Due to Spatial4j bug #85, we can't simply do:
+ // testOperation(indexedShape,
+ // SpatialOperation.IsWithin,
+ // queryShape, true);
+
+ //both on dateline but expressed using opposite signs
+ setupGeo();
+ final Rectangle indexedShape = ctx.makeRectangle(180, 180, -10, 10);
+ final Rectangle queryShape = ctx.makeRectangle(-180, -180, -20, 20);
+ final SpatialOperation operation = SpatialOperation.IsWithin;
+ final boolean match = true;//yes it is within
+
+ //the rest is super.testOperation without leading assert:
+
+ adoc("0", indexedShape);
+ commit();
+ Query query = strategy.makeQuery(new SpatialArgs(operation, queryShape));
+ SearchResults got = executeQuery(query, 1);
+ assert got.numFound <= 1 : "unclean test env";
+ if ((got.numFound == 1) != match)
+ fail(operation+" I:" + indexedShape + " Q:" + queryShape);
+ deleteAll();//clean up after ourselves
+ }
+
private void setupGeo() {
this.ctx = SpatialContext.GEO;
this.strategy = new BBoxStrategy(ctx, "bbox");