@Test public void testTouchAndParallel() throws Exception { LineString ls = (LineString) wkt.read("LINESTRING(-5 0, 0 1, -5 2, 0 2, 0 3, -5 3, 0 4)"); Geometry clipped = clipper.clip(ls, false); assertTrue(clipped.equalsExact(wkt.read("LINESTRING(0 2, 0 3)"))); showResult("Touch and parallel", ls, 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);  }