Search in sources :

Example 1 with CanvasMonitor

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;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) TransformException(org.opengis.referencing.operation.TransformException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeographicBoundingBox(org.opengis.metadata.extent.GeographicBoundingBox) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope2D(org.apache.sis.geometry.Envelope2D) PortrayalException(org.geotoolkit.display.PortrayalException) TransformException(org.opengis.referencing.operation.TransformException) MismatchedFeatureException(org.opengis.feature.MismatchedFeatureException) FactoryException(org.opengis.util.FactoryException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 2 with CanvasMonitor

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);
        }
    }
}
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 3 with CanvasMonitor

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;
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) MathTransform(org.opengis.referencing.operation.MathTransform) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) Graphics2D(java.awt.Graphics2D)

Example 4 with CanvasMonitor

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());
    }
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) Color(java.awt.Color) CannotReprojectException(org.geotoolkit.processing.coverage.resample.CannotReprojectException) NullArgumentException(org.apache.sis.util.NullArgumentException) LinearTransform(org.apache.sis.referencing.operation.transform.LinearTransform) PortrayalException(org.geotoolkit.display.PortrayalException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) TransformException(org.opengis.referencing.operation.TransformException) NullArgumentException(org.apache.sis.util.NullArgumentException) CannotReprojectException(org.geotoolkit.processing.coverage.resample.CannotReprojectException) ProcessException(org.geotoolkit.process.ProcessException) BufferedImage(java.awt.image.BufferedImage) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) Paint(java.awt.Paint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) Graphics2D(java.awt.Graphics2D) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) AffineTransform(java.awt.geom.AffineTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) WritableRenderedImage(java.awt.image.WritableRenderedImage) RenderedImage(java.awt.image.RenderedImage) PortrayalException(org.geotoolkit.display.PortrayalException)

Example 5 with CanvasMonitor

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;
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) FactoryException(org.opengis.util.FactoryException) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) BufferedImage(java.awt.image.BufferedImage) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

CanvasMonitor (org.geotoolkit.display.canvas.control.CanvasMonitor)7 PortrayalException (org.geotoolkit.display.PortrayalException)5 TransformException (org.opengis.referencing.operation.TransformException)5 Color (java.awt.Color)4 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)4 FactoryException (org.opengis.util.FactoryException)4 Graphics2D (java.awt.Graphics2D)3 IOException (java.io.IOException)3 ProcessException (org.geotoolkit.process.ProcessException)3 Envelope (org.opengis.geometry.Envelope)3 AffineTransform (java.awt.geom.AffineTransform)2 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)2 BufferedImage (java.awt.image.BufferedImage)2 RenderedImage (java.awt.image.RenderedImage)2 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)2 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)2 MapLayers (org.apache.sis.portrayal.MapLayers)2 LinearTransform (org.apache.sis.referencing.operation.transform.LinearTransform)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 SolidColorPainter (org.geotoolkit.display2d.canvas.painter.SolidColorPainter)2