@Override public ConstantScoreQuery makeQuery(SpatialArgs args) { if (!SpatialOperation.is( args.getOperation(), SpatialOperation.Intersects, SpatialOperation.IsWithin)) throw new UnsupportedSpatialOperation(args.getOperation()); Shape shape = args.getShape(); if (shape instanceof Rectangle) { Rectangle bbox = (Rectangle) shape; return new ConstantScoreQuery(makeWithin(bbox)); } else if (shape instanceof Circle) { Circle circle = (Circle) shape; Rectangle bbox = circle.getBoundingBox(); Query approxQuery = makeWithin(bbox); BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder(); FunctionRangeQuery vsRangeQuery = new FunctionRangeQuery( makeDistanceValueSource(circle.getCenter()), 0.0, circle.getRadius(), true, true); bqBuilder.add( approxQuery, BooleanClause.Occur.FILTER); // should have lowest "cost" value; will drive iteration bqBuilder.add(vsRangeQuery, BooleanClause.Occur.FILTER); return new ConstantScoreQuery(bqBuilder.build()); } else { throw new UnsupportedOperationException( "Only Rectangles and Circles are currently supported, " + "found [" + shape.getClass() + "]"); // TODO } }
@Test public void testRectangle() throws IOException { Shape s = read("-10 -20 10 20"); assertEquals(ctx.makeRectangle(-10, 10, -20, 20), s); assertEquals(s, writeThenRead(s)); assertTrue(s.hasArea()); }
@Test public void testCircle() throws IOException { Shape s = read("Circle(1.23 4.56 distance=7.89)"); assertEquals(ctx.makeCircle(1.23, 4.56, 7.89), s); assertEquals(s, writeThenRead(s)); assertEquals(s, read("CIRCLE( 4.56,1.23 d=7.89 )")); // use lat,lon and use 'd' abbreviation assertTrue(s.hasArea()); }
@Test public void testPoint() throws IOException { Shape s = read("10 20"); assertEquals(ctx.makePoint(10, 20), s); assertEquals(s, writeThenRead(s)); assertEquals(s, read("20,10")); // check comma for y,x format assertEquals(s, read("20, 10")); // test space assertFalse(s.hasArea()); }