/** * Creates a smoothed copy of the input Geometry. This is only useful for polygonal and lineal * geometries. Point objects will be returned unchanged. The smoothing algorithm inserts new * vertices which are positioned using Bezier splines. All vertices of the input Geometry will * be present in the output Geometry. * * <p>The {@code fit} parameter controls how tightly the smoothed lines conform to the input * line segments, with a value of 1 being tightest and 0 being loosest. Values outside this * range will be adjusted up or down as required. * * <p>The input Geometry can be a simple type (e.g. LineString, Polygon), a multi-type (e.g. * MultiLineString, MultiPolygon) or a GeometryCollection. The returned object will be of the * same type. * * @param geom the input geometry * @param fit tightness of fit from 0 (loose) to 1 (tight) * @return a new Geometry object of the same class as {@code geom} * @throws IllegalArgumentException if {@code geom} is {@code null} */ public static Geometry smooth(final Geometry geom, double fit) { return smooth(geom, fit, new GeometryFactory()); }
/** * Creates a smoothed copy of the input Geometry. This is only useful for polygonal and lineal * geometries. Point objects will be returned unchanged. The smoothing algorithm inserts new * vertices which are positioned using Bezier splines. All vertices of the input Geometry will * be present in the output Geometry. * * <p>The {@code fit} parameter controls how tightly the smoothed lines conform to the input * line segments, with a value of 1 being tightest and 0 being loosest. Values outside this * range will be adjusted up or down as required. * * <p>The input Geometry can be a simple type (e.g. LineString, Polygon), a multi-type (e.g. * MultiLineString, MultiPolygon) or a GeometryCollection. The returned object will be of the * same type. * * @param geom the input geometry * @param fit tightness of fit from 0 (loose) to 1 (tight) * @param factory the GeometryFactory to use for creating smoothed objects * @return a new Geometry object of the same class as {@code geom} * @throws IllegalArgumentException if either {@code geom} or {@code factory} is {@code null} */ public static Geometry smooth(final Geometry geom, double fit, final GeometryFactory factory) { ensureNonNull("geom", geom); ensureNonNull("factory", factory); // Adjust fit if necessary fit = Math.max(0.0, Math.min(1.0, fit)); return smooth(geom, fit, factory, new GeometrySmoother(factory)); }
private static Geometry smoothGeometryCollection( GeometryFactory factory, GeometrySmoother smoother, Geometry geom, double fit) { final int N = geom.getNumGeometries(); Geometry[] smoothed = new Geometry[N]; for (int i = 0; i < N; i++) { smoothed[i] = smooth(geom.getGeometryN(i), fit, factory, smoother); } return factory.createGeometryCollection(smoothed); }
@Test(expected = IllegalArgumentException.class) public void smoothWithNullGeometry() { JTS.smooth(null, 0); }
@Test(expected = IllegalArgumentException.class) public void smoothWithNullFactory() { LineString line = factory.createLineString(getLineCoords()); JTS.smooth(line, 0, null); }
@Test public void smoothPointReturnsSameObject() { Point point = factory.createPoint(new Coordinate()); Geometry smoothed = JTS.smooth(point, 0); assertTrue(smoothed == point); }
@Test public void smoothMultiPointReturnsSameObject() { Coordinate[] coords = getLineCoords(); MultiPoint mpoint = factory.createMultiPoint(coords); Geometry smoothed = JTS.smooth(mpoint, 0); assertTrue(smoothed == mpoint); }
@Test public void smoothLinearRing() { Coordinate[] coords = getPolyCoords(); LineString line = factory.createLinearRing(coords); Geometry smoothed = JTS.smooth(line, 0); assertTrue(smoothed instanceof LinearRing); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope lineEnv = line.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(lineEnv)); }
@Test public void smoothLineString() { Coordinate[] coords = getLineCoords(); LineString line = factory.createLineString(coords); Geometry smoothed = JTS.smooth(line, 0); assertTrue(smoothed instanceof LineString); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope lineEnv = line.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(lineEnv)); }
@Test public void smoothPolygon() { Coordinate[] coords = getPolyCoords(); Polygon poly = factory.createPolygon(factory.createLinearRing(coords), null); Geometry smoothed = JTS.smooth(poly, 0); assertTrue(smoothed instanceof Polygon); CoordList list = new CoordList(smoothed.getCoordinates()); assertTrue(list.containsAll(coords)); Envelope polyEnv = poly.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(polyEnv)); }
@Test public void smoothMultiLineString() { LineString[] lines = new LineString[3]; lines[0] = factory.createLineString(getLineCoords(0)); lines[1] = factory.createLineString(getLineCoords(10)); lines[2] = factory.createLineString(getLineCoords(20)); MultiLineString mls = factory.createMultiLineString(lines); Geometry smoothed = JTS.smooth(mls, 0); assertTrue(smoothed instanceof MultiLineString); assertEquals(3, smoothed.getNumGeometries()); Envelope mlsEnv = mls.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(mlsEnv)); }
@Test public void smoothMultiPolygon() { Polygon[] polys = new Polygon[3]; polys[0] = factory.createPolygon(factory.createLinearRing(getPolyCoords(0)), null); polys[1] = factory.createPolygon(factory.createLinearRing(getPolyCoords(10)), null); polys[2] = factory.createPolygon(factory.createLinearRing(getPolyCoords(20)), null); MultiPolygon mp = factory.createMultiPolygon(polys); Geometry smoothed = JTS.smooth(mp, 0); assertTrue(smoothed instanceof MultiPolygon); assertEquals(3, smoothed.getNumGeometries()); Envelope mpEnv = mp.getEnvelopeInternal(); Envelope smoothEnv = smoothed.getEnvelopeInternal(); assertTrue(smoothEnv.covers(mpEnv)); }
Geometry smoothed = JTS.smooth(gc, 0); assertTrue(smoothed instanceof GeometryCollection); assertEquals(3, smoothed.getNumGeometries());