* A legend graphic is produced for each applicable rule. They're being held
* here until the process is done and then painted on a "stack" like legend.
*/
final List<RenderedImage> legendsStack = new ArrayList<RenderedImage>(ruleCount);
final SLDStyleFactory styleFactory = new SLDStyleFactory();
double minimumSymbolSize = MINIMUM_SYMBOL_SIZE;
// get minSymbolSize from LEGEND_OPTIONS, if defined
if (request.getLegendOptions().get("minSymbolSize") instanceof String) {
String minSymbolSizeOpt = (String) request.getLegendOptions()
.get("minSymbolSize");
try {
minimumSymbolSize = Double.parseDouble(minSymbolSizeOpt);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Invalid minSymbolSize value: should be a number");
}
}
// calculate the symbols rescaling factor necessary for them to be
// drawn inside the icon box
double symbolScale = calcSymbolScale(w, h, layer, sampleFeature,
applicableRules, minimumSymbolSize);
for (int i = 0; i < ruleCount; i++) {
final RenderedImage image = ImageUtils.createImage(w, h, (IndexColorModel) null,
transparent);
final Map<RenderingHints.Key, Object> hintsMap = new HashMap<RenderingHints.Key, Object>();
final Graphics2D graphics = ImageUtils.prepareTransparency(transparent, LegendUtils.getBackgroundColor(request), image,
hintsMap);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Feature sample = getSampleFeatureForRule(layer,
sampleFeature, applicableRules[i]);
FilterFactory ff = CommonFactoryFinder.getFilterFactory();
final Symbolizer[] symbolizers = applicableRules[i].getSymbolizers();
final GraphicLegend legend = applicableRules[i].getLegend();
// If this rule has a legend graphic defined in the SLD, use it
if (legend != null) {
if (this.samplePoint == null) {
Coordinate coord = new Coordinate(w / 2, h / 2);
try {
this.samplePoint = new LiteShape2(geomFac.createPoint(coord), null, null, false);
} catch (Exception e) {
this.samplePoint = null;
}
}
shapePainter.paint(graphics, this.samplePoint, legend, scaleDenominator, false);
} else {
for (int sIdx = 0; sIdx < symbolizers.length; sIdx++) {
Symbolizer symbolizer = symbolizers[sIdx];
if (symbolizer instanceof RasterSymbolizer) {
// skip it
} else {
// rescale symbols if needed
if (symbolScale > 1.0
&& symbolizer instanceof PointSymbolizer) {
PointSymbolizer pointSymbolizer = cloneSymbolizer(symbolizer);
if (pointSymbolizer.getGraphic() != null) {
double size = getPointSymbolizerSize(sample,
pointSymbolizer, Math.min(w, h) - 4);
pointSymbolizer.getGraphic().setSize(
ff.literal(size / symbolScale
+ minimumSymbolSize));
symbolizer = pointSymbolizer;
}
}
Style2D style2d = styleFactory.createStyle(sample,
symbolizer, scaleRange);
LiteShape2 shape = getSampleShape(symbolizer, w, h);
if (style2d != null) {
shapePainter.paint(graphics, shape, style2d,