SimplifyGeometryFunction implements GeometryOperator { @Override public Geometry apply(Geometry geometry, Context context) { if (geometry == null || geometry.isEmpty()) { return geometry; } LOGGER.trace( "simplifying geometry: {}\n {} coordinates, isValid? {}", geometry, geometry.getCoordinates().length, geometry.isValid()); Double distanceTolerance = context.getDistanceTolerance(); Geometry simplifiedGeometry; if (distanceTolerance != null) { simplifiedGeometry = TopologyPreservingSimplifier.simplify(geometry, distanceTolerance); } else { simplifiedGeometry = new TopologyPreservingSimplifier(geometry).getResultGeometry(); } LOGGER.trace( "simplified geometry: {}\n {} coordinates, isValid? {}", simplifiedGeometry, simplifiedGeometry.getCoordinates().length, simplifiedGeometry.isValid()); return simplifiedGeometry.isValid() ? simplifiedGeometry : geometry; }  @Override Geometry apply(Geometry geometry, Context context); @Override String toString();  }
@Test public void testApply() throws ParseException { Geometry originalGeometry = wktReader.read(REALLY_LONG_LINESTRING); Geometry transformedGeometry = simplifyGeometryFunction.apply(originalGeometry, new GeometryOperator.Context()); assertThat( transformedGeometry.getCoordinates().length, Matchers.lessThan(originalGeometry.getCoordinates().length)); }
@Test public void testApplyToLineString() throws ParseException { String lineStringWkt = "LINESTRING ( 0 0, 0.01 0.01, 0.05 0.05 )"; String expectedLineStringWkt = "LINESTRING ( 0 0, 0.05 0.05 )"; GeometryOperator.Context context = new GeometryOperator.Context(); context.setDistanceTolerance(0.01); assertThat( simplifyGeometryFunction.apply(wktReader.read(lineStringWkt), context).norm(), is(wktReader.read(expectedLineStringWkt).norm())); }
@Test public void testApplyNullArg() { GeometryOperator.Context context = new GeometryOperator.Context(); Geometry result = simplifyGeometryFunction.apply(null, context); assertThat(result, nullValue()); }
@Test public void testEmptryLineString() { GeometryOperator.Context context = new GeometryOperator.Context(); final Geometry emptyGeo = GEOMETRY_FACTORY.createLineString((Coordinate[]) null); Geometry result = simplifyGeometryFunction.apply(emptyGeo, context); assertThat(result, is(emptyGeo)); }
@Test public void testEmptyPolygon() { GeometryOperator.Context context = new GeometryOperator.Context(); final Geometry emptyGeo = GEOMETRY_FACTORY.createMultiPolygon(null); Geometry result = simplifyGeometryFunction.apply(emptyGeo, context); assertThat(result, is(emptyGeo)); }