SurfaceLinearizer { @SuppressWarnings("unchecked") public <T extends Surface> T linearize( T surface, LinearizationCriterion crit ) { T linearizedSurface = null; switch ( surface.getSurfaceType() ) { case Polygon: { Polygon polygon = (Polygon) surface; Ring exteriorRing = polygon.getExteriorRing(); Ring linearizedExteriorRing = (Ring) curveLinearizer.linearize( exteriorRing, crit ); List<Ring> interiorRings = polygon.getInteriorRings(); List<Ring> linearizedInteriorRings = new ArrayList<Ring>( interiorRings.size() ); for ( Ring interiorRing : interiorRings ) { linearizedInteriorRings.add( (Ring) curveLinearizer.linearize( interiorRing, crit ) ); } linearizedSurface = (T) geomFac.createPolygon( polygon.getId(), polygon.getCoordinateSystem(), linearizedExteriorRing, linearizedInteriorRings ); break; } case PolyhedralSurface: { List<? extends SurfacePatch> patches = surface.getPatches(); List<PolygonPatch> linearizedPatches = new ArrayList<PolygonPatch>( patches.size() ); for ( SurfacePatch patch : surface.getPatches() ) { if ( !( patch instanceof PolygonPatch ) ) { String msg = "Linearization of non planar surface patches is not implemented"; throw new IllegalArgumentException( msg ); } linearizedPatches.add( linearize( (PolygonPatch) patch, crit ) ); } linearizedSurface = (T) geomFac.createPolyhedralSurface( surface.getId(), surface.getCoordinateSystem(), linearizedPatches ); break; } case Surface: { List<? extends SurfacePatch> patches = surface.getPatches(); List<SurfacePatch> linearizedPatches = new ArrayList<SurfacePatch>( patches.size() ); for ( SurfacePatch patch : surface.getPatches() ) { if ( !( patch instanceof PolygonPatch ) ) { String msg = "Linearization of non planar surface patches is not implemented"; throw new IllegalArgumentException( msg ); } linearizedPatches.add( linearize( (PolygonPatch) patch, crit ) ); } linearizedSurface = (T) geomFac.createSurface( surface.getId(), linearizedPatches, surface.getCoordinateSystem() ); break; } default: { LOG.warn( "The surface type " + surface.getSurfaceType() + " currently cannot be linearized. It's being returned as it is." ); linearizedSurface = surface; } } return linearizedSurface; } SurfaceLinearizer( GeometryFactory geomFac ); @SuppressWarnings("unchecked") T linearize( T surface, LinearizationCriterion crit ); PolygonPatch linearize( PolygonPatch patch, LinearizationCriterion crit );  }
@Test public void linearizePolygon() { Polygon polygon = new DefaultPolygon( null, null, null, createExteriorRing(), createInteriorRings() ); Polygon res = linearizer.linearize( polygon, new MaxErrorCriterion( MAX_ERROR, MAX_POINTS ) ); LOG.debug( "exterior ring:" ); for ( Curve curve : res.getExteriorRing().getMembers() ) { for ( Point p : curve.getControlPoints() ) { LOG.debug( p.get0() + ", " + p.get1() ); } } for ( Points pts : res.getInteriorRingsCoordinates() ) { LOG.debug( "interior ring:" ); for ( int i = 0; i < pts.size(); i++ ) LOG.debug( pts.get( i ).get0() + ", " + pts.get( i ).get1() ); } }