protected override IShape RandomShape() { if (random.Next(3) == 0) { NtsSpatialContext ctx = (NtsSpatialContext)base.ctx; return(ctx.MakeShape(RandomGeometry(random.Next(3, 20)), false, false)); } else { return(base.RandomShape()); } }
/// <summary> /// Creates the NtsGeometry, potentially validating, repairing, and preparing. /// </summary> protected virtual NtsGeometry MakeShapeFromGeometry(IGeometry geometry) { bool dateline180Check = DatelineRule != Nts.DatelineRule.None; NtsGeometry ntsGeom; try { ntsGeom = m_ctx.MakeShape(geometry, dateline180Check, m_ctx.IsAllowMultiOverlap); if (IsAutoValidate) { ntsGeom.Validate(); } } catch (Exception e) { //repair: if (m_validationRule == Nts.ValidationRule.RepairConvexHull) { ntsGeom = m_ctx.MakeShape(geometry.ConvexHull(), dateline180Check, m_ctx.IsAllowMultiOverlap); } else if (m_validationRule == Nts.ValidationRule.RepairBuffer0) { ntsGeom = m_ctx.MakeShape(geometry.Buffer(0), dateline180Check, m_ctx.IsAllowMultiOverlap); } else { //TODO there are other smarter things we could do like repairing inner holes and subtracting // from outer repaired shell; but we needn't try too hard. throw e; } } if (IsAutoIndex) { ntsGeom.Index(); } return(ntsGeom); }
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 IShape ReadNtsGeom(BinaryReader dataInput) { NtsSpatialContext ctx = (NtsSpatialContext)base.ctx; #pragma warning disable 612, 618 WKBReader reader = new WKBReader(ctx.GeometryFactory); #pragma warning restore 612, 618 try { Stream inStream = new InputStreamAnonymousHelper(dataInput); IGeometry geom = reader.Read(inStream); //false: don't check for dateline-180 cross or multi-polygon overlaps; this won't happen // once it gets written, and we're reading it now return(ctx.MakeShape(geom, false, false)); } catch (GeoAPI.IO.ParseException ex) { throw new InvalidShapeException("error reading WKT", ex); } }
public virtual void TestParsePolygon() { IShape polygonNoHoles = new PolygonBuilder(ctx) .Point(100, 0) .Point(101, 0) .Point(101, 1) .Point(100, 2) .Point(100, 0) .Build(); string polygonNoHolesSTR = "POLYGON ((100 0, 101 0, 101 1, 100 2, 100 0))"; AssertParses(polygonNoHolesSTR, polygonNoHoles); AssertParses("POLYGON((100 0,101 0,101 1,100 2,100 0))", polygonNoHoles); AssertParses("GEOMETRYCOLLECTION ( " + polygonNoHolesSTR + ")", ctx.MakeCollection(new List <IShape>(new IShape[] { polygonNoHoles }))); IShape polygonWithHoles = new PolygonBuilder(ctx) .Point(100, 0) .Point(101, 0) .Point(101, 1) .Point(100, 1) .Point(100, 0) .NewHole() .Point(100.2, 0.2) .Point(100.8, 0.2) .Point(100.8, 0.8) .Point(100.2, 0.8) .Point(100.2, 0.2) .EndHole() .Build(); AssertParses("POLYGON ((100 0, 101 0, 101 1, 100 1, 100 0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))", polygonWithHoles); GeometryFactory gf = ctx.GeometryFactory; AssertParses("POLYGON EMPTY", ctx.MakeShape( gf.CreatePolygon(gf.CreateLinearRing(new Coordinate[] { }), null) )); }
public virtual void TestPoly() { NtsSpatialContext ctx = (NtsSpatialContext)base.ctx; ctx.MakeShape(RandomGeometry(random.Next(3, 20)), false, false); }