@Test public void testMulti() throws Exception { Polygon polygon = (Polygon) wkt.read( "POLYGON ((1.0201465201465183 12.338461538461548, 3.272161172161173 7.7956043956044, 3.893406593406595 11.60073260073261, 6.456043956043961 7.679120879120883, 6.223076923076928 12.64908424908426, 1.0201465201465183 12.338461538461548))"); MultiPolygon clipped = (MultiPolygon) clipper.clip(polygon, true); assertEquals(2, clipped.getNumGeometries()); MultiPolygon mpoly = (MultiPolygon) wkt.read( "MULTIPOLYGON (((1.0201465201465183 12.338461538461548, 3.272161172161173 7.7956043956044, 3.893406593406595 11.60073260073261, 6.456043956043961 7.679120879120883, 6.223076923076928 12.64908424908426, 1.0201465201465183 12.338461538461548)))"); clipped = (MultiPolygon) clipper.clip(mpoly, true); assertEquals(2, clipped.getNumGeometries()); }
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(); Polygon fence = gf.createPolygon(buildBoundsString(gf, csf), null); return g.intersection(fence); } 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(); Polygon fence = gf.createPolygon(buildBoundsString(gf, csf), null); return g.intersection(fence); } 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(); Polygon fence = gf.createPolygon(buildBoundsString(gf, csf), null); return g.intersection(fence); } 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(); Polygon fence = gf.createPolygon(buildBoundsString(gf, csf), null); return g.intersection(fence); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } GeometryClipper(Envelope bounds); Envelope getBounds(); Geometry clipSafe(Geometry g, boolean ensureValid, double scale); 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(); Polygon fence = gf.createPolygon(buildBoundsString(gf, csf), null); return g.intersection(fence); } else { return clipPolygon((Polygon) g); } } else if (g instanceof GeometryCollection) { return clipCollection((GeometryCollection) g, ensureValid); } else { return g; } } GeometryClipper(Envelope bounds); Envelope getBounds(); Geometry clipSafe(Geometry g, boolean ensureValid, double scale); Geometry clip(Geometry g, boolean ensureValid);  }