use of org.geotoolkit.display2d.ext.grid.DefaultGridTemplate 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.ext.grid.DefaultGridTemplate in project geotoolkit by Geomatys.
the class DecorationXMLParser method parseDecoration.
private static void parseDecoration(final DecorationExtension deco, final Element decoNode) {
final String type = decoNode.getAttribute(ATT_NAME);
final Map<String, String> params = parseParameters(decoNode);
final BackgroundTemplate background = parseBackground(decoNode);
final Map<String, Object> parsed = new HashMap<>();
// offsets are the same for everyone
parsed.put(PARAM_OFFSET_X, parseInteger(params.get(PARAM_OFFSET_X), 0));
parsed.put(PARAM_OFFSET_Y, parseInteger(params.get(PARAM_OFFSET_Y), 0));
if (type.equalsIgnoreCase(TYPE_COMPAS)) {
final NorthArrowTemplate template = new DefaultNorthArrowTemplate(background, parseURL(params.get(PARAM_SOURCE), DecorationXMLParser.class.getResource("/org/geotoolkit/icon/boussole.svg")), new Dimension(parseInteger(params.get(PARAM_WIDTH), 100), parseInteger(params.get(PARAM_HEIGHT), 100)));
parsed.put(ATT_NAME, TYPE_COMPAS);
parsed.put(TYPE_COMPAS, template);
parsed.put(PARAM_POSITION, parsePosition(params.get(PARAM_POSITION), SwingConstants.NORTH_EAST));
} else if (type.equalsIgnoreCase(TYPE_GRID)) {
CoordinateReferenceSystem crs = null;
if (params.get(PARAM_CRS) != null) {
try {
crs = CRS.forCode(params.get(PARAM_CRS));
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, null, ex);
}
}
Stroke mainLineStroke = new BasicStroke(2);
Paint mainLinePaint = Color.DARK_GRAY;
Font mainLineFont = new Font("serial", Font.BOLD, 14);
Stroke secondLineStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10, new float[] { 5, 5 }, 0);
Paint secondLinePaint = Color.GRAY;
Font secondLineFont = new Font("serial", Font.BOLD, 14);
NodeList nodes = decoNode.getElementsByTagName(TAG_MAIN);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
final Element sub = (Element) nodes.item(i);
final Map<String, String> subParams = parseParameters(sub);
mainLineStroke = parseStroke(subParams.get(PARAM_STROKE_WIDTH), subParams.get(PARAM_STROKE_DASHES));
mainLinePaint = parseColor(subParams.get(PARAM_STROKE_COLOR), subParams.get(PARAM_STROKE_OPACITY), Color.DARK_GRAY);
mainLineFont = parseFont(subParams.get(PARAM_FONT));
}
nodes = decoNode.getElementsByTagName(TAG_SECOND);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
final Element sub = (Element) nodes.item(i);
final Map<String, String> subParams = parseParameters(sub);
secondLineStroke = parseStroke(subParams.get(PARAM_STROKE_WIDTH), subParams.get(PARAM_STROKE_DASHES));
secondLinePaint = parseColor(subParams.get(PARAM_STROKE_COLOR), subParams.get(PARAM_STROKE_OPACITY), Color.DARK_GRAY);
secondLineFont = parseFont(subParams.get(PARAM_FONT));
}
final GridTemplate template = new DefaultGridTemplate(crs, mainLineStroke, mainLinePaint, secondLineStroke, secondLinePaint, mainLineFont, mainLinePaint, 0, new Color(0f, 0f, 0f, 0f), secondLineFont, secondLinePaint, 0, new Color(0f, 0f, 0f, 0f));
parsed.put(ATT_NAME, TYPE_GRID);
parsed.put(TYPE_GRID, template);
} else if (type.equalsIgnoreCase(TYPE_IMAGE)) {
final URL source = parseURL(params.get(PARAM_SOURCE), null);
BufferedImage buffer;
try {
buffer = ImageIO.read(source);
} catch (Exception ex) {
LOGGER.log(Level.WARNING, null, ex);
buffer = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
final ImageTemplate template = new DefaultImageTemplate(background, buffer);
parsed.put(ATT_NAME, TYPE_IMAGE);
parsed.put(TYPE_IMAGE, template);
parsed.put(PARAM_POSITION, parsePosition(params.get(PARAM_POSITION), SwingConstants.NORTH_WEST));
} else if (type.equalsIgnoreCase(TYPE_LEGEND)) {
final LegendTemplate template = new DefaultLegendTemplate(background, parseInteger(params.get(PARAM_GAP), 2), new Dimension(parseInteger(params.get(PARAM_GLYPH_WIDTH), 30), parseInteger(params.get(PARAM_GLYPH_HEIGHT), 20)), parseFont(params.get(PARAM_SECOND_FONT)), parseBoolean(params.get(PARAM_LAYER_NAME), true), parseFont(params.get(PARAM_MAIN_FONT)));
parsed.put(ATT_NAME, TYPE_LEGEND);
parsed.put(TYPE_LEGEND, template);
parsed.put(PARAM_POSITION, parsePosition(params.get(PARAM_POSITION), SwingConstants.EAST));
} else if (type.equalsIgnoreCase(TYPE_SCALE_GRAPHIC)) {
String unit = params.get(PARAM_UNIT);
if (unit == null || unit.isEmpty()) {
unit = "km";
}
final ScaleBarTemplate template = new DefaultScaleBarTemplate(background, new Dimension(parseInteger(params.get(PARAM_WIDTH), 250), parseInteger(params.get(PARAM_HEIGHT), 30)), 10, false, 5, NumberFormat.getNumberInstance(), Color.BLACK, Color.BLACK, Color.WHITE, 3, true, false, new Font("Serial", Font.PLAIN, 12), true, Units.valueOf(unit));
parsed.put(ATT_NAME, TYPE_SCALE_GRAPHIC);
parsed.put(TYPE_SCALE_GRAPHIC, template);
parsed.put(PARAM_POSITION, parsePosition(params.get(PARAM_POSITION), SwingConstants.SOUTH_WEST));
} else if (type.equalsIgnoreCase(TYPE_SCALE_NUMERIC)) {
// not handle yet
} else if (type.equalsIgnoreCase(TYPE_TEXT)) {
String txt = params.get(PARAM_TEXT);
if (txt == null) {
txt = "";
}
final TextTemplate template = new DefaultTextTemplate(background, txt);
parsed.put(ATT_NAME, TYPE_TEXT);
parsed.put(TYPE_TEXT, template);
parsed.put(PARAM_POSITION, parsePosition(params.get(PARAM_POSITION), SwingConstants.SOUTH_WEST));
}
deco.decorations.add(parsed);
}
Aggregations