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());
}
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();
}
}
Aggregations