001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.render.internal.gridcoverage.basic;
010:
011: import java.awt.Graphics2D;
012: import java.io.IOException;
013:
014: import net.refractions.udig.project.ILayer;
015: import net.refractions.udig.project.internal.render.impl.RendererImpl;
016: import net.refractions.udig.project.render.RenderException;
017: import net.refractions.udig.render.gridcoverage.basic.GridCoverageRendererUtils;
018: import net.refractions.udig.render.gridcoverage.basic.State;
019: import net.refractions.udig.render.gridcoverage.basic.internal.Messages;
020:
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.geotools.renderer.lite.GridCoverageRenderer;
023: import org.opengis.coverage.grid.GridCoverage;
024: import org.opengis.referencing.crs.CoordinateReferenceSystem;
025:
026: /**
027: * A special renderer optimized for grid coverages. For the moment the
028: * symbolizer parameter considered is opacity
029: *
030: * @author Jesse Eichar
031: * @author Andrea Aime
032: * @version $Revision: 1.9 $
033: */
034: public class BasicGridCoverageRenderer extends RendererImpl {
035:
036: private GridCoverageRenderer renderer;
037:
038: private GridCoverage oldCoverage;
039:
040: private CoordinateReferenceSystem oldCrs;
041:
042: public synchronized void render(Graphics2D destination,
043: IProgressMonitor monitor) throws RenderException {
044: State state = null;
045: try {
046: state = prepareRender(monitor);
047: } catch (IOException e1) {
048: throw new RenderException(e1);
049: }
050: doRender(renderer, destination, state);
051: }
052:
053: /**
054: * Renders a GridCoverage
055: * @param renderer
056: * @param graphics
057: * @throws RenderException
058: */
059: public static void doRender(GridCoverageRenderer renderer,
060: Graphics2D graphics, State state) throws RenderException {
061: double scale = state.context.getViewportModel()
062: .getScaleDenominator();
063: if (scale < state.minScale || scale > state.maxScale)
064: return;
065:
066: state.context.setStatus(ILayer.WAIT);
067: state.context
068: .setStatusMessage(Messages.BasicGridCoverageRenderer_statusMessage);
069:
070: try {
071: GridCoverageRendererUtils.paintGraphic(renderer, graphics,
072: state);
073: } catch (Throwable t) {
074: throw new RenderException(
075: Messages.BasicGridCoverageRenderer_errorPainting, t);
076: }
077: if (state.context.getStatus() == ILayer.WAIT) {
078: //status hasn't changed... everything looks good
079: state.context.setStatus(ILayer.DONE);
080: state.context.setStatusMessage(null);
081: }
082:
083: }
084:
085: private State prepareRender(IProgressMonitor monitor)
086: throws IOException {
087: GridCoverage coverage = getContext().getGeoResource().resolve(
088: GridCoverage.class, monitor);
089: CoordinateReferenceSystem targetCRS = getViewportCRS();
090: if (renderer == null || !oldCoverage.equals(coverage)
091: || !oldCrs.equals(targetCRS)) {
092: oldCoverage = coverage;
093: oldCrs = targetCRS;
094: renderer = GridCoverageRendererUtils.createRenderer(
095: coverage, targetCRS);
096: }
097:
098: return GridCoverageRendererUtils.getRenderState(getContext());
099: }
100:
101: public void stopRendering() {
102: setState(STATE_EDEFAULT);
103: }
104:
105: public void dispose() {
106: // TODO
107: }
108:
109: public void render(IProgressMonitor monitor) throws RenderException {
110: render(getContext().getImage().createGraphics(), monitor);
111: }
112:
113: private CoordinateReferenceSystem getViewportCRS() {
114: return getContext().getViewportModel().getCRS();
115: }
116:
117: }
|