@Test public void testDonutCrossingInvalid() throws Exception { Geometry g = wkt.read("POLYGON((6 2, 14 2, 14 8, 6 8, 6 2), (8 4, 12 4, 12 6, 8 6, 8 4))"); Geometry clipped = clipper.clip(g, false); assertTrue(clipped.equalsExact(wkt.read("POLYGON ((10 2, 10 8, 6 8, 6 2, 10 2), (10 4, 10 6, 8 6, 8 4, 10 4))"))); showResult("Donut crossing, invalid geom", g, clipped); }
public Geometry clip(Geometry g, boolean ensureValid) { if (g == null) { return null; } Envelope geomEnvelope = g.getEnvelopeInternal(); if (geomEnvelope.isNull()) { return null; } if (bounds.contains(geomEnvelope)) { return g; } else if (!bounds.intersects(geomEnvelope)) { return null; } if (g instanceof LineString) { return clipLineString((LineString) g); } else if (g instanceof Polygon) { if(ensureValid) { GeometryFactory gf = g.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); return g.intersection(gf.createPolygon(buildBoundsString(gf, csf), null)); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } }
GeometryClipper { public Geometry clip(Geometry g, boolean ensureValid) { if (g == null) { return null; } Envelope geomEnvelope = g.getEnvelopeInternal(); if (geomEnvelope.isNull()) { return null; } if (bounds.contains(geomEnvelope)) { return g; } else if (!bounds.intersects(geomEnvelope)) { return null; } if (g instanceof LineString) { return clipLineString((LineString) g); } else if (g instanceof Polygon) { if(ensureValid) { GeometryFactory gf = g.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); return g.intersection(gf.createPolygon(buildBoundsString(gf, csf), null)); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } }
GeometryClipper { public Geometry clip(Geometry g, boolean ensureValid) { if (g == null) { return null; } Envelope geomEnvelope = g.getEnvelopeInternal(); if (geomEnvelope.isNull()) { return null; } if (bounds.contains(geomEnvelope)) { return g; } else if (!bounds.intersects(geomEnvelope)) { return null; } if (g instanceof LineString) { return clipLineString((LineString) g); } else if (g instanceof Polygon) { if(ensureValid) { GeometryFactory gf = g.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); return g.intersection(gf.createPolygon(buildBoundsString(gf, csf), null)); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } GeometryClipper(Envelope bounds); }
GeometryClipper { public Geometry clip(Geometry g, boolean ensureValid) { if (g == null) { return null; } Envelope geomEnvelope = g.getEnvelopeInternal(); if (geomEnvelope.isNull()) { return null; } if (bounds.contains(geomEnvelope)) { return g; } else if (!bounds.intersects(geomEnvelope)) { return null; } if (g instanceof LineString) { return clipLineString((LineString) g); } else if (g instanceof Polygon) { if(ensureValid) { GeometryFactory gf = g.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); return g.intersection(gf.createPolygon(buildBoundsString(gf, csf), null)); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } GeometryClipper(Envelope bounds); Envelope getBounds(); Geometry clip(Geometry g, boolean ensureValid); }
GeometryClipper { public Geometry clip(Geometry g, boolean ensureValid) { if (g == null) { return null; } Envelope geomEnvelope = g.getEnvelopeInternal(); if (geomEnvelope.isNull()) { return null; } if (bounds.contains(geomEnvelope)) { return g; } else if (!bounds.intersects(geomEnvelope)) { return null; } if (g instanceof LineString) { return clipLineString((LineString) g); } else if (g instanceof Polygon) { if(ensureValid) { GeometryFactory gf = g.getFactory(); CoordinateSequenceFactory csf = gf.getCoordinateSequenceFactory(); return g.intersection(gf.createPolygon(buildBoundsString(gf, csf), null)); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } GeometryClipper(Envelope bounds); Envelope getBounds(); Geometry clip(Geometry g, boolean ensureValid);  }