Package com.spatial4j.core.shape

Examples of com.spatial4j.core.shape.Circle


  public static boolean equals(Circle thiz, Object o) {
    assert thiz != null;
    if (thiz == o) return true;
    if (!(o instanceof Circle)) return false;

    Circle circle = (Circle) o;

    if (!thiz.getCenter().equals(circle.getCenter())) return false;
    if (Double.compare(circle.getRadius(), thiz.getRadius()) != 0) return false;

    return true;
  }
View Full Code Here


              nf.format(rect.getMinY()) + " " +
              nf.format(rect.getMaxX()) + " " +
              nf.format(rect.getMaxY());
    }
    else if (shape instanceof Circle) {
      Circle c = (Circle) shape;
      return "Circle(" +
          nf.format(c.getCenter().getX()) + " " +
          nf.format(c.getCenter().getY()) + " " +
          "d=" + nf.format(c.getRadius()) +
          ")";
    }
    return shape.toString();
  }
View Full Code Here

    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();
      ValueSourceFilter vsf = new ValueSourceFilter(
          new QueryWrapperFilter(makeWithin(bbox)),
          makeDistanceValueSource(circle.getCenter()),
          0,
          circle.getRadius() );
      return new ConstantScoreQuery(vsf);
    } else {
      throw new UnsupportedOperationException("Only Rectangles and Circles are currently supported, " +
          "found [" + shape.getClass() + "]");//TODO
    }
View Full Code Here

    else if( SpatialOperation.is( op,
      SpatialOperation.Intersects,
      SpatialOperation.IsWithin ) ) {
      spatial = makeWithin(bbox);
      if( args.getShape() instanceof Circle) {
        Circle circle = (Circle)args.getShape();

        // Make the ValueSource
        valueSource = makeDistanceValueSource(shape.getCenter());

        ValueSourceFilter vsf = new ValueSourceFilter(
            new QueryWrapperFilter( spatial ), valueSource, 0, circle.getRadius() );

        spatial = new FilteredQuery( new MatchAllDocsQuery(), vsf );
      }
    }
    else if( op == SpatialOperation.IsDisjointTo ) {
View Full Code Here

      throw new IllegalArgumentException("distErr must be > 0");
    SpatialContext ctx = grid.getSpatialContext();
    if (shape instanceof Point) {
      return ctx.makeCircle((Point)shape, distErr);
    } else if (shape instanceof Circle) {
      Circle circle = (Circle) shape;
      double newDist = circle.getRadius() + distErr;
      if (ctx.isGeo() && newDist > 180)
        newDist = 180;
      return ctx.makeCircle(circle.getCenter(), newDist);
    } else {
      Rectangle bbox = shape.getBoundingBox();
      double newMinX = bbox.getMinX() - distErr;
      double newMaxX = bbox.getMaxX() + distErr;
      double newMinY = bbox.getMinY() - distErr;
View Full Code Here

  public void testArea() {
    double radius = DistanceUtils.EARTH_MEAN_RADIUS_KM * KM_TO_DEG;
    //surface of a sphere is 4 * pi * r^2
    final double earthArea = 4 * Math.PI * radius * radius;

    Circle c = ctx.makeCircle(randomIntBetween(-180,180), randomIntBetween(-90,90),
            180);//180 means whole earth
    assertEquals(earthArea, c.getArea(ctx), 1.0);
    assertEquals(earthArea, ctx.getWorldBounds().getArea(ctx), 1.0);

    //now check half earth
    Circle cHalf = ctx.makeCircle(c.getCenter(), 90);
    assertEquals(earthArea/2, cHalf.getArea(ctx), 1.0);

    //circle with same radius at +20 lat with one at -20 lat should have same area as well as bbox with same area
    Circle c2 = ctx.makeCircle(c.getCenter(), 30);
    Circle c3 = ctx.makeCircle(c.getCenter().getX(), 20, 30);
    assertEquals(c2.getArea(ctx), c3.getArea(ctx), 0.01);
    Circle c3Opposite = ctx.makeCircle(c.getCenter().getX(), -20, 30);
    assertEquals(c3.getArea(ctx), c3Opposite.getArea(ctx), 0.01);
    assertEquals(c3.getBoundingBox().getArea(ctx), c3Opposite.getBoundingBox().getArea(ctx), 0.01);

    //small shapes near the equator should have similar areas to euclidean rectangle
    Rectangle smallRect = ctx.makeRectangle(0, 1, 0, 1);
    assertEquals(1.0, smallRect.getArea(null), 0.0);
    double smallDelta = smallRect.getArea(null) - smallRect.getArea(ctx);
    assertTrue(smallDelta > 0 && smallDelta < 0.0001);

    Circle smallCircle = ctx.makeCircle(0,0,1);
    smallDelta = smallCircle.getArea(null) - smallCircle.getArea(ctx);
    assertTrue(smallDelta > 0 && smallDelta < 0.0001);

    //bigger, but still fairly similar
    //c2 = ctx.makeCircle(c.getCenter(), 30);
    double areaRatio = c2.getArea(null) / c2.getArea(ctx);
View Full Code Here

    this.strategy = new PointVectorStrategy(ctx, getClass().getSimpleName());
  }

  @Test
  public void testCircleShapeSupport() {
    Circle circle = ctx.makeCircle(ctx.makePoint(0, 0), 10);
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, circle);
    Query query = this.strategy.makeQuery(args);

    assertNotNull(query);
  }
View Full Code Here

        String multilinesGeoJson = XContentFactory.jsonBuilder().startObject().field("type", "circle")
                .startArray("coordinates").value(100.0).value(0.0).endArray()
                .field("radius", "100m")
                .endObject().string();

        Circle expected = SPATIAL_CONTEXT.makeCircle(100.0, 0.0, 360 * 100 / GeoUtils.EARTH_EQUATOR);
        assertGeometryEquals(expected, multilinesGeoJson);
    }
View Full Code Here

    }

    @Test
    public void testGeoCircle() {
        double earthCircumference = 40075016.69;
        Circle circle = ShapeBuilder.newCircleBuilder().center(0, 0).radius("100m").build();
        assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(0, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
        circle = ShapeBuilder.newCircleBuilder().center(+180, 0).radius("100m").build();
        assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(180, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
        circle = ShapeBuilder.newCircleBuilder().center(-180, 0).radius("100m").build();
        assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(-180, 0, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
        circle = ShapeBuilder.newCircleBuilder().center(0, 90).radius("100m").build();
        assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(0, 90, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
        circle = ShapeBuilder.newCircleBuilder().center(0, -90).radius("100m").build();
        assertEquals((360 * 100) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(0, -90, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
        double randomLat = (randomDouble() * 180) - 90;
        double randomLon = (randomDouble() * 360) - 180;
        double randomRadius = randomIntBetween(1, (int) earthCircumference / 4);
        circle = ShapeBuilder.newCircleBuilder().center(randomLon, randomLat).radius(randomRadius + "m").build();
        assertEquals((360 * randomRadius) / earthCircumference, circle.getRadius(), 0.00000001);
        assertEquals((Point) new PointImpl(randomLon, randomLat, ShapeBuilder.SPATIAL_CONTEXT), circle.getCenter());
    }
View Full Code Here

TOP

Related Classes of com.spatial4j.core.shape.Circle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.