Search in sources :

Example 1 with J2DCanvasBuffered

use of org.geotoolkit.display2d.canvas.J2DCanvasBuffered in project geotoolkit by Geomatys.

the class ProjectedGeometryTest method createProjectedGeometry.

private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
    final int canvasWidth = canvasBounds.width;
    final int canvasHeight = canvasBounds.height;
    // build a maplayer
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
    final FeatureType type = ftb.build();
    final Feature feature = type.newInstance();
    JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
    feature.setPropertyValue("geom", geometry);
    final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
    final List<GraphicalSymbol> symbols = new ArrayList<>();
    symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
    final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
    final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
    final MutableStyle style = SF.style(ps);
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(style);
    // build a rendering canvas
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
    canvas.applyTransform(objToDisp);
    final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
    final ProjectedGeometry pg = new ProjectedGeometry(context);
    pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
    Envelope env = canvas.getVisibleEnvelope();
    System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
    System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
    return pg;
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PointSymbolizer(org.opengis.style.PointSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Graphic(org.opengis.style.Graphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet)

Example 2 with J2DCanvasBuffered

use of org.geotoolkit.display2d.canvas.J2DCanvasBuffered in project geotoolkit by Geomatys.

the class PortrayalServiceTest method testGridGeometryPreserved.

/**
 * Test the grid geometry we provided is used.
 */
@Test
public void testGridGeometryPreserved() throws PortrayalException, FactoryException {
    final SceneDef sceneDef = new SceneDef();
    final GridGeometry gridGeometry = new GridGeometry(new GridExtent(360, 180), CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic()), GridOrientation.HOMOTHETY);
    final CanvasDef canvasDef = new CanvasDef(gridGeometry);
    final J2DCanvas canvas = new J2DCanvasBuffered(CommonCRS.WGS84.geographic(), new Dimension(10, 10));
    DefaultPortrayalService.prepareCanvas(canvas, canvasDef, sceneDef);
    assertEquals(gridGeometry, canvas.getGridGeometry());
    assertEquals(gridGeometry, canvas.getGridGeometry2D());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) J2DCanvas(org.geotoolkit.display2d.canvas.J2DCanvas) Test(org.junit.Test)

Example 3 with J2DCanvasBuffered

use of org.geotoolkit.display2d.canvas.J2DCanvasBuffered in project geotoolkit by Geomatys.

the class DefaultPortrayalService method present.

// //////////////////////////////////////////////////////////////////////////
// PRESENTING A CONTEXT ////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
 * Generate presentation objects for a scene.
 * @param canvasDef
 * @param sceneDef
 * @return stream of Presentation instance.
 */
public static Stream<Presentation> present(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
    Stream<Presentation> stream = Stream.empty();
    final Envelope contextEnv = canvasDef.getEnvelope();
    final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
    final Dimension dim = canvasDef.getDimension();
    final BufferedImage img = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(crs, canvasDef.getDimension(), sceneDef.getHints());
    prepareCanvas(canvas, canvasDef, sceneDef);
    final RenderingContext2D renderContext = canvas.prepareContext(img.createGraphics());
    final MapLayers context = sceneDef.getContext();
    final List<MapLayer> layers = MapBuilder.getLayers(context);
    for (MapLayer layer : layers) {
        if (!layer.isVisible())
            continue;
        final Resource resource = layer.getData();
        stream = Stream.concat(stream, present(layer, resource, renderContext));
    }
    return stream;
}
Also used : J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) MapLayer(org.apache.sis.portrayal.MapLayer) Resource(org.apache.sis.storage.Resource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Dimension(java.awt.Dimension) Presentation(org.apache.sis.internal.map.Presentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) Envelope(org.opengis.geometry.Envelope) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 4 with J2DCanvasBuffered

use of org.geotoolkit.display2d.canvas.J2DCanvasBuffered 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 5 with J2DCanvasBuffered

use of org.geotoolkit.display2d.canvas.J2DCanvasBuffered in project geotoolkit by Geomatys.

the class DefaultPortrayalService method portray.

public static BufferedImage portray(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
    final Envelope contextEnv = canvasDef.getEnvelope();
    final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
    final Graphics2D graphics = canvasDef.getGraphics();
    final J2DCanvas canvas;
    if (graphics == null) {
        // render in image
        canvas = new J2DCanvasBuffered(crs, canvasDef.getDimension(), sceneDef.getHints());
    } else {
        // render to provided graphics2D
        canvas = new J2DCanvasDirect(crs, sceneDef.getHints());
        ((J2DCanvasDirect) canvas).setGraphics2D(graphics);
        canvas.setDisplayBounds(new Rectangle(canvasDef.getDimension()));
    }
    prepareCanvas(canvas, canvasDef, sceneDef);
    canvas.repaint();
    BufferedImage buffer = null;
    if (graphics == null) {
        buffer = (BufferedImage) canvas.getSnapShot();
        try {
            canvas.dispose();
        } catch (RuntimeException e) {
            LOGGER.warning("Error on resource disposal. Might cause memory leak.");
            LOGGER.log(Level.FINE, "Error on resource disposal. Might cause memory leak.", e);
        }
    }
    return buffer;
}
Also used : J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) Rectangle(java.awt.Rectangle) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Envelope(org.opengis.geometry.Envelope) J2DCanvas(org.geotoolkit.display2d.canvas.J2DCanvas) J2DCanvasDirect(org.geotoolkit.display2d.canvas.J2DCanvasDirect) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

J2DCanvasBuffered (org.geotoolkit.display2d.canvas.J2DCanvasBuffered)7 Dimension (java.awt.Dimension)6 Envelope (org.opengis.geometry.Envelope)4 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)3 BufferedImage (java.awt.image.BufferedImage)3 PortrayalException (org.geotoolkit.display.PortrayalException)3 TransformException (org.opengis.referencing.operation.TransformException)3 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)2 MapLayer (org.apache.sis.portrayal.MapLayer)2 MapLayers (org.apache.sis.portrayal.MapLayers)2 J2DCanvas (org.geotoolkit.display2d.canvas.J2DCanvas)2 RenderingContext2D (org.geotoolkit.display2d.canvas.RenderingContext2D)2 Hints (org.geotoolkit.factory.Hints)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 Graphics2D (java.awt.Graphics2D)1 Point (java.awt.Point)1 Rectangle (java.awt.Rectangle)1 Shape (java.awt.Shape)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1