private void AssertNtsConsistentRelate(IShape shape) { IntersectionMatrix expectedM = POLY_SHAPE.Geometry.Relate(((NtsSpatialContext)ctx).GetGeometryFrom(shape)); SpatialRelation expectedSR = NtsGeometry.IntersectionMatrixToSpatialRelation(expectedM); //NTS considers a point on a boundary INTERSECTS, not CONTAINS if (expectedSR == SpatialRelation.INTERSECTS && shape is Core.Shapes.IPoint) { expectedSR = SpatialRelation.CONTAINS; } AssertRelation(null, expectedSR, POLY_SHAPE, shape); if (ctx.IsGeo) { //shift shape, set to shape2 IShape shape2; if (shape is IRectangle) { IRectangle r = (IRectangle)shape; shape2 = MakeNormRect(r.MinX + DL_SHIFT, r.MaxX + DL_SHIFT, r.MinY, r.MaxY); } else if (shape is Core.Shapes.IPoint) { Core.Shapes.IPoint p = (Core.Shapes.IPoint)shape; shape2 = ctx.MakePoint(base.NormX(p.X + DL_SHIFT), p.Y); } else { throw new Exception("" + shape); } AssertRelation(null, expectedSR, POLY_SHAPE_DL, shape2); } }
#pragma warning disable xUnit1013 public virtual void TestRelationsImpl(bool prepare) #pragma warning restore xUnit1013 { Debug.Assert(!((NtsWktShapeParser)ctx.WktShapeParser).IsAutoIndex); //base polygon NtsGeometry @base = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((0 0, 10 0, 5 5, 0 0))"); //shares only "10 0" with base NtsGeometry polyI = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((10 0, 20 0, 15 5, 10 0))"); //within base: differs from base by one point is within NtsGeometry polyW = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((0 0, 9 0, 5 5, 0 0))"); //a boundary point of base Core.Shapes.IPoint pointB = ctx.MakePoint(0, 0); //a shared boundary line of base NtsGeometry lineB = (NtsGeometry)ctx.ReadShapeFromWkt("LINESTRING(0 0, 10 0)"); //a line sharing only one point with base NtsGeometry lineI = (NtsGeometry)ctx.ReadShapeFromWkt("LINESTRING(10 0, 20 0)"); if (prepare) { @base.Index(); } AssertRelation(SpatialRelation.CONTAINS, @base, @base);//preferred result as there is no EQUALS AssertRelation(SpatialRelation.INTERSECTS, @base, polyI); AssertRelation(SpatialRelation.CONTAINS, @base, polyW); AssertRelation(SpatialRelation.CONTAINS, @base, pointB); AssertRelation(SpatialRelation.CONTAINS, @base, lineB); AssertRelation(SpatialRelation.INTERSECTS, @base, lineI); if (prepare) { lineB.Index(); } AssertRelation(SpatialRelation.CONTAINS, lineB, lineB);//line contains itself AssertRelation(SpatialRelation.CONTAINS, lineB, pointB); }
private NtsGeometry POLY_SHAPE_DL; //POLY_SHAPE shifted by DL_SHIFT to cross the dateline public NtsGeometryTest() : base(NtsSpatialContext.GEO) { POLY_SHAPE = (NtsGeometry)ctx.ReadShapeFromWkt(POLY_STR); if (ctx.IsGeo) { POLY_SHAPE_DL = ShiftPoly(POLY_SHAPE, DL_SHIFT); Assert.True(POLY_SHAPE_DL.BoundingBox.CrossesDateLine); } }
public virtual void TestWidthGreaterThan180() { //does NOT cross the dateline but is a wide shape >180 NtsGeometry ntsGeo = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((-161 49, 0 49, 20 49, 20 89.1, 0 89.1, -161 89.2, -161 49))"); CustomAssert.EqualWithDelta(161 + 20, ntsGeo.BoundingBox.Width, 0.001); //shift it to cross the dateline and check that it's still good ntsGeo = ShiftPoly(ntsGeo, 180); CustomAssert.EqualWithDelta(161 + 20, ntsGeo.BoundingBox.Width, 0.001); }
private NtsGeometry ShiftPoly(NtsGeometry poly, int lon_shift) { IGeometry pGeom = poly.Geometry; Assert.True(pGeom.IsValid); //shift 180 to the right pGeom = (IGeometry)pGeom.Clone(); pGeom.Apply(new CoordinateFilterAnonymousHelper(this, lon_shift)); pGeom.GeometryChanged(); Assert.False(pGeom.IsValid); return((NtsGeometry)ctx.ReadShapeFromWkt(pGeom.AsText())); }
#pragma warning disable 672 public override string ToString(IShape shape) #pragma warning restore 672 { //Note: this logic is from the defunct NtsShapeReadWriter if (shape is NtsGeometry) { NtsGeometry ntsGeom = (NtsGeometry)shape; return(ntsGeom.Geometry.AsText()); } //Note: doesn't handle ShapeCollection or BufferedLineString #pragma warning disable 612, 618 return(base.ToString(shape)); #pragma warning restore 612, 618 }
public void testArea() { //simple bbox Rectangle r = RandomRectangle(20); var ctxJts = (NtsSpatialContext)ctx; var rPoly = new NtsGeometry(ctxJts.GetGeometryFrom(r), ctxJts, false); CustomAssert.EqualWithDelta(r.GetArea(null), rPoly.GetArea(null), 0.0); CustomAssert.EqualWithDelta(r.GetArea(ctx), rPoly.GetArea(ctx), 0.000001); //same since fills 100% CustomAssert.EqualWithDelta(1300, POLY_SHAPE.GetArea(null), 0.0); //fills 27% CustomAssert.EqualWithDelta(0.27, POLY_SHAPE.GetArea(ctx) / POLY_SHAPE.GetBoundingBox().GetArea(ctx), 0.009); Assert.True(POLY_SHAPE.GetBoundingBox().GetArea(ctx) > POLY_SHAPE.GetArea(ctx)); }
public virtual void TestArea() { //simple bbox IRectangle r = RandomRectangle(20); NtsSpatialContext ctxNts = (NtsSpatialContext)ctx; NtsGeometry rPoly = ctxNts.MakeShape(ctxNts.GetGeometryFrom(r), false, false); CustomAssert.EqualWithDelta(r.GetArea(null), rPoly.GetArea(null), 0.0); CustomAssert.EqualWithDelta(r.GetArea(ctx), rPoly.GetArea(ctx), 0.000001);//same since fills 100% CustomAssert.EqualWithDelta(1300, POLY_SHAPE.GetArea(null), 0.0); //fills 27% CustomAssert.EqualWithDelta(0.27, POLY_SHAPE.GetArea(ctx) / POLY_SHAPE.BoundingBox.GetArea(ctx), 0.009); Assert.True(POLY_SHAPE.BoundingBox.GetArea(ctx) > POLY_SHAPE.GetArea(ctx)); }
public NtsPolygonTest() : base(NtsSpatialContext.GEO) { POLY_SHAPE = (NtsGeometry)ctx.ReadShape(POLY_STR); if (TEST_DL_POLY && ctx.IsGeo()) { var pGeom = POLY_SHAPE.GetGeom(); Assert.True(pGeom.IsValid); //shift 180 to the right pGeom = (IGeometry)pGeom.Clone(); pGeom.Apply(new NtsPolygonTestCoordinateFilter(this)); pGeom.GeometryChanged(); Assert.False(pGeom.IsValid); POLY_SHAPE_DL = (NtsGeometry)ctx.ReadShape(pGeom.AsText()); Assert.True( POLY_SHAPE_DL.GetBoundingBox().GetCrossesDateLine() || 360 == POLY_SHAPE_DL.GetBoundingBox().GetWidth()); } }
public void testRussia() { //TODO THE RUSSIA TEST DATA SET APPEARS CORRUPT // But this test "works" anyhow, and exercises a ton. //Russia exercises JtsGeometry fairly well because of these characteristics: // * a MultiPolygon // * crosses the dateline // * has coordinates needing normalization (longitude +180.000xxx) // * some geometries might(?) not be "valid" (requires union to overcome) String wktStr = readFirstLineFromRsrc("russia.wkt.txt"); NtsGeometry jtsGeom = (NtsGeometry)ctx.ReadShape(wktStr); //Unexplained holes revealed via KML export: // TODO Test contains: 64°12'44.82"N 61°29'5.20"E // 64.21245 61.48475 // FAILS //assertRelation(null,SpatialRelation.CONTAINS, jtsGeom, ctx.makePoint(61.48, 64.21)); }
private void assertJtsConsistentRelate(Shape shape) { IntersectionMatrix expectedM = POLY_SHAPE.GetGeom().Relate(((NtsSpatialContext)ctx).GetGeometryFrom(shape)); SpatialRelation expectedSR = NtsGeometry.IntersectionMatrixToSpatialRelation(expectedM); //JTS considers a point on a boundary INTERSECTS, not CONTAINS if (expectedSR == SpatialRelation.INTERSECTS && shape is Point) { expectedSR = SpatialRelation.CONTAINS; } assertRelation(null, expectedSR, POLY_SHAPE, shape); if (TEST_DL_POLY && ctx.IsGeo()) { //shift shape, set to shape2 Shape shape2; if (shape is Rectangle) { Rectangle r = (Rectangle)shape; shape2 = makeNormRect(r.GetMinX() + DL_SHIFT, r.GetMaxX() + DL_SHIFT, r.GetMinY(), r.GetMaxY()); if (!TEST_DL_OTHER && shape2.GetBoundingBox().GetCrossesDateLine()) { return; } } else if (shape is Point) { Point p = (Point)shape; shape2 = ctx.MakePoint(normX(p.GetX() + DL_SHIFT), p.GetY()); } else { throw new Exception("" + shape); } assertRelation(null, expectedSR, POLY_SHAPE_DL, shape2); } }