/[schmitzm]/trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java revision 256 by alfonx, Fri Jul 31 14:43:47 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/LegendIconFeatureRenderer.java revision 372 by alfonx, Thu Sep 3 18:41:50 2009 UTC
# Line 45  package skrueger.geotools; Line 45  package skrueger.geotools;
45  import java.awt.Canvas;  import java.awt.Canvas;
46  import java.awt.Color;  import java.awt.Color;
47  import java.awt.Dimension;  import java.awt.Dimension;
 import java.awt.FontMetrics;  
 import java.awt.Graphics;  
48  import java.awt.Graphics2D;  import java.awt.Graphics2D;
49  import java.awt.RenderingHints;  import java.awt.RenderingHints;
50  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
51  import java.awt.image.ImageObserver;  import java.awt.image.ImageObserver;
 import java.util.ArrayList;  
 import java.util.List;  
52    
53  import javax.swing.tree.DefaultTreeCellRenderer;  import javax.swing.tree.DefaultTreeCellRenderer;
54    
55  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
56  import org.geotools.factory.GeoTools;  import org.geotools.factory.GeoTools;
57  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;  
58  import org.geotools.feature.IllegalAttributeException;  import org.geotools.feature.IllegalAttributeException;
59  import org.geotools.geometry.jts.LiteShape2;  import org.geotools.geometry.jts.LiteShape2;
60  import org.geotools.renderer.lite.StyledShapePainter;  import org.geotools.renderer.lite.StyledShapePainter;
# Line 76  import org.geotools.styling.Style; Line 69  import org.geotools.styling.Style;
69  import org.geotools.styling.Symbolizer;  import org.geotools.styling.Symbolizer;
70  import org.geotools.styling.TextSymbolizer;  import org.geotools.styling.TextSymbolizer;
71  import org.geotools.util.NumberRange;  import org.geotools.util.NumberRange;
72    import org.opengis.feature.simple.SimpleFeature;
73    import org.opengis.feature.simple.SimpleFeatureType;
74    import org.opengis.feature.type.AttributeDescriptor;
75    
76  import schmitzm.geotools.feature.FeatureUtil;  import schmitzm.geotools.feature.FeatureUtil;
77    
# Line 92  import com.vividsolutions.jts.geom.Polyg Line 88  import com.vividsolutions.jts.geom.Polyg
88   * <br>   * <br>
89   * <ul>   * <ul>
90   * <li>07.02.2008:<br>   * <li>07.02.2008:<br>
91   * Determining the default values of a {@link FeatureType} by   * Determining the default values of a {@link SimpleFeatureType} by
92   * {@link FeatureUtil#getDefaultAttributeValues(FeatureType)} instead of using   * {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of using
93   * {@link AttributeType#createDefaultValue()} directly, because the latter   * {@link AttributeDescriptor#createDefaultValue()} directly, because the latter
94   * returns {@code null} even though the attribut is not nillable.</li>   * returns {@code null} even though the attribut is not nillable.</li>
95   * </ul>   * </ul>
96   *   *
# Line 243  public class LegendIconFeatureRenderer e Line 239  public class LegendIconFeatureRenderer e
239    
240                  return sampleShape;                  return sampleShape;
241          }          }
242    //
243          /**  //      /**
244           * Puts a BufferedImage into this.legendGraphic  //       * Puts a BufferedImage into this.legendGraphic
245           */  //       */
246          public void produceLegendGraphic(FeatureType featureType, Style gt2Style,  //      public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,
247                          Rule[] applicableRules) {  //                      Rule[] applicableRules) {
248    //
249                  // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();  //              // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();
250    //
251                  // if (request.getRule() != null) {  //              // if (request.getRule() != null) {
252                  // applicableRules = new Rule[] { request.getRule() };  //              // applicableRules = new Rule[] { request.getRule() };
253                  // } else {  //              // } else {
254                  // applicableRules = getApplicableRules(ftStyles, scaleDenominator);  //              // applicableRules = getApplicableRules(ftStyles, scaleDenominator);
255                  // }  //              // }
256    //
257                  final int ruleCount = applicableRules.length;  //              final int ruleCount = applicableRules.length;
258    //
259                  /**  //              /**
260                   * A legend graphic is produced for each applicable rule. They're being  //               * A legend graphic is produced for each applicable rule. They're being
261                   * 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"
262                   * like legend.  //               * like legend.
263                   */  //               */
264                  final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(  //              final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(
265                                  ruleCount);  //                              ruleCount);
266    //
267                  for (int i = 0; i < ruleCount; i++) {  //              for (int i = 0; i < ruleCount; i++) {
268                          BufferedImage image = createImageForRule(applicableRules[i],  //                      BufferedImage image = createImageForRule(applicableRules[i],
269                                          featureType, SIZE);  //                                      featureType, SIZE);
270                          legendsStack.add(image);  //                      legendsStack.add(image);
271                  }  //              }
272    //
273                  // this.legendGraphic =  //              // this.legendGraphic =
274                  // scaleImage(mergeLegends(legendsStack,applicableRules), request);  //              // scaleImage(mergeLegends(legendsStack,applicableRules), request);
275                  this.legendGraphic = mergeLegends(legendsStack, applicableRules);  //              this.legendGraphic = mergeLegends(legendsStack, applicableRules);
276          }  //      }
277    //
278          /**  //      /**
279           * Recieves a list of <code>BufferedImages</code> and produces a new one  //       * Recieves a list of <code>BufferedImages</code> and produces a new one
280           * which holds all the images in <code>imageStack</code> one above the  //       * which holds all the images in <code>imageStack</code> one above the
281           * other.  //       * other.
282           *  //       *
283           * @param imageStack  //       * @param imageStack
284           *            the list of BufferedImages, one for each applicable Rule  //       *            the list of BufferedImages, one for each applicable Rule
285           * @param rules  //       * @param rules
286           *            The applicable rules, one for each image in the stack  //       *            The applicable rules, one for each image in the stack
287           * @param request  //       * @param request
288           *            The request.  //       *            The request.
289           *  //       *
290           * @return the stack image with all the images on the argument list.  //       * @return the stack image with all the images on the argument list.
291           *  //       *
292           * @throws IllegalArgumentException  //       * @throws IllegalArgumentException
293           *             if the list is empty  //       *             if the list is empty
294           */  //       */
295          private static BufferedImage mergeLegends(List<BufferedImage> imageStack,  //      private static BufferedImage mergeLegends(List<BufferedImage> imageStack,
296                          Rule[] rules) {  //                      Rule[] rules) {
297                  if (imageStack.size() == 0) {  //              if (imageStack.size() == 0) {
298                          throw new IllegalArgumentException("No legend graphics passed");  //                      throw new IllegalArgumentException("No legend graphics passed");
299                  }  //              }
300    //
301                  BufferedImage finalLegend = null;  //              BufferedImage finalLegend = null;
302    //
303                  if (imageStack.size() == 1) {  //              if (imageStack.size() == 1) {
304                          finalLegend = (BufferedImage) imageStack.get(0);  //                      finalLegend = (BufferedImage) imageStack.get(0);
305                  } else {  //              } else {
306                          final int imgCount = imageStack.size();  //                      final int imgCount = imageStack.size();
307                          final String[] labels = new String[imgCount];  //                      final String[] labels = new String[imgCount];
308    //
309                          BufferedImage img = ((BufferedImage) imageStack.get(0));  //                      BufferedImage img = ((BufferedImage) imageStack.get(0));
310                          FontMetrics fontMetrics = img.getGraphics().getFontMetrics();  //                      FontMetrics fontMetrics = img.getGraphics().getFontMetrics();
311    //
312                          final int rowHeight = Math.max(fontMetrics.getHeight(), img  //                      final int rowHeight = Math.max(fontMetrics.getHeight(), img
313                                          .getHeight());  //                                      .getHeight());
314    //
315                          // calculate the total dimensions of the image  //                      // calculate the total dimensions of the image
316                          int totalHeight = rowHeight * imgCount;  //                      int totalHeight = rowHeight * imgCount;
317                          int totalWidth = 0;  //                      int totalWidth = 0;
318    //
319                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
320                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
321    //
322                                  Rule rule = rules[i];  //                              Rule rule = rules[i];
323    //
324                                  // does this rule have a label  //                              // does this rule have a label
325                                  labels[i] = rule.getTitle();  //                              labels[i] = rule.getTitle();
326    //
327                                  if (labels[i] == null) {  //                              if (labels[i] == null) {
328                                          labels[i] = rule.getName();  //                                      labels[i] = rule.getName();
329                                  }  //                              }
330    //
331                                  int w = img.getWidth();  //                              int w = img.getWidth();
332    //
333                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
334                                          Graphics g = img.getGraphics();  //                                      Graphics g = img.getGraphics();
335                                          w += g.getFontMetrics().stringWidth(labels[i]);  //                                      w += g.getFontMetrics().stringWidth(labels[i]);
336                                  }  //                              }
337    //
338                                  totalWidth = Math.max(w, totalWidth);  //                              totalWidth = Math.max(w, totalWidth);
339                          }  //                      }
340    //
341                          // create the final image  //                      // create the final image
342                          finalLegend = new BufferedImage(totalWidth, totalHeight,  //                      finalLegend = new BufferedImage(totalWidth, totalHeight,
343                                          BufferedImage.TYPE_INT_ARGB);  //                                      BufferedImage.TYPE_INT_ARGB);
344    //
345                          Graphics2D finalGraphics = finalLegend.createGraphics();  //                      Graphics2D finalGraphics = finalLegend.createGraphics();
346    //
347                          finalGraphics.setColor(Color.white);  //                      finalGraphics.setColor(Color.white);
348                          finalGraphics.fillRect(0, 0, totalWidth, totalHeight);  //                      finalGraphics.fillRect(0, 0, totalWidth, totalHeight);
349    //
350                          int h = 0;  //                      int h = 0;
351    //
352                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
353                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
354    //
355                                  // draw the image  //                              // draw the image
356                                  int y = h;  //                              int y = h;
357    //
358                                  if (img.getHeight() < rowHeight) {  //                              if (img.getHeight() < rowHeight) {
359                                          // move the image to the center of the row  //                                      // move the image to the center of the row
360                                          y += (int) ((rowHeight - img.getHeight()) / 2d);  //                                      y += (int) ((rowHeight - img.getHeight()) / 2d);
361                                  }  //                              }
362    //
363                                  finalGraphics.drawImage(img, 0, y, imgObs);  //                              finalGraphics.drawImage(img, 0, y, imgObs);
364    //
365                                  // draw the label  //                              // draw the label
366                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
367                                          finalGraphics.setColor(Color.BLACK);  //                                      finalGraphics.setColor(Color.BLACK);
368    //
369                                          y = (h + rowHeight) - fontMetrics.getDescent();  //                                      y = (h + rowHeight) - fontMetrics.getDescent();
370    //
371                                          if (fontMetrics.getHeight() < rowHeight) {  //                                      if (fontMetrics.getHeight() < rowHeight) {
372                                                  // move the baseline to the center of the row  //                                              // move the baseline to the center of the row
373                                                  y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);  //                                              y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);
374                                          }  //                                      }
375    //
376                                          finalGraphics.drawString(labels[i], img.getWidth(), y);  //                                      finalGraphics.drawString(labels[i], img.getWidth(), y);
377                                  }  //                              }
378    //
379                                  h += rowHeight;  //                              h += rowHeight;
380                          }  //                      }
381                  }  //              }
382    //
383                  return finalLegend;  //              return finalLegend;
384          }  //      }
385    //
386          /**  //      /**
387           * DOCUMENT ME!  //       * DOCUMENT ME!
388           *  //       *
389           * @return  //       * @return
390           *  //       *
391           * @throws IllegalStateException  //       * @throws IllegalStateException
392           *             DOCUMENT ME!  //       *             DOCUMENT ME!
393           */  //       */
394          public BufferedImage getLegendGraphic() {  //      public BufferedImage getLegendGraphic() {
395                  if (this.legendGraphic == null) {  //              if (this.legendGraphic == null) {
396                          throw new IllegalStateException();  //                      throw new IllegalStateException();
397                  }  //              }
398                  return this.legendGraphic;  //              return this.legendGraphic;
399          }  //      }
400    
401          // /**          // /**
402          // * Paints a little rectangle in the color defined by the          // * Paints a little rectangle in the color defined by the
# Line 448  public class LegendIconFeatureRenderer e Line 444  public class LegendIconFeatureRenderer e
444    
445          /**          /**
446           * Creates a little BufferedImage that presents the Style/Symbols used by           * Creates a little BufferedImage that presents the Style/Symbols used by
447           * the {@link MapLegend} to show a legend for the {@link FeatureType}           * the {@link MapLegend} to show a legend for the {@link SimpleFeatureType}
448           *           *
449           * @param rule           * @param rule
450           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
451           * @param featureType           * @param featureType
452           *            Schema that describes the kind of the sample {@link Feature}           *            Schema that describes the kind of the sample {@link SimpleFeature}
453           *            that will be rendered with the {@link Style}           *            that will be rendered with the {@link Style}
454           * @param bg           * @param bg
455           *            Background {@link Color} or <code>null</code>           *            Background {@link Color} or <code>null</code>
456           *           *
457           * @throws IllegalAttributeException           * @throws IllegalAttributeException
458           */           */
459          public BufferedImage createImageForRule(Rule rule, FeatureType featureType,          public BufferedImage createImageForRule(Rule rule, SimpleFeatureType featureType,
460                          Dimension size, Color bg) {                          Dimension size, Color bg) {
461    
462                  Symbolizer[] symbolizers = rule.getSymbolizers();                  Symbolizer[] symbolizers = rule.getSymbolizers();
# Line 504  public class LegendIconFeatureRenderer e Line 500  public class LegendIconFeatureRenderer e
500                                  // RasterSymbolizers..");                                  // RasterSymbolizers..");
501                                  // }                                  // }
502                                  // else                                  // else
503                                  final Feature sampleFeature = FeatureUtil.createSampleFeature(featureType);                                  final SimpleFeature sampleFeature = FeatureUtil.createSampleFeature(featureType);
504    
505                                  // The SLDStyleFactory has to be recreated, as it seams to cache                                  // The SLDStyleFactory has to be recreated, as it seams to cache
506                                  // some stuff.                                  // some stuff.
# Line 559  public class LegendIconFeatureRenderer e Line 555  public class LegendIconFeatureRenderer e
555    
556          /**          /**
557           * Creates a little BufferedImage that presents the Style/Symbols used by           * Creates a little BufferedImage that presents the Style/Symbols used by
558           * the {@link MapLegend} to show a legend for the {@link FeatureType}           * the {@link MapLegend} to show a legend for the {@link SimpleFeatureType}
559           *           *
560           * @param rule           * @param rule
561           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
562           * @param featureType           * @param featureType
563           *            Schema that describes the kind of the sample {@link Feature}           *            Schema that describes the kind of the sample {@link SimpleFeature}
564           *            that will be rendered with the {@link Style}           *            that will be rendered with the {@link Style}
565           *           *
566           * @throws IllegalAttributeException           * @throws IllegalAttributeException
567           */           */
568          public BufferedImage createImageForRule(final Rule rule,          public BufferedImage createImageForRule(final Rule rule,
569                          final FeatureType featureType, final Dimension size) {                          final SimpleFeatureType featureType, final Dimension size) {
570                  return createImageForRule(rule, featureType, size, null);                  return createImageForRule(rule, featureType, size, null);
571          }          }
572    

Legend:
Removed from v.256  
changed lines
  Added in v.372

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26