Search in sources :

Example 1 with ContextContainer2D

use of org.geotoolkit.display2d.container.ContextContainer2D 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);
        }
    }
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 2 with ContextContainer2D

use of org.geotoolkit.display2d.container.ContextContainer2D in project geotoolkit by Geomatys.

the class DefaultPortrayalService method visit.

public static void visit(final CanvasDef canvasDef, final SceneDef sceneDef, final VisitDef visitDef) throws PortrayalException {
    final Envelope contextEnv = canvasDef.getEnvelope();
    final Dimension canvasDimension = canvasDef.getDimension();
    final Hints hints = sceneDef.getHints();
    final MapLayers context = sceneDef.getContext();
    final boolean strechImage = canvasDef.isStretchImage();
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(contextEnv.getCoordinateReferenceSystem(), canvasDimension, hints);
    final ContextContainer2D renderer = new ContextContainer2D(canvas);
    canvas.setContainer(renderer);
    renderer.setContext(context);
    try {
        canvas.setObjectiveCRS(contextEnv.getCoordinateReferenceSystem());
    } catch (TransformException | FactoryException ex) {
        throw new PortrayalException("Could not set objective crs", ex);
    }
    // we specifically say to not repect X/Y proportions
    canvas.setAxisProportions(!strechImage);
    try {
        canvas.setVisibleArea(contextEnv);
    } catch (NoninvertibleTransformException | TransformException ex) {
        throw new PortrayalException(ex);
    }
    final Shape selectedArea = visitDef.getArea();
    final GraphicVisitor visitor = visitDef.getVisitor();
    try {
        canvas.getGraphicsIn(selectedArea, visitor);
    } catch (Exception ex) {
        if (ex instanceof PortrayalException) {
            throw (PortrayalException) ex;
        } else {
            throw new PortrayalException(ex);
        }
    } finally {
        visitor.endVisit();
        canvas.clearCache();
    }
}
Also used : Shape(java.awt.Shape) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) GraphicVisitor(org.geotoolkit.display2d.GraphicVisitor) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 3 with ContextContainer2D

use of org.geotoolkit.display2d.container.ContextContainer2D in project geotoolkit by Geomatys.

the class GraphicLegendJ2D method paint.

@Override
protected void paint(final RenderingContext2D context, final int position, final int[] offset) {
    if (!isVisible()) {
        return;
    }
    final GraphicContainer container = getCanvas().getContainer();
    if (!(container instanceof ContextContainer2D))
        return;
    final ContextContainer2D cc = (ContextContainer2D) container;
    final MapLayers mapContext = cc.getContext();
    final Graphics2D g = context.getGraphics();
    context.switchToDisplayCRS();
    final Rectangle bounds = context.getCanvasDisplayBounds();
    Dimension maxSize = J2DLegendUtilities.estimate(g, mapContext, template, true);
    final int imgHeight = maxSize.height;
    final int imgWidth = maxSize.width;
    int x = 0;
    int y = 0;
    switch(position) {
        case NORTH:
            x = (bounds.width - imgWidth) / 2 + offset[0];
            y = offset[1];
            break;
        case NORTH_EAST:
            x = (bounds.width - imgWidth) - offset[0];
            y = offset[1];
            break;
        case NORTH_WEST:
            x = offset[0];
            y = offset[1];
            break;
        case SOUTH:
            x = (bounds.width - imgWidth) / 2 + offset[0];
            y = (bounds.height - imgHeight) - offset[1];
            break;
        case SOUTH_EAST:
            x = (bounds.width - imgWidth) - offset[0];
            y = (bounds.height - imgHeight) - offset[1];
            break;
        case SOUTH_WEST:
            x = offset[0];
            y = (bounds.height - imgHeight) - offset[1];
            break;
        case CENTER:
            x = (bounds.width - imgWidth) / 2 + offset[0];
            y = (bounds.height - imgHeight) / 2 + offset[1];
            break;
        case EAST:
            x = (bounds.width - imgWidth) - offset[0];
            y = (bounds.height - imgHeight) / 2 + offset[1];
            break;
        case WEST:
            x = offset[0];
            y = (bounds.height - imgHeight) / 2 + offset[1];
            break;
    }
    try {
        // paint all labels, so that we avoid conflicts
        context.getLabelRenderer(true).portrayLabels();
    } catch (TransformException ex) {
        context.getMonitor().exceptionOccured(ex, Level.WARNING);
    }
    final Rectangle area = new Rectangle(x, y, imgWidth, imgHeight);
    J2DLegendUtilities.paintLegend(mapContext, g, area, template);
}
Also used : GraphicContainer(org.geotoolkit.display.container.GraphicContainer) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) Rectangle(java.awt.Rectangle) TransformException(org.opengis.referencing.operation.TransformException) Dimension(java.awt.Dimension) MapLayers(org.apache.sis.portrayal.MapLayers) Graphics2D(java.awt.Graphics2D)

Aggregations

MapLayers (org.apache.sis.portrayal.MapLayers)3 ContextContainer2D (org.geotoolkit.display2d.container.ContextContainer2D)3 TransformException (org.opengis.referencing.operation.TransformException)3 Dimension (java.awt.Dimension)2 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)2 IOException (java.io.IOException)2 IIOException (javax.imageio.IIOException)2 DisjointExtentException (org.apache.sis.coverage.grid.DisjointExtentException)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 PortrayalException (org.geotoolkit.display.PortrayalException)2 GO2Hints (org.geotoolkit.display2d.GO2Hints)2 Hints (org.geotoolkit.factory.Hints)2 ProcessException (org.geotoolkit.process.ProcessException)2 Envelope (org.opengis.geometry.Envelope)2 FactoryException (org.opengis.util.FactoryException)2 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 Rectangle (java.awt.Rectangle)1 Shape (java.awt.Shape)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1