Search in sources :

Example 1 with RenderingContext2D

use of org.geotoolkit.display2d.canvas.RenderingContext2D 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 RenderingContext2D

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

the class CachedPointSymbolizerTest method testMargin.

@Test
public void testMargin() throws FactoryException {
    final GridGeometry grid = new GridGeometry(new GridExtent(1, 1), CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic()), GridOrientation.HOMOTHETY);
    final RenderingContext2D ctx = new RenderingContext2D(grid, null);
    {
        // NO ANCHOR, NO DISPLACEMENT
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, DEFAULT_DISPLACEMENT);
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width)
        assertEquals(8f, margin, DELTA);
    }
    {
        // NO ANCHOR
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, SF.displacement(10, 15));
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width) + 15(disp)
        assertEquals(23f, margin, DELTA);
    }
    {
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, SF.anchorPoint(0, 1.7), SF.displacement(10, 15));
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width) + 15(disp) + 16*(1.7-0.5)
        assertEquals(23f + 19.2f, margin, DELTA);
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) PointSymbolizer(org.opengis.style.PointSymbolizer) GridExtent(org.apache.sis.coverage.grid.GridExtent) Stroke(org.opengis.style.Stroke) Fill(org.opengis.style.Fill) Graphic(org.opengis.style.Graphic) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Mark(org.opengis.style.Mark) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with RenderingContext2D

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

the class CanvasRenderer method repaint.

/**
 * {@inheritDoc }
 */
@Override
public boolean repaint(final Shape area) {
    monitor.renderingStarted();
    fireRenderingStateChanged(RENDERING);
    final Graphics2D output = g2d;
    output.addRenderingHints(getHints(true));
    final RenderingContext2D context = prepareContext(output);
    // paint background if there is one.
    if (painter != null) {
        painter.paint(context);
    }
    boolean dataPainted = false;
    final GraphicContainer container = getContainer();
    if (container != null) {
        dataPainted |= render(context, container.flatten(true));
    }
    /**
     * End painting, erase dirtyArea
     */
    output.dispose();
    fireRenderingStateChanged(ON_HOLD);
    monitor.renderingFinished();
    return dataPainted;
}
Also used : GraphicContainer(org.geotoolkit.display.container.GraphicContainer) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Graphics2D(java.awt.Graphics2D)

Example 4 with RenderingContext2D

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

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

the class MapLayerJ2D method paintLayer.

/**
 * Render layer, will only be painted if an appropriate graphic builder is attached
 * to it.
 */
public Stream<Presentation> paintLayer(final RenderingContext2D context) throws PortrayalException, DataStoreException {
    // we abort painting if the layer is not visible.
    if (!item.isVisible())
        return Stream.empty();
    // we abort if opacity is to low
    final double opacity = item.getOpacity();
    if (opacity < 1e-6)
        return Stream.empty();
    if (1 - opacity < 1e-6) {
        // we are very close to opacity one, no need to create a intermediate image
        return streamPresentations(context);
    } else {
        // create an intermediate layer which will be painted on the main context
        // after with the given opacity
        final Rectangle rect = context.getCanvasDisplayBounds();
        final BufferedImage inter = createBufferedImage(ColorModel.getRGBdefault(), ColorModel.getRGBdefault().createCompatibleSampleModel(rect.width, rect.height));
        final Graphics2D g2d = inter.createGraphics();
        final RenderingContext2D interContext = context.create(g2d);
        final J2DPainter painter = new J2DPainter();
        try (Stream<Presentation> stream = streamPresentations(interContext)) {
            painter.paint(interContext, stream, true);
        }
        final GridCoverageBuilder gcb = new GridCoverageBuilder();
        gcb.setDomain(interContext.getGridGeometry());
        gcb.setValues(inter);
        final GridCoverage coverage = gcb.build();
        final RasterPresentation rp = new RasterPresentation(layer, null, coverage);
        rp.forGrid(context);
        rp.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity);
        return Stream.of(rp);
    }
}
Also used : GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) Rectangle(java.awt.Rectangle) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) Presentation(org.apache.sis.internal.map.Presentation) RasterPresentation(org.geotoolkit.display2d.presentation.RasterPresentation) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

RenderingContext2D (org.geotoolkit.display2d.canvas.RenderingContext2D)6 BufferedImage (java.awt.image.BufferedImage)3 ArrayList (java.util.ArrayList)3 MapLayer (org.apache.sis.portrayal.MapLayer)3 Dimension (java.awt.Dimension)2 Graphics2D (java.awt.Graphics2D)2 List (java.util.List)2 Presentation (org.apache.sis.internal.map.Presentation)2 FeatureSet (org.apache.sis.storage.FeatureSet)2 J2DCanvasBuffered (org.geotoolkit.display2d.canvas.J2DCanvasBuffered)2 Envelope (org.opengis.geometry.Envelope)2 Graphic (org.opengis.style.Graphic)2 PointSymbolizer (org.opengis.style.PointSymbolizer)2 Rectangle (java.awt.Rectangle)1 RenderingHints (java.awt.RenderingHints)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1