public synchronized void render( Graphics2D graphics, IProgressMonitor monitor )
throws RenderException {
try {
// get the current context
final IRenderContext currentContext = getContext();
final IGeoResource geoResource = currentContext.getGeoResource();
if (geoResource.canResolve(RendererImpl.class)) {
// if the service wants to use its own renderer, let it be
RendererImpl rendererImpl = geoResource.resolve(RendererImpl.class, monitor);
rendererImpl.setContext(currentContext);
rendererImpl.render(graphics, monitor);
rendererImpl.dispose();
return;
}
//check that actually we have something to draw
currentContext.setStatus(ILayer.WAIT);
currentContext.setStatusMessage(Messages.BasicGridCoverageRenderer_rendering_status);
//get the envelope and the screen extent
ReferencedEnvelope envelope = getRenderBounds();
if( envelope == null || envelope.isNull()){
envelope = context.getImageBounds();
}
Point upperLeft = currentContext.worldToPixel( new Coordinate( envelope.getMinX(), envelope.getMinY()) );
Point bottomRight = currentContext.worldToPixel( new Coordinate( envelope.getMaxX(), envelope.getMaxY()) );
Rectangle screenSize = new Rectangle( upperLeft );
screenSize.add( bottomRight );
IMapDisplay mapDisplay = currentContext.getMapDisplay();
AbstractGridCoverage2DReader reader = (AbstractGridCoverage2DReader) geoResource.resolve( AbstractGridCoverage2DReader.class, monitor);
if( reader == null ){
return; // unable to connect!
}
CoordinateReferenceSystem destinationCRS = currentContext.getCRS();
ReferencedEnvelope bounds = (ReferencedEnvelope) currentContext.getImageBounds();
bounds=bounds.transform(destinationCRS, true);
ParameterValueGroup group = geoResource.resolve( ParameterValueGroup.class, monitor);
if(group==null){
group=reader.getFormat().getReadParameters();
}
else{
// temporary fix for image-io (JG: what is the nature of this fix?)
try{
ParameterValue<?> jaiImageReaderParam = group.parameter(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().toString());
if(jaiImageReaderParam!=null){
jaiImageReaderParam.setValue(false);
}
}catch (ParameterNotFoundException e) {
// do nothing
}
}
ParameterValue<?> readGridGeometry2DParam = group.parameter(AbstractGridFormat.READ_GRIDGEOMETRY2D.getName().toString());
// GridEnvelope range = new GridEnvelope2D(0, 0, mapDisplay.getWidth(), mapDisplay.getHeight());
// MathTransform displayToLayer = currentContext.worldToScreenMathTransform().inverse();
// ReferencingFactoryFinder.getMathTransformFactory(null).createConcatenatedTransform(displayToLayer,
// currentContext.getLayer().mapToLayerTransform());
// GridGeometry2D geom = new GridGeometry2D(range, displayToLayer, destinationCRS);
// readGridGeometry2DParam.setValue(geom);
GridEnvelope2D gridEnvelope = new GridEnvelope2D(0, 0, mapDisplay.getWidth(), mapDisplay.getHeight());
org.opengis.geometry.Envelope env;
double west= bounds.getMinX();
double east= bounds.getMaxX();
double south= bounds.getMinY();
double north= bounds.getMaxY();
if (destinationCRS != null) {
env = new ReferencedEnvelope(west, east, south, north, destinationCRS);
} else {
DirectPosition2D minDp = new DirectPosition2D(west, south);
DirectPosition2D maxDp = new DirectPosition2D(east, north);
env = new Envelope2D(minDp, maxDp);
}
readGridGeometry2DParam.setValue(new GridGeometry2D(gridEnvelope, env));
currentContext.setStatus(ILayer.WORKING);
setState( STARTING );
ParameterValue[] parameterValues = group.values().toArray(new ParameterValue[0]);
GridCoverage2D coverage = (GridCoverage2D) reader.read(parameterValues);
if(coverage!=null){
//setting rendering hints
//
RenderingHints hints = new RenderingHints(new HashMap<RenderingHints.Key,Object>());
hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));
hints.add(new RenderingHints(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE));
hints.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED));
hints.add(new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED));
hints.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
hints.add(new RenderingHints(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE));
hints.add(new RenderingHints(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
hints.add(new RenderingHints(JAI.KEY_INTERPOLATION,new InterpolationNearest()));
graphics.addRenderingHints(hints);
// JG: Store title cache on the layer blackboard so it can last between runs.
// Performance question: may do better to have a single larger tile cache on the map blackboard?
//
final TileCache tempCache=currentContext.getTileCache();
hints.add(new RenderingHints(JAI.KEY_TILE_CACHE,tempCache));
//hints.add( new Hints( Hints.RESAMPLE_TOLERANCE, 0.000000000001 ));
if( CRS.getHorizontalCRS(destinationCRS) == null ){
destinationCRS = coverage.getCoordinateReferenceSystem2D();
}
//
AffineTransform worldToScreen = null; // we are leting the GridCoverageRenderer sort that out
//draw
try {
Style style = grabStyle();
Rule rule = SLDs.getRasterSymbolizerRule(style);
final double currentScale = currentContext.getViewportModel().getScaleDenominator();
double minScale = rule.getMinScaleDenominator();
double maxScale = rule.getMaxScaleDenominator();
if (minScale <= currentScale && currentScale <= maxScale ) {
final GridCoverageRenderer paint = new GridCoverageRenderer( destinationCRS, envelope, screenSize,worldToScreen,hints );
final RasterSymbolizer rasterSymbolizer = SLD.rasterSymbolizer(style);