use of org.geotoolkit.display.canvas.control.CanvasMonitor in project geotoolkit by Geomatys.
the class RenderingRoutines method optimizeBBox.
public static Envelope optimizeBBox(RenderingContext2D renderingContext, FeatureSet featureSet, double symbolsMargin) throws PortrayalException {
Envelope bbox = renderingContext.getCanvasObjectiveBounds2D();
final CoordinateReferenceSystem bboxCRS = bbox.getCoordinateReferenceSystem();
final CanvasMonitor monitor = renderingContext.getMonitor();
final CoordinateReferenceSystem layerCRS;
try {
layerCRS = FeatureExt.getCRS(featureSet.getType());
} catch (DataStoreException ex) {
throw new PortrayalException(ex.getMessage(), ex);
}
// expand the search area by the maximum symbol size
if (symbolsMargin > 0) {
final GeneralEnvelope env = new GeneralEnvelope(bbox);
env.setRange(0, env.getMinimum(0) - symbolsMargin, env.getMaximum(0) + symbolsMargin);
env.setRange(1, env.getMinimum(1) - symbolsMargin, env.getMaximum(1) + symbolsMargin);
bbox = env;
}
// or if the crs is defined only on the feature geometry
if (layerCRS != null && !Utilities.equalsIgnoreMetadata(layerCRS, bboxCRS)) {
// BBox and layer bounds have different CRS. reproject bbox bounds
Envelope env;
try {
env = Envelopes.transform(bbox, layerCRS);
if (GeneralEnvelope.castOrCopy(env).isEmpty()) {
// possible NaN values or out of crs validity area
GeneralEnvelope benv = GeneralEnvelope.castOrCopy(bbox);
benv.normalize();
env = Envelopes.transform(benv, layerCRS);
}
} catch (TransformException ex) {
// TODO is fixed in geotidy, the result envelope will have infinte values where needed
// TODO should do something about this, since canvas bounds may be over the crs bounds
monitor.exceptionOccured(ex, Level.WARNING);
env = new Envelope2D();
} catch (IllegalArgumentException ex) {
// looks like the coordinate of the bbox are outside of the crs valide area.
// some crs raise this error, other not.
// if so we should reduce our bbox to the valide extent of the crs.
monitor.exceptionOccured(ex, Level.WARNING);
final GeographicBoundingBox gbox = CRS.getGeographicBoundingBox(layerCRS);
if (gbox == null) {
env = new GeneralEnvelope(layerCRS);
} else {
env = new GeneralEnvelope(gbox);
}
} catch (Exception ex) {
// we should not catch this but we must not block the canvas
monitor.exceptionOccured(ex, Level.WARNING);
return null;
}
// TODO looks like the envelope after transform operation doesnt have always exactly the same CRS.
// fix CRS classes method and remove the two next lines.
env = new GeneralEnvelope(env);
((GeneralEnvelope) env).setCoordinateReferenceSystem(layerCRS);
bbox = env;
}
return bbox;
}
use of org.geotoolkit.display.canvas.control.CanvasMonitor in project geotoolkit by Geomatys.
the class DefaultPortrayalService method prepareCanvas.
public static void prepareCanvas(final J2DCanvas canvas, final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
final ContextContainer2D renderer = new ContextContainer2D(canvas);
canvas.setContainer(renderer);
final Color bgColor = canvasDef.getBackground();
if (bgColor != null) {
canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
}
final CanvasMonitor monitor = canvasDef.getMonitor();
if (monitor != null) {
canvas.setMonitor(monitor);
}
final Hints hints = sceneDef.getHints();
if (hints != null) {
for (Entry<?, ?> entry : hints.entrySet()) {
canvas.setRenderingHint((Key) entry.getKey(), entry.getValue());
}
}
final MapLayers context = sceneDef.getContext();
renderer.setContext(context);
GridGeometry gridGeometry = canvasDef.getGridGeometry();
if (gridGeometry != null) {
try {
canvas.setGridGeometry(gridGeometry);
} catch (FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
} else {
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
try {
canvas.setObjectiveCRS(crs);
} catch (TransformException | FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
// we specifically say to not repect X/Y proportions
canvas.setAxisProportions(!canvasDef.isStretchImage());
// setVisibleArea -> setAxisRange -> setRange.
if (contextEnv != null) {
try {
canvas.setGridGeometry(canvasDef.getOrCreateGridGeometry());
} catch (Exception e) {
// Rollback to previous behavior
try {
canvas.setVisibleArea(contextEnv);
if (canvasDef.getAzimuth() != 0) {
canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
}
} catch (NoninvertibleTransformException | TransformException ex) {
ex.addSuppressed(e);
throw new PortrayalException(ex);
}
}
}
}
// paints all extensions
final List<PortrayalExtension> extensions = sceneDef.extensions();
if (extensions != null) {
for (final PortrayalExtension extension : extensions) {
if (extension != null)
extension.completeCanvas(canvas);
}
}
}
use of org.geotoolkit.display.canvas.control.CanvasMonitor in project geotoolkit by Geomatys.
the class RasterPresentation method paint.
@Override
public boolean paint(RenderingContext2D renderingContext) throws PortrayalException {
final CanvasMonitor monitor = renderingContext.getMonitor();
final Graphics2D g2d = renderingContext.getGraphics();
boolean dataRendered = false;
final GridCoverage dataImage = coverage;
g2d.setComposite(composite);
/*
* If we haven't got any reprojection we delegate affine transformation to java2D
* we must switch to objectiveCRS for grid coverage
*/
renderingContext.switchToObjectiveCRS();
MathTransform trs2D = renderingTransform(dataImage.getGridGeometry());
if (monitor.stopRequested())
return dataRendered;
if (renderingContext.wraps == null) {
// single rendering
dataRendered |= renderCoverage(renderingContext, dataImage, trs2D);
} else {
// check if the geometry overlaps the meridian
int nbIncRep = renderingContext.wraps.wrapIncNb;
int nbDecRep = renderingContext.wraps.wrapDecNb;
final Geometry objBounds = JTS.toGeometry(dataImage.getGridGeometry().getEnvelope());
// POLYGON(-179,10, 181,10, 181,-10, 179,-10)
if (objBounds.intersects(renderingContext.wraps.wrapIncLine)) {
// duplicate geometry on the other warp line
nbDecRep++;
} else // POLYGON(-179,10, -181,10, -181,-10, -179,-10)
if (objBounds.intersects(renderingContext.wraps.wrapDecLine)) {
// duplicate geometry on the other warp line
nbIncRep++;
}
dataRendered |= renderCoverage(renderingContext, dataImage, trs2D);
// -- repetition of increasing and decreasing sides.
for (int i = 0; i < nbDecRep; i++) {
g2d.setTransform(renderingContext.wraps.wrapDecObjToDisp[i]);
dataRendered |= renderCoverage(renderingContext, dataImage, trs2D);
}
for (int i = 0; i < nbIncRep; i++) {
g2d.setTransform(renderingContext.wraps.wrapIncObjToDisp[i]);
dataRendered |= renderCoverage(renderingContext, dataImage, trs2D);
}
}
renderingContext.switchToDisplayCRS();
return dataRendered;
}
use of org.geotoolkit.display.canvas.control.CanvasMonitor in project geotoolkit by Geomatys.
the class GO2Utilities method portray.
/**
* @return true if some datas has been rendered
*/
public static boolean portray(final RenderingContext2D renderingContext, GridCoverage coverage) throws PortrayalException {
final CanvasMonitor monitor = renderingContext.getMonitor();
final Graphics2D g2d = renderingContext.getGraphics();
final CoordinateReferenceSystem objectiveCrs = renderingContext.getObjectiveCRS2D();
final CoordinateReferenceSystem coverageCRS = coverage.getCoordinateReferenceSystem();
boolean sameCRS = true;
try {
final CoordinateReferenceSystem candidate2D = CRS.getHorizontalComponent(coverageCRS);
if (candidate2D == null)
throw new PortrayalException("Cannot find horizontal component of input data CRS.");
if (!Utilities.equalsIgnoreMetadata(candidate2D, objectiveCrs)) {
sameCRS = false;
// We do not force geophysics only because we use a neighbor interpolation.
coverage = GO2Utilities.resample(coverage, objectiveCrs, InterpolationCase.NEIGHBOR);
}
} catch (CannotReprojectException ex) {
monitor.exceptionOccured(ex, Level.WARNING);
return false;
} catch (Exception ex) {
// several kind of errors can happen here, we catch anything to avoid blocking the map component.
monitor.exceptionOccured(new IllegalStateException("Coverage is not in the requested CRS, found : " + "\n" + coverageCRS + " was expecting : \n" + renderingContext.getObjectiveCRS() + "\nOriginal Cause:" + ex.getMessage(), ex), Level.WARNING);
return false;
}
if (coverage == null) {
monitor.exceptionOccured(new NullArgumentException("GO2Utilities : Reprojected coverage is null."), Level.WARNING);
return false;
}
// we must switch to objectiveCRS for grid coverage
renderingContext.switchToObjectiveCRS();
coverage = coverage.forConvertedValues(true);
RenderedImage img = coverage.render(null);
if (!sameCRS) {
// otherwise we will have black borders after reprojection
if (!img.getColorModel().hasAlpha()) {
// ensure we have a bufferedImage for floodfill operation
final BufferedImage buffer;
if (img instanceof BufferedImage) {
buffer = (BufferedImage) img;
} else {
buffer = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
buffer.createGraphics().drawRenderedImage(RenderingWorkaround.wrap(img), new AffineTransform());
}
// remove black borders+
FloodFill.fill(buffer, new Color[] { Color.BLACK }, new Color(0f, 0f, 0f, 0f), new java.awt.Point(0, 0), new java.awt.Point(buffer.getWidth() - 1, 0), new java.awt.Point(buffer.getWidth() - 1, buffer.getHeight() - 1), new java.awt.Point(0, buffer.getHeight() - 1));
img = buffer;
}
}
// If there's no reprojection above, and input coverage has more than 2 dimensions, we must search for horizontal axes.
final int xAxis = AxisDirections.indexOfColinear(objectiveCrs.getCoordinateSystem(), coverage.getCoordinateReferenceSystem().getCoordinateSystem());
final MathTransform gridToCRS = coverage.getGridGeometry().reduce(xAxis, xAxis + 1).getGridToCRS(PixelInCell.CELL_CORNER);
if (gridToCRS instanceof AffineTransform) {
g2d.setComposite(GO2Utilities.ALPHA_COMPOSITE_1F);
g2d.drawRenderedImage(RenderingWorkaround.wrap(img), (AffineTransform) gridToCRS);
return true;
} else if (gridToCRS instanceof LinearTransform) {
final LinearTransform lt = (LinearTransform) gridToCRS;
final int col = lt.getMatrix().getNumCol();
final int row = lt.getMatrix().getNumRow();
// TODO using only the first parameters of the linear transform
throw new PortrayalException("Could not render image, GridToCRS is a not an AffineTransform, found a " + gridToCRS.getClass());
} else {
throw new PortrayalException("Could not render image, GridToCRS is a not an AffineTransform, found a " + gridToCRS.getClass());
}
}
use of org.geotoolkit.display.canvas.control.CanvasMonitor in project geotoolkit by Geomatys.
the class Portrayer method portray.
public BufferedImage portray(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
canvas.setSize(canvasDef.getDimension());
canvas.setRenderingHints(sceneDef.getHints());
final Color bgColor = canvasDef.getBackground();
if (bgColor != null) {
canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
}
final CanvasMonitor monitor = canvasDef.getMonitor();
if (monitor != null) {
canvas.setMonitor(monitor);
}
final MapLayers context = sceneDef.getContext();
container.setContext(context);
try {
canvas.setObjectiveCRS(crs);
} catch (TransformException | FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
// we specifically say to not repect X/Y proportions
canvas.setAxisProportions(!canvasDef.isStretchImage());
try {
canvas.setVisibleArea(contextEnv);
if (canvasDef.getAzimuth() != 0) {
canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
}
} catch (NoninvertibleTransformException ex) {
throw new PortrayalException(ex);
} catch (TransformException ex) {
throw new PortrayalException(ex);
}
// paints all extensions
final List<PortrayalExtension> extensions = sceneDef.extensions();
if (extensions != null) {
for (final PortrayalExtension extension : extensions) {
if (extension != null)
extension.completeCanvas(canvas);
}
}
canvas.repaint();
final BufferedImage buffer = canvas.getSnapShot();
container.setContext(EMPTY_CONTEXT);
return buffer;
}
Aggregations