public final URI render ( final MapfishMapContext mapContext , final ScalebarAttributeValues scalebarParams , final File tempFolder , final Template template ) throws IOException , ParserConfigurationException { final double dpi = mapContext . getDPI ( ) ; // get the map bounds final Rectangle paintArea = new Rectangle ( mapContext . getMapSize ( ) ) ; MapBounds bounds = mapContext . getBounds ( ) ; final DistanceUnit mapUnit = getUnit ( bounds ) ; final Scale scale = bounds . getScale ( paintArea , PDF_DPI ) ; final double scaleDenominator = scale . getDenominator ( scalebarParams . geodetic , bounds . getProjection ( ) , dpi , bounds . getCenter ( ) ) ; DistanceUnit scaleUnit = scalebarParams . getUnit ( ) ; if ( scaleUnit == null ) { scaleUnit = mapUnit ; } // adjust scalebar width and height to the DPI value final double maxLengthInPixel = ( scalebarParams . getOrientation ( ) . isHorizontal ( ) ) ? scalebarParams . getSize ( ) . width : scalebarParams . getSize ( ) . height ; final double maxIntervalLengthInWorldUnits = DistanceUnit . PX . convertTo ( maxLengthInPixel , scaleUnit ) * scaleDenominator / scalebarParams . intervals ; final double niceIntervalLengthInWorldUnits = getNearestNiceValue ( maxIntervalLengthInWorldUnits , scaleUnit , scalebarParams . lockUnits ) ; final ScaleBarRenderSettings settings = new ScaleBarRenderSettings ( ) ; settings . setParams ( scalebarParams ) ; settings . setMaxSize ( scalebarParams . getSize ( ) ) ; settings . setPadding ( getPadding ( settings ) ) ; // start the rendering File path = null ; if ( template . getConfiguration ( ) . renderAsSvg ( scalebarParams . renderAsSvg ) ) { // render scalebar as SVG final SVGGraphics2D graphics2D = CreateMapProcessor . createSvgGraphics ( scalebarParams . getSize ( ) ) ; try { tryLayout ( graphics2D , scaleUnit , scaleDenominator , niceIntervalLengthInWorldUnits , settings , 0 ) ; path = File . createTempFile ( "scalebar-graphic-" , ".svg" , tempFolder ) ; CreateMapProcessor . saveSvgFile ( graphics2D , path ) ; } finally { graphics2D . dispose ( ) ; } } else { // render scalebar as raster graphic double dpiRatio = mapContext . getDPI ( ) / PDF_DPI ; final BufferedImage bufferedImage = new BufferedImage ( ( int ) Math . round ( scalebarParams . getSize ( ) . width * dpiRatio ) , ( int ) Math . round ( scalebarParams . getSize ( ) . height * dpiRatio ) , TYPE_4BYTE_ABGR ) ; final Graphics2D graphics2D = bufferedImage . createGraphics ( ) ; try { AffineTransform saveAF = new AffineTransform ( graphics2D . getTransform ( ) ) ; graphics2D . scale ( dpiRatio , dpiRatio ) ; tryLayout ( graphics2D , scaleUnit , scaleDenominator , niceIntervalLengthInWorldUnits , settings , 0 ) ; graphics2D . setTransform ( saveAF ) ; path = File . createTempFile ( "scalebar-graphic-" , ".png" , tempFolder ) ; ImageUtils . writeImage ( bufferedImage , "png" , path ) ; } finally { graphics2D . dispose ( ) ; } } return path . toURI ( ) ; }
Render the scalebar .