Search in sources :

Example 1 with GraphicVisitor

use of org.geotoolkit.display2d.GraphicVisitor in project geotoolkit by Geomatys.

the class PortrayalServiceTest method testCoverageVisit0_360.

/**
 * Test picking on a coverage in range 0-360.
 * @throws PortrayalException
 */
@Test
public void testCoverageVisit0_360() throws PortrayalException {
    // Create 0-360 coverage
    final BufferedImage img = new BufferedImage(350, 180, BufferedImage.TYPE_INT_ARGB);
    CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
    crs = AbstractCRS.castOrCopy(crs).forConvention(AxesConvention.POSITIVE_RANGE);
    final AffineTransform2D gridToCrs = new AffineTransform2D(1, 0, 0, -1, 0, 90);
    final GridExtent extent = new GridExtent(350, 180);
    final GridGeometry gg = new GridGeometry(extent, PixelInCell.CELL_CORNER, gridToCrs, crs);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(img);
    gcb.setDomain(gg);
    final GridCoverage coverage = gcb.build();
    final GridCoverageResource gcr = new InMemoryGridCoverageResource(coverage);
    final MapLayer layer = MapBuilder.createCoverageLayer(gcr);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    final GeneralEnvelope viewEnv = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    viewEnv.setRange(0, -180, +180);
    viewEnv.setRange(1, -90, +90);
    final AtomicInteger count = new AtomicInteger();
    GraphicVisitor gv = new GraphicVisitor() {

        @Override
        public void startVisit() {
        }

        @Override
        public void endVisit() {
        }

        @Override
        public void visit(Presentation graphic, RenderingContext context, SearchArea area) {
            count.incrementAndGet();
        }

        @Override
        public boolean isStopRequested() {
            return false;
        }
    };
    final SceneDef scene = new SceneDef(context);
    final CanvasDef canvas = new CanvasDef(new Dimension(360, 180), viewEnv);
    final VisitDef visit = new VisitDef(new Rectangle(10, 80, 2, 2), gv);
    DefaultPortrayalService.visit(canvas, scene, visit);
    assertEquals(1, count.get());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) RenderingContext(org.geotoolkit.display.canvas.RenderingContext) GridExtent(org.apache.sis.coverage.grid.GridExtent) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) SearchArea(org.geotoolkit.display.SearchArea) GraphicVisitor(org.geotoolkit.display2d.GraphicVisitor) MapLayer(org.apache.sis.portrayal.MapLayer) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Presentation(org.apache.sis.internal.map.Presentation) BufferedImage(java.awt.image.BufferedImage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 2 with GraphicVisitor

use of org.geotoolkit.display2d.GraphicVisitor 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)

Aggregations

Dimension (java.awt.Dimension)2 MapLayers (org.apache.sis.portrayal.MapLayers)2 GraphicVisitor (org.geotoolkit.display2d.GraphicVisitor)2 Rectangle (java.awt.Rectangle)1 Shape (java.awt.Shape)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IIOException (javax.imageio.IIOException)1 SampleDimension (org.apache.sis.coverage.SampleDimension)1 DisjointExtentException (org.apache.sis.coverage.grid.DisjointExtentException)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 GridCoverageBuilder (org.apache.sis.coverage.grid.GridCoverageBuilder)1 GridExtent (org.apache.sis.coverage.grid.GridExtent)1 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)1 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)1 Presentation (org.apache.sis.internal.map.Presentation)1 AffineTransform2D (org.apache.sis.internal.referencing.j2d.AffineTransform2D)1 MapLayer (org.apache.sis.portrayal.MapLayer)1