use of org.geotools.map.legend.Drawer in project hale by halestudio.
the class DefinitionImages method getLegendImage.
/**
* Get a legend image for a given type definition
*
* @param type the type definition
* @param dataSet the data set the type definition belongs to
* @param definedOnly if only for defined styles a image shall be created
* @return the legend image or <code>null</code>
*/
protected BufferedImage getLegendImage(TypeDefinition type, DataSet dataSet, boolean definedOnly) {
StyleService ss = PlatformUI.getWorkbench().getService(StyleService.class);
Style style = (definedOnly) ? (ss.getDefinedStyle(type)) : (ss.getStyle(type, dataSet));
if (style == null) {
return null;
}
// create a dummy feature based on the style
Drawer d = Drawer.create();
SimpleFeature feature = null;
Symbolizer[] symbolizers = SLD.symbolizers(style);
if (symbolizers.length > 0) {
Symbolizer symbolizer = symbolizers[0];
if (symbolizer instanceof LineSymbolizer) {
feature = d.feature(d.line(LINE_POINTS));
} else if (symbolizer instanceof PointSymbolizer) {
feature = d.feature(d.point(WIDTH / 2, HEIGHT / 2));
}
if (symbolizer instanceof PolygonSymbolizer) {
feature = d.feature(d.polygon(POLY_POINTS));
}
}
if (feature != null) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
// GraphicsEnvironment.getLocalGraphicsEnvironment().
// getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(WIDTH, HEIGHT,
// Transparency.TRANSLUCENT);
// use white background to have a neutral color even if selected
Color bg = Color.WHITE;
Graphics2D g = image.createGraphics();
try {
g.setColor(bg);
g.fillRect(0, 0, WIDTH, HEIGHT);
} finally {
g.dispose();
}
d.drawDirect(image, feature, style);
return image;
}
return null;
}
Aggregations