use of org.geotoolkit.display2d.canvas.J2DCanvas 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.J2DCanvas in project geotoolkit by Geomatys.
the class ColorModelTest method testOpaqueUnpredictableBackground.
@Test
public void testOpaqueUnpredictableBackground() throws NoSuchAuthorityCodeException, FactoryException, PortrayalException {
final MapLayers context = MapBuilder.createContext();
final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.geographic());
env.setRange(0, -180, 180);
env.setRange(1, -90, 90);
final CanvasDef cdef = new CanvasDef(new Dimension(800, 600), env);
cdef.setBackground(Color.GREEN);
final BufferedImage img = DefaultPortrayalService.portray(cdef, new SceneDef(context, null, new PortrayalExtension() {
@Override
public void completeCanvas(J2DCanvas canvas) throws PortrayalException {
canvas.setBackgroundPainter(new GradiantColorPainter());
}
}));
// background is opaque we should obtain an RGB color model
assertTrue(!(img.getColorModel() instanceof IndexColorModel));
assertEquals(ColorSpace.TYPE_RGB, img.getColorModel().getColorSpace().getType());
assertEquals(3, img.getColorModel().getNumComponents());
assertEquals(3, img.getColorModel().getNumColorComponents());
}
use of org.geotoolkit.display2d.canvas.J2DCanvas 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;
}
use of org.geotoolkit.display2d.canvas.J2DCanvas in project geotoolkit by Geomatys.
the class ReportDemo method main.
public static void main(String[] args) throws Exception {
Demos.init();
final InputStream template = ReportDemo.class.getResourceAsStream("/data/report/complexReport.jrxml");
final Entry<JasperReport, FeatureType> entry = JasperReportService.prepareTemplate(template);
final JasperReport report = entry.getKey();
final FeatureType type = entry.getValue();
System.out.println(type);
// source to make an atlas ----------------------------------------------------
final FeatureStore store = (FeatureStore) DataStores.open((Map) Collections.singletonMap("path", ReportDemo.class.getResource("/data/world/Countries.shp").toURI()));
final GenericName name = store.getNames().iterator().next();
final FeatureCollection countries = store.createSession(true).getFeatureCollection(new Query(name));
// Iterator over all the countries --------------------------------------------
final FeatureIterator ite = countries.iterator();
// We map the feature type to the report type ---------------------------------
final GenericMappingFeatureIterator mapped = new GenericMappingFeatureIterator(ite, new FeatureMapper() {
@Override
public FeatureType getSourceType() {
return countries.getType();
}
@Override
public FeatureType getTargetType() {
return type;
}
@Override
public Feature transform(Feature feature) {
final Feature modified = type.newInstance();
// create the main map with a single feature ------------------
final FeatureCollection col = FeatureStoreUtilities.collection(feature);
final MapLayers context = MapBuilder.createContext();
final MutableStyle style = RandomStyleBuilder.createRandomVectorStyle(col.getType());
final MapLayer layer = MapBuilder.createLayer(col);
layer.setStyle(style);
context.getComponents().add(layer);
try {
// add a custom decoration on our map.
final GridTemplate gridTemplate = new DefaultGridTemplate(CommonCRS.WGS84.normalizedGeographic(), new BasicStroke(1.5f), new Color(120, 120, 120, 200), new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3, new float[] { 5, 5 }, 0), new Color(120, 120, 120, 60), new Font("serial", Font.BOLD, 12), Color.GRAY, 0, Color.WHITE, new Font("serial", Font.ITALIC, 10), Color.GRAY, 0, Color.WHITE);
final PortrayalExtension ext = new PortrayalExtension() {
@Override
public void completeCanvas(J2DCanvas canvas) throws PortrayalException {
canvas.getContainer().getRoot().getChildren().add(new GraphicGridJ2D(canvas, gridTemplate));
}
};
final CanvasDef canvasDef = new CanvasDef(new Dimension(1, 1), null);
canvasDef.setBackground(Color.WHITE);
canvasDef.setStretchImage(false);
canvasDef.setEnvelope(Envelopes.transform(context.getEnvelope().get(), CRS.forCode("EPSG:3395")));
final SceneDef sceneDef = new SceneDef(context, null, ext);
final MapDef mapdef = new MapDef(canvasDef, sceneDef, null);
modified.setPropertyValue("map3", mapdef);
} catch (Exception ex) {
ex.printStackTrace();
}
// casual attributs -------------------
modified.setPropertyValue("CNTRY_NAME", feature.getProperty("CNTRY_NAME").getValue());
modified.setPropertyValue("POP_CNTRY", feature.getProperty("POP_CNTRY").getValue());
// chart -------------------------
final DefaultPieDataset pds = new DefaultPieDataset();
pds.setValue((Comparable) feature.getProperty("SOVEREIGN").getValue(), Math.random());
pds.setValue((Comparable) feature.getProperty("ISO_3DIGIT").getValue(), Math.random());
final JFreeChart chart = ChartFactory.createPieChart("Info", pds, true, true, Locale.FRENCH);
modified.setPropertyValue("chart4", new ChartDef(chart));
// legend --------------------------
modified.setPropertyValue("legend5", new LegendDef());
// scale bar -------------------
modified.setPropertyValue("scalebar6", new ScaleBarDef());
// north arow -------------------
modified.setPropertyValue("northarrow7", new NorthArrowDef());
// subtable --------------
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("subdata");
ftb.addAttribute(Integer.class).setName("men");
ftb.addAttribute(Integer.class).setName("women");
ftb.addAttribute(String.class).setName("desc");
final FeatureType subType = ftb.build();
final FeatureCollection subcol = FeatureStoreUtilities.collection("sub", subType);
try {
FeatureWriter fw = subcol.getSession().getFeatureStore().getFeatureWriter(Query.filtered(subType.getName().toString(), Filter.exclude()));
for (int i = 0, n = new Random().nextInt(20); i < n; i++) {
Feature f = fw.next();
f.setPropertyValue("men", new Random().nextInt());
f.setPropertyValue("women", new Random().nextInt());
f.setPropertyValue("desc", "some text from attribut");
fw.write();
}
fw.close();
} catch (DataStoreException ex) {
ex.printStackTrace();
}
modified.setPropertyValue("table8", new CollectionDataSource(subcol));
return modified;
}
});
// Generate the report --------------------------------------------------------
final OutputDef output = new OutputDef(JasperReportService.MIME_PDF, new File("atlas.pdf"));
JasperReportService.generateReport(report, mapped, null, output);
}
use of org.geotoolkit.display2d.canvas.J2DCanvas in project geotoolkit by Geomatys.
the class Canvas2DSynchronizer method propertyChange.
/**
* Listen to map movements and propagate to other maps.
*/
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (!J2DCanvas.GRIDGEOMETRY_KEY.equals(evt.getPropertyName())) {
return;
}
if (updating)
return;
updating = true;
final J2DCanvas baseCanvas = (J2DCanvas) evt.getSource();
final CoordinateReferenceSystem crs = baseCanvas.getObjectiveCRS();
final AffineTransform centerTransform = baseCanvas.getCenterTransform();
for (CanvasState state : canvas) {
if (state.canvas == baseCanvas || !state.isTarget)
continue;
try {
state.canvas.setObjectiveCRS(crs);
state.canvas.setCenterTransform(centerTransform);
} catch (TransformException | FactoryException ex) {
Logger.getLogger("org.geotoolkit.display2d").log(Level.INFO, ex.getMessage(), ex);
}
}
updating = false;
}
Aggregations