2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
59 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
60 |
import org.geotools.factory.GeoTools; |
import org.geotools.factory.GeoTools; |
61 |
import org.geotools.factory.Hints; |
import org.geotools.factory.Hints; |
|
import org.geotools.feature.AttributeType; |
|
|
import org.geotools.feature.Feature; |
|
|
import org.geotools.feature.FeatureType; |
|
62 |
import org.geotools.feature.IllegalAttributeException; |
import org.geotools.feature.IllegalAttributeException; |
63 |
import org.geotools.geometry.jts.LiteShape2; |
import org.geotools.geometry.jts.LiteShape2; |
64 |
import org.geotools.renderer.lite.StyledShapePainter; |
import org.geotools.renderer.lite.StyledShapePainter; |
73 |
import org.geotools.styling.Symbolizer; |
import org.geotools.styling.Symbolizer; |
74 |
import org.geotools.styling.TextSymbolizer; |
import org.geotools.styling.TextSymbolizer; |
75 |
import org.geotools.util.NumberRange; |
import org.geotools.util.NumberRange; |
76 |
|
import org.opengis.feature.simple.SimpleFeature; |
77 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
78 |
|
import org.opengis.feature.type.AttributeDescriptor; |
79 |
|
|
80 |
import schmitzm.geotools.feature.FeatureUtil; |
import schmitzm.geotools.feature.FeatureUtil; |
81 |
|
|
92 |
* <br> |
* <br> |
93 |
* <ul> |
* <ul> |
94 |
* <li>07.02.2008:<br> |
* <li>07.02.2008:<br> |
95 |
* Determining the default values of a {@link FeatureType} by |
* Determining the default values of a {@link SimpleFeatureType} by |
96 |
* {@link FeatureUtil#getDefaultAttributeValues(FeatureType)} instead of using |
* {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of using |
97 |
* {@link AttributeType#createDefaultValue()} directly, because the latter |
* {@link AttributeDescriptor#createDefaultValue()} directly, because the latter |
98 |
* returns {@code null} even though the attribut is not nillable.</li> |
* returns {@code null} even though the attribut is not nillable.</li> |
99 |
* </ul> |
* </ul> |
100 |
* |
* |
243 |
|
|
244 |
return sampleShape; |
return sampleShape; |
245 |
} |
} |
246 |
|
// |
247 |
/** |
// /** |
248 |
* Puts a BufferedImage into this.legendGraphic |
// * Puts a BufferedImage into this.legendGraphic |
249 |
*/ |
// */ |
250 |
public void produceLegendGraphic(FeatureType featureType, Style gt2Style, |
// public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style, |
251 |
Rule[] applicableRules) { |
// Rule[] applicableRules) { |
252 |
|
// |
253 |
// final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles(); |
// // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles(); |
254 |
|
// |
255 |
// if (request.getRule() != null) { |
// // if (request.getRule() != null) { |
256 |
// applicableRules = new Rule[] { request.getRule() }; |
// // applicableRules = new Rule[] { request.getRule() }; |
257 |
// } else { |
// // } else { |
258 |
// applicableRules = getApplicableRules(ftStyles, scaleDenominator); |
// // applicableRules = getApplicableRules(ftStyles, scaleDenominator); |
259 |
// } |
// // } |
260 |
|
// |
261 |
final int ruleCount = applicableRules.length; |
// final int ruleCount = applicableRules.length; |
262 |
|
// |
263 |
/** |
// /** |
264 |
* A legend graphic is produced for each applicable rule. They're being |
// * A legend graphic is produced for each applicable rule. They're being |
265 |
* holded here until the process is done and then painted on a "stack" |
// * holded here until the process is done and then painted on a "stack" |
266 |
* like legend. |
// * like legend. |
267 |
*/ |
// */ |
268 |
final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>( |
// final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>( |
269 |
ruleCount); |
// ruleCount); |
270 |
|
// |
271 |
for (int i = 0; i < ruleCount; i++) { |
// for (int i = 0; i < ruleCount; i++) { |
272 |
BufferedImage image = createImageForRule(applicableRules[i], |
// BufferedImage image = createImageForRule(applicableRules[i], |
273 |
featureType, SIZE); |
// featureType, SIZE); |
274 |
legendsStack.add(image); |
// legendsStack.add(image); |
275 |
} |
// } |
276 |
|
// |
277 |
// this.legendGraphic = |
// // this.legendGraphic = |
278 |
// scaleImage(mergeLegends(legendsStack,applicableRules), request); |
// // scaleImage(mergeLegends(legendsStack,applicableRules), request); |
279 |
this.legendGraphic = mergeLegends(legendsStack, applicableRules); |
// this.legendGraphic = mergeLegends(legendsStack, applicableRules); |
280 |
} |
// } |
281 |
|
// |
282 |
/** |
// /** |
283 |
* Recieves a list of <code>BufferedImages</code> and produces a new one |
// * Recieves a list of <code>BufferedImages</code> and produces a new one |
284 |
* which holds all the images in <code>imageStack</code> one above the |
// * which holds all the images in <code>imageStack</code> one above the |
285 |
* other. |
// * other. |
286 |
* |
// * |
287 |
* @param imageStack |
// * @param imageStack |
288 |
* the list of BufferedImages, one for each applicable Rule |
// * the list of BufferedImages, one for each applicable Rule |
289 |
* @param rules |
// * @param rules |
290 |
* The applicable rules, one for each image in the stack |
// * The applicable rules, one for each image in the stack |
291 |
* @param request |
// * @param request |
292 |
* The request. |
// * The request. |
293 |
* |
// * |
294 |
* @return the stack image with all the images on the argument list. |
// * @return the stack image with all the images on the argument list. |
295 |
* |
// * |
296 |
* @throws IllegalArgumentException |
// * @throws IllegalArgumentException |
297 |
* if the list is empty |
// * if the list is empty |
298 |
*/ |
// */ |
299 |
private static BufferedImage mergeLegends(List<BufferedImage> imageStack, |
// private static BufferedImage mergeLegends(List<BufferedImage> imageStack, |
300 |
Rule[] rules) { |
// Rule[] rules) { |
301 |
if (imageStack.size() == 0) { |
// if (imageStack.size() == 0) { |
302 |
throw new IllegalArgumentException("No legend graphics passed"); |
// throw new IllegalArgumentException("No legend graphics passed"); |
303 |
} |
// } |
304 |
|
// |
305 |
BufferedImage finalLegend = null; |
// BufferedImage finalLegend = null; |
306 |
|
// |
307 |
if (imageStack.size() == 1) { |
// if (imageStack.size() == 1) { |
308 |
finalLegend = (BufferedImage) imageStack.get(0); |
// finalLegend = (BufferedImage) imageStack.get(0); |
309 |
} else { |
// } else { |
310 |
final int imgCount = imageStack.size(); |
// final int imgCount = imageStack.size(); |
311 |
final String[] labels = new String[imgCount]; |
// final String[] labels = new String[imgCount]; |
312 |
|
// |
313 |
BufferedImage img = ((BufferedImage) imageStack.get(0)); |
// BufferedImage img = ((BufferedImage) imageStack.get(0)); |
314 |
FontMetrics fontMetrics = img.getGraphics().getFontMetrics(); |
// FontMetrics fontMetrics = img.getGraphics().getFontMetrics(); |
315 |
|
// |
316 |
final int rowHeight = Math.max(fontMetrics.getHeight(), img |
// final int rowHeight = Math.max(fontMetrics.getHeight(), img |
317 |
.getHeight()); |
// .getHeight()); |
318 |
|
// |
319 |
// calculate the total dimensions of the image |
// // calculate the total dimensions of the image |
320 |
int totalHeight = rowHeight * imgCount; |
// int totalHeight = rowHeight * imgCount; |
321 |
int totalWidth = 0; |
// int totalWidth = 0; |
322 |
|
// |
323 |
for (int i = 0; i < imgCount; i++) { |
// for (int i = 0; i < imgCount; i++) { |
324 |
img = (BufferedImage) imageStack.get(i); |
// img = (BufferedImage) imageStack.get(i); |
325 |
|
// |
326 |
Rule rule = rules[i]; |
// Rule rule = rules[i]; |
327 |
|
// |
328 |
// does this rule have a label |
// // does this rule have a label |
329 |
labels[i] = rule.getTitle(); |
// labels[i] = rule.getTitle(); |
330 |
|
// |
331 |
if (labels[i] == null) { |
// if (labels[i] == null) { |
332 |
labels[i] = rule.getName(); |
// labels[i] = rule.getName(); |
333 |
} |
// } |
334 |
|
// |
335 |
int w = img.getWidth(); |
// int w = img.getWidth(); |
336 |
|
// |
337 |
if (labels[i] != null) { |
// if (labels[i] != null) { |
338 |
Graphics g = img.getGraphics(); |
// Graphics g = img.getGraphics(); |
339 |
w += g.getFontMetrics().stringWidth(labels[i]); |
// w += g.getFontMetrics().stringWidth(labels[i]); |
340 |
} |
// } |
341 |
|
// |
342 |
totalWidth = Math.max(w, totalWidth); |
// totalWidth = Math.max(w, totalWidth); |
343 |
} |
// } |
344 |
|
// |
345 |
// create the final image |
// // create the final image |
346 |
finalLegend = new BufferedImage(totalWidth, totalHeight, |
// finalLegend = new BufferedImage(totalWidth, totalHeight, |
347 |
BufferedImage.TYPE_INT_ARGB); |
// BufferedImage.TYPE_INT_ARGB); |
348 |
|
// |
349 |
Graphics2D finalGraphics = finalLegend.createGraphics(); |
// Graphics2D finalGraphics = finalLegend.createGraphics(); |
350 |
|
// |
351 |
finalGraphics.setColor(Color.white); |
// finalGraphics.setColor(Color.white); |
352 |
finalGraphics.fillRect(0, 0, totalWidth, totalHeight); |
// finalGraphics.fillRect(0, 0, totalWidth, totalHeight); |
353 |
|
// |
354 |
int h = 0; |
// int h = 0; |
355 |
|
// |
356 |
for (int i = 0; i < imgCount; i++) { |
// for (int i = 0; i < imgCount; i++) { |
357 |
img = (BufferedImage) imageStack.get(i); |
// img = (BufferedImage) imageStack.get(i); |
358 |
|
// |
359 |
// draw the image |
// // draw the image |
360 |
int y = h; |
// int y = h; |
361 |
|
// |
362 |
if (img.getHeight() < rowHeight) { |
// if (img.getHeight() < rowHeight) { |
363 |
// move the image to the center of the row |
// // move the image to the center of the row |
364 |
y += (int) ((rowHeight - img.getHeight()) / 2d); |
// y += (int) ((rowHeight - img.getHeight()) / 2d); |
365 |
} |
// } |
366 |
|
// |
367 |
finalGraphics.drawImage(img, 0, y, imgObs); |
// finalGraphics.drawImage(img, 0, y, imgObs); |
368 |
|
// |
369 |
// draw the label |
// // draw the label |
370 |
if (labels[i] != null) { |
// if (labels[i] != null) { |
371 |
finalGraphics.setColor(Color.BLACK); |
// finalGraphics.setColor(Color.BLACK); |
372 |
|
// |
373 |
y = (h + rowHeight) - fontMetrics.getDescent(); |
// y = (h + rowHeight) - fontMetrics.getDescent(); |
374 |
|
// |
375 |
if (fontMetrics.getHeight() < rowHeight) { |
// if (fontMetrics.getHeight() < rowHeight) { |
376 |
// move the baseline to the center of the row |
// // move the baseline to the center of the row |
377 |
y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d); |
// y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d); |
378 |
} |
// } |
379 |
|
// |
380 |
finalGraphics.drawString(labels[i], img.getWidth(), y); |
// finalGraphics.drawString(labels[i], img.getWidth(), y); |
381 |
} |
// } |
382 |
|
// |
383 |
h += rowHeight; |
// h += rowHeight; |
384 |
} |
// } |
385 |
} |
// } |
386 |
|
// |
387 |
return finalLegend; |
// return finalLegend; |
388 |
} |
// } |
389 |
|
// |
390 |
/** |
// /** |
391 |
* DOCUMENT ME! |
// * DOCUMENT ME! |
392 |
* |
// * |
393 |
* @return |
// * @return |
394 |
* |
// * |
395 |
* @throws IllegalStateException |
// * @throws IllegalStateException |
396 |
* DOCUMENT ME! |
// * DOCUMENT ME! |
397 |
*/ |
// */ |
398 |
public BufferedImage getLegendGraphic() { |
// public BufferedImage getLegendGraphic() { |
399 |
if (this.legendGraphic == null) { |
// if (this.legendGraphic == null) { |
400 |
throw new IllegalStateException(); |
// throw new IllegalStateException(); |
401 |
} |
// } |
402 |
return this.legendGraphic; |
// return this.legendGraphic; |
403 |
} |
// } |
404 |
|
|
405 |
// /** |
// /** |
406 |
// * Paints a little rectangle in the color defined by the |
// * Paints a little rectangle in the color defined by the |
448 |
|
|
449 |
/** |
/** |
450 |
* Creates a little BufferedImage that presents the Style/Symbols used by |
* Creates a little BufferedImage that presents the Style/Symbols used by |
451 |
* the {@link MapLegend} to show a legend for the {@link FeatureType} |
* the {@link MapLegend} to show a legend for the {@link SimpleFeatureType} |
452 |
* |
* |
453 |
* @param rule |
* @param rule |
454 |
* {@link Rule} that provides the text and the style to present |
* {@link Rule} that provides the text and the style to present |
455 |
* @param featureType |
* @param featureType |
456 |
* Schema that describes the kind of the sample {@link Feature} |
* Schema that describes the kind of the sample {@link SimpleFeature} |
457 |
* that will be rendered with the {@link Style} |
* that will be rendered with the {@link Style} |
458 |
* @param bg |
* @param bg |
459 |
* Background {@link Color} or <code>null</code> |
* Background {@link Color} or <code>null</code> |
460 |
* |
* |
461 |
* @throws IllegalAttributeException |
* @throws IllegalAttributeException |
462 |
*/ |
*/ |
463 |
public BufferedImage createImageForRule(Rule rule, FeatureType featureType, |
public BufferedImage createImageForRule(Rule rule, SimpleFeatureType featureType, |
464 |
Dimension size, Color bg) { |
Dimension size, Color bg) { |
465 |
|
|
466 |
Symbolizer[] symbolizers = rule.getSymbolizers(); |
Symbolizer[] symbolizers = rule.getSymbolizers(); |
504 |
// RasterSymbolizers.."); |
// RasterSymbolizers.."); |
505 |
// } |
// } |
506 |
// else |
// else |
507 |
final Feature sampleFeature = FeatureUtil.createSampleFeature(featureType); |
final SimpleFeature sampleFeature = FeatureUtil.createSampleFeature(featureType); |
508 |
|
|
509 |
// The SLDStyleFactory has to be recreated, as it seams to cache |
// The SLDStyleFactory has to be recreated, as it seams to cache |
510 |
// some stuff. |
// some stuff. |
559 |
|
|
560 |
/** |
/** |
561 |
* Creates a little BufferedImage that presents the Style/Symbols used by |
* Creates a little BufferedImage that presents the Style/Symbols used by |
562 |
* the {@link MapLegend} to show a legend for the {@link FeatureType} |
* the {@link MapLegend} to show a legend for the {@link SimpleFeatureType} |
563 |
* |
* |
564 |
* @param rule |
* @param rule |
565 |
* {@link Rule} that provides the text and the style to present |
* {@link Rule} that provides the text and the style to present |
566 |
* @param featureType |
* @param featureType |
567 |
* Schema that describes the kind of the sample {@link Feature} |
* Schema that describes the kind of the sample {@link SimpleFeature} |
568 |
* that will be rendered with the {@link Style} |
* that will be rendered with the {@link Style} |
569 |
* |
* |
570 |
* @throws IllegalAttributeException |
* @throws IllegalAttributeException |
571 |
*/ |
*/ |
572 |
public BufferedImage createImageForRule(final Rule rule, |
public BufferedImage createImageForRule(final Rule rule, |
573 |
final FeatureType featureType, final Dimension size) { |
final SimpleFeatureType featureType, final Dimension size) { |
574 |
return createImageForRule(rule, featureType, size, null); |
return createImageForRule(rule, featureType, size, null); |
575 |
} |
} |
576 |
|
|