public void testMakeRect(SpatialContext ctx) { this.ctx = ctx; //test rectangle constructor Assert.Equal(new RectangleImpl(1, 3, 2, 4), new RectangleImpl(new PointImpl(1, 2), new PointImpl(3, 4))); //test ctx.makeRect Assert.Equal(ctx.MakeRect(1, 3, 2, 4), ctx.MakeRect(ctx.MakePoint(1, 2), ctx.MakePoint(3, 4))); }
public static Rectangle CalcBoxByDistFromPtDEG(double lat, double lon, double distance, SpatialContext ctx) { //See http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates Section 3.1, 3.2 and 3.3 double radius = ctx.GetUnits().EarthRadius(); double dist_deg = Dist2Degrees(distance, radius); if (dist_deg == 0) return ctx.MakeRect(lon, lon, lat, lat);//essentially a point if (dist_deg >= 180)//distance is >= opposite side of the globe return ctx.GetWorldBounds(); //--calc latitude bounds double latN_deg = lat + dist_deg; double latS_deg = lat - dist_deg; if (latN_deg >= 90 || latS_deg <= -90) {//touches either pole //we have special logic for longitude double lonW_deg = -180, lonE_deg = 180;//world wrap: 360 deg if (latN_deg <= 90 && latS_deg >= -90) {//doesn't pass either pole: 180 deg lonW_deg = lon - 90; lonE_deg = lon + 90; } if (latN_deg > 90) latN_deg = 90; if (latS_deg < -90) latS_deg = -90; return ctx.MakeRect(lonW_deg, lonE_deg, latS_deg, latN_deg); } else { //--calc longitude bounds double lon_delta_deg = CalcBoxByDistFromPt_deltaLonDEG(lat, lon, distance, radius); double lonW_deg = lon - lon_delta_deg; double lonE_deg = lon + lon_delta_deg; return ctx.MakeRect(lonW_deg, lonE_deg, latS_deg, latN_deg);//ctx will normalize longitude } }
/// <summary> /// WARNING: geoms is copied by reference. /// </summary> /// <param name="geoms"></param> /// <param name="ctx"></param> public MultiShape(IEnumerable<Shape> geoms, SpatialContext ctx) { if (!geoms.Any()) throw new ArgumentException("must be given at least 1 shape", "geoms"); this.geoms = geoms; //compute and cache bbox double minX = Double.PositiveInfinity; double minY = Double.PositiveInfinity; double maxX = Double.NegativeInfinity; double maxY = Double.NegativeInfinity; foreach (var geom in geoms) { Rectangle r = geom.GetBoundingBox(); minX = Math.Min(minX, r.GetMinX()); minY = Math.Min(minY, r.GetMinY()); maxX = Math.Max(maxX, r.GetMaxX()); maxY = Math.Max(maxY, r.GetMaxY()); } this.bbox = ctx.MakeRect(minX, maxX, minY, maxY); }
public override Rectangle CalcBoxByDistFromPt(Point @from, double distance, SpatialContext ctx) { return ctx.MakeRect(from.GetX() - distance, from.GetX() + distance, from.GetY() - distance, from.GetY() + distance); }
public void TestSimpleCircle(SpatialContext ctx) { base.ctx = ctx; double[] theXs = new double[] { -10, 0, 10 }; foreach (double x in theXs) { double[] theYs = new double[] { -20, 0, 20 }; foreach (double y in theYs) { TestCircle(x, y, 0); TestCircle(x, y, 5); } } //INTERSECTION: //Start with some static tests that have shown to cause failures at some point: Assert.Equal( /*"getX not getY",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(107, -81, 147).Relate(ctx.MakeRect(92, 121, -89, 74), ctx)); TestCircleIntersect(); }
public void TestGeoCircle(SpatialContext ctx) { base.ctx = ctx; //--Start with some static tests that once failed: //Bug: numeric edge at pole, fails to init ctx.MakeCircle(110, -12, ctx.GetDistCalc().DegreesToDistance(90 + 12)); //Bug: horizXAxis not in enclosing rectangle, assertion ctx.MakeCircle(-44, 16, DegToDist(106)); ctx.MakeCircle(-36, -76, DegToDist(14)); ctx.MakeCircle(107, 82, DegToDist(172)); // TODO need to update this test to be valid //{ // //Bug in which distance was being confused as being in the same coordinate system as x,y. // double distDeltaToPole = 0.001;//1m // double distDeltaToPoleDEG = ctx.getDistCalc().distanceToDegrees(distDeltaToPole); // double dist = 1;//1km // double distDEG = ctx.getDistCalc().distanceToDegrees(dist); // Circle c = ctx.makeCircle(0, 90 - distDeltaToPoleDEG - distDEG, dist); // Rectangle cBBox = c.getBoundingBox(); // Rectangle r = ctx.makeRect(cBBox.getMaxX() * 0.99, cBBox.getMaxX() + 1, c.getCenter().getY(), c.getCenter().getY()); // assertEquals(INTERSECTS, c.getBoundingBox().relate(r, ctx)); // assertEquals("dist != xy space", INTERSECTS, c.relate(r, ctx));//once failed here //} Assert.Equal(/*"nudge back circle", */ SpatialRelation.CONTAINS, ctx.MakeCircle(-150, -90, DegToDist(122)).Relate(ctx.MakeRect(0, -132, 32, 32), ctx)); Assert.Equal(/* "wrong estimate", */ SpatialRelation.DISJOINT, ctx.MakeCircle(-166, 59, 5226.2).Relate(ctx.MakeRect(36, 66, 23, 23), ctx)); Assert.Equal(/*"bad CONTAINS (dateline)",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(56, -50, 12231.5).Relate(ctx.MakeRect(108, 26, 39, 48), ctx)); Assert.Equal(/*"bad CONTAINS (backwrap2)",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(112, -3, DegToDist(91)).Relate(ctx.MakeRect(-163, 29, -38, 10), ctx)); Assert.Equal(/*"bad CONTAINS (r x-wrap)",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(-139, 47, DegToDist(80)).Relate(ctx.MakeRect(-180, 180, -3, 12), ctx)); Assert.Equal(/*"bad CONTAINS (pwrap)",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(-139, 47, DegToDist(80)).Relate(ctx.MakeRect(-180, 179, -3, 12), ctx)); Assert.Equal(/*"no-dist 1",*/ SpatialRelation.WITHIN, ctx.MakeCircle(135, 21, 0).Relate(ctx.MakeRect(-103, -154, -47, 52), ctx)); Assert.Equal(/*"bbox <= >= -90 bug",*/ SpatialRelation.CONTAINS, ctx.MakeCircle(-64, -84, DegToDist(124)).Relate(ctx.MakeRect(-96, 96, -10, -10), ctx)); //The horizontal axis line of a geo circle doesn't necessarily pass through c's ctr. Assert.Equal(/*"c's horiz axis doesn't pass through ctr",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(71, -44, DegToDist(40)).Relate(ctx.MakeRect(15, 27, -62, -34), ctx)); Assert.Equal(/*"pole boundary",*/ SpatialRelation.INTERSECTS, ctx.MakeCircle(-100, -12, DegToDist(102)).Relate(ctx.MakeRect(143, 175, 4, 32), ctx)); Assert.Equal(/*"full circle assert",*/ SpatialRelation.CONTAINS, ctx.MakeCircle(-64, 32, DegToDist(180)).Relate(ctx.MakeRect(47, 47, -14, 90), ctx)); //--Now proceed with systematic testing: double distToOpposeSide = ctx.GetUnits().EarthRadius() * Math.PI; Assert.Equal(ctx.GetWorldBounds(), ctx.MakeCircle(0, 0, distToOpposeSide).GetBoundingBox()); //assertEquals(ctx.makeCircle(0,0,distToOpposeSide/2 - 500).getBoundingBox()); double[] theXs = new double[] { -180, -45, 90 }; foreach (double x in theXs) { double[] theYs = new double[] { -90, -45, 0, 45, 90 }; foreach (double y in theYs) { TestCircle(x, y, 0); TestCircle(x, y, 500); TestCircle(x, y, DegToDist(90)); TestCircle(x, y, ctx.GetUnits().EarthRadius() * 6); } } TestCircleIntersect(); }
public void TestGeoRectangle(SpatialContext ctx) { base.ctx = ctx; //First test some relateXRange // opposite +/- 180 Assert.Equal(SpatialRelation.INTERSECTS, ctx.MakeRect(170, 180, 0, 0).RelateXRange(-180, -170, ctx)); Assert.Equal(SpatialRelation.INTERSECTS, ctx.MakeRect(-90, -45, 0, 0).RelateXRange(-45, -135, ctx)); Assert.Equal(SpatialRelation.CONTAINS, ctx.GetWorldBounds().RelateXRange(-90, -135, ctx)); //point on edge at dateline using opposite +/- 180 Assert.Equal(SpatialRelation.CONTAINS, ctx.MakeRect(170, 180, 0, 0).Relate(ctx.MakePoint(-180, 0), ctx)); //test 180 becomes -180 for non-zero width rectangle Assert.Equal(ctx.MakeRect(-180, -170, 0, 0), ctx.MakeRect(180, -170, 0, 0)); Assert.Equal(ctx.MakeRect(170, 180, 0, 0), ctx.MakeRect(170, -180, 0, 0)); double[] lons = new double[] { 0, 45, 160, 180, -45, -175, -180 }; //minX foreach (double lon in lons) { double[] lonWs = new double[] { 0, 20, 180, 200, 355, 360 }; //width foreach (double lonW in lonWs) { TestRectangle(lon, lonW, 0, 0); TestRectangle(lon, lonW, -10, 10); TestRectangle(lon, lonW, 80, 10); //polar cap TestRectangle(lon, lonW, -90, 180); //full lat range } } //Test geo rectangle intersections TestRectIntersect(); }
/** Returns min-max lat, min-max lon. */ public static Rectangle DecodeBoundary(String geohash, SpatialContext ctx) { double minY = -90, maxY = 90, minX = -180, maxX = 180; bool isEven = true; for (int i = 0; i < geohash.Length; i++) { char c = geohash[i]; if (c >= 'A' && c <= 'Z') c = Convert.ToChar(c - Convert.ToChar('A' - 'a')); int cd = BASE_32_IDX[c - BASE_32[0]]; //TODO check successful? foreach (var mask in BITS) { if (isEven) { if ((cd & mask) != 0) { minX = (minX + maxX) / 2D; } else { maxX = (minX + maxX) / 2D; } } else { if ((cd & mask) != 0) { minY = (minY + maxY) / 2D; } else { maxY = (minY + maxY) / 2D; } } isEven = !isEven; } } return ctx.MakeRect(minX, maxX, minY, maxY); }