protected void TestRectangle(double minX, double width, double minY, double height) { double maxX = minX + width; double maxY = minY + height; minX = normX(minX); maxX = normX(maxX); Rectangle r = ctx.MakeRectangle(minX, maxX, minY, maxY); //test equals & hashcode of duplicate Rectangle r2 = ctx.MakeRectangle(minX, maxX, minY, maxY); Assert.Equal(r, r2); Assert.Equal(r.GetHashCode(), r2.GetHashCode()); String msg = r.ToString(); Assert.Equal(/*msg,*/ width != 0 && height != 0, r.HasArea()); Assert.Equal(/*msg,*/ width != 0 && height != 0, r.GetArea(ctx) > 0); if (ctx.IsGeo() && r.GetWidth() == 360 && r.GetHeight() == 180) { //whole globe double earthRadius = DistanceUtils.ToDegrees(1); CustomAssert.EqualWithDelta(4 * Math.PI * earthRadius * earthRadius, r.GetArea(ctx), 1.0); //1km err } AssertEqualsRatio(msg, height, r.GetHeight()); AssertEqualsRatio(msg, width, r.GetWidth()); Point center = r.GetCenter(); msg += " ctr:" + center; //System.out.println(msg); assertRelation(msg, SpatialRelation.CONTAINS, r, center); DistanceCalculator dc = ctx.GetDistCalc(); double dUR = dc.Distance(center, r.GetMaxX(), r.GetMaxY()); double dLR = dc.Distance(center, r.GetMaxX(), r.GetMinY()); double dUL = dc.Distance(center, r.GetMinX(), r.GetMaxY()); double dLL = dc.Distance(center, r.GetMinX(), r.GetMinY()); Assert.Equal(/*msg,*/ width != 0 || height != 0, dUR != 0); if (dUR != 0) { Assert.True(dUR > 0 && dLL > 0); } AssertEqualsRatio(msg, dUR, dUL); AssertEqualsRatio(msg, dLR, dLL); if (!ctx.IsGeo() || center.GetY() == 0) { AssertEqualsRatio(msg, dUR, dLL); } }
public static double GetDistance(double fromLat, double fromLng, double toLat, double toLng) { var ptFrom = RavenSpatialContext.MakePoint(fromLng, fromLat); var ptTo = RavenSpatialContext.MakePoint(toLng, toLat); return(RavenSpatialContext.GetDistCalc().Distance(ptFrom, ptTo)); }
//we don't have a line shape so we use a rectangle for these axis public CircleImpl(Point p, double radiusDEG, SpatialContext ctx) { //We assume any validation of params already occurred (including bounding dist) this.ctx = ctx; this.point = p; this.radiusDEG = radiusDEG; this.enclosingBox = ctx.GetDistCalc().CalcBoxByDistFromPt(point, this.radiusDEG, ctx, null); }
public double Calculate(SpatialContext ctx, Document document, Point origin, bool round = false) { var point = document.GetPoint(ctx); var calc = ctx.GetDistCalc(); var distance = calc.Distance(origin, point); var distanceKm = DistanceUtils.Degrees2Dist(distance, DistanceUtils.EARTH_EQUATORIAL_RADIUS_KM); return(round ? Math.Round(distanceKm * 1000d, 2, MidpointRounding.AwayFromZero) : distanceKm); }
/// <summary> /// Returns a ValueSource with values ranging from 1 to 0, depending inversely /// on the distance from {@link #makeDistanceValueSource(com.spatial4j.core.shape.Point)}. /// The formula is <c>c/(d + c)</c> where 'd' is the distance and 'c' is /// one tenth the distance to the farthest edge from the center. Thus the /// scores will be 1 for indexed points at the center of the query shape and as /// low as ~0.1 at its furthest edges. /// </summary> /// <param name="queryShape"></param> /// <returns></returns> public ValueSource MakeRecipDistanceValueSource(Shape queryShape) { Rectangle bbox = queryShape.GetBoundingBox(); double diagonalDist = ctx.GetDistCalc().Distance( ctx.MakePoint(bbox.GetMinX(), bbox.GetMinY()), bbox.GetMaxX(), bbox.GetMaxY()); double distToEdge = diagonalDist * 0.5; float c = (float)distToEdge * 0.1f; //one tenth return(new ReciprocalFloatFunction(MakeDistanceValueSource(queryShape.GetCenter()), 1f, c, c)); }
public void TestDefault() { SpatialContext s = SpatialContext.GEO; SpatialContext t = Call();//default Assert.Equal(s.GetType(), t.GetType()); Assert.Equal(s.IsGeo(), t.IsGeo()); Assert.Equal(s.GetDistCalc(), t.GetDistCalc()); Assert.Equal(s.GetWorldBounds(), t.GetWorldBounds()); }
public double GetArea(SpatialContext ctx) { if (ctx == null) { return(Math.PI * radiusDEG * radiusDEG); } else { return(ctx.GetDistCalc().Area(this)); } }
public void TestCustom() { SpatialContext sc = Call("geo", "false"); Assert.True(!sc.IsGeo()); Assert.Equal(new CartesianDistCalc(), sc.GetDistCalc()); sc = Call("geo", "false", "distCalculator", "cartesian^2", "worldBounds", "-100 0 75 200");//West South East North Assert.Equal(new CartesianDistCalc(true), sc.GetDistCalc()); Assert.Equal(new RectangleImpl(-100, 75, 0, 200, sc), sc.GetWorldBounds()); sc = Call("geo", "true", "distCalculator", "lawOfCosines"); Assert.True(sc.IsGeo()); var test = new GeodesicSphereDistCalc.LawOfCosines(); Assert.Equal(test, sc.GetDistCalc()); }
public double GetArea(SpatialContext ctx) { if (ctx == null) { return(GetWidth() * GetHeight()); } else { return(ctx.GetDistCalc().Area(this)); } }
/// <summary> /// Computes the distance given a shape and the {@code distErrPct}. The /// algorithm is the fraction of the distance from the center of the query /// shape to its furthest bounding box corner. /// </summary> /// <param name="shape">Mandatory.</param> /// <param name="distErrPct">0 to 0.5</param> /// <param name="ctx">Mandatory</param> /// <returns>A distance (in degrees).</returns> public static double CalcDistanceFromErrPct(Shape shape, double distErrPct, SpatialContext ctx) { if (distErrPct < 0 || distErrPct > 0.5) { throw new ArgumentException("distErrPct " + distErrPct + " must be between [0 to 0.5]", "distErrPct"); } if (distErrPct == 0 || shape is Point) { return(0); } Rectangle bbox = shape.GetBoundingBox(); //The diagonal distance should be the same computed from any opposite corner, // and this is the longest distance that might be occurring within the shape. double diagonalDist = ctx.GetDistCalc().Distance( ctx.MakePoint(bbox.GetMinX(), bbox.GetMinY()), bbox.GetMaxX(), bbox.GetMaxY()); return(diagonalDist * 0.5 * distErrPct); }
public virtual void Reset(double x, double y, double radiusDEG) { point.Reset(x, y); this.radiusDEG = radiusDEG; this.enclosingBox = ctx.GetDistCalc().CalcBoxByDistFromPt(point, this.radiusDEG, ctx, enclosingBox); }
private const double EPS = 10e-4; //delta when doing double assertions. Geo eps is not that small.; private DistanceCalculator dc() { return(ctx.GetDistCalc()); }
public DistanceSimilarity(SpatialContext ctx, Point queryPoint) { this.queryPoint = queryPoint; this.distCalc = ctx.GetDistCalc(); this.nullValue = (ctx.IsGeo() ? 180 : double.MaxValue); }
/// <summary> /// Computes the distance given a shape and the {@code distErrPct}. The /// algorithm is the fraction of the distance from the center of the query /// shape to its furthest bounding box corner. /// </summary> /// <param name="shape">Mandatory.</param> /// <param name="distErrPct">0 to 0.5</param> /// <param name="ctx">Mandatory</param> /// <returns>A distance (in degrees).</returns> public static double CalcDistanceFromErrPct(Shape shape, double distErrPct, SpatialContext ctx) { if (distErrPct < 0 || distErrPct > 0.5) { throw new ArgumentException("distErrPct " + distErrPct + " must be between [0 to 0.5]", "distErrPct"); } if (distErrPct == 0 || shape is Point) { return 0; } Rectangle bbox = shape.GetBoundingBox(); //The diagonal distance should be the same computed from any opposite corner, // and this is the longest distance that might be occurring within the shape. double diagonalDist = ctx.GetDistCalc().Distance( ctx.MakePoint(bbox.GetMinX(), bbox.GetMinY()), bbox.GetMaxX(), bbox.GetMaxY()); return diagonalDist * 0.5 * distErrPct; }