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