Example #1
0
 /**
  * Adds a <tt>Theme</tt> that the <tt>Optimizer</tt> should consider.
  *
  * @param theme
  */
 public void addTheme(Theme theme) {
   if (!themes.contains(theme)) {
     ArrayList themeElements = theme.getDisplayElements();
     for (int i = 0; i < themeElements.size(); i++) {
       Object o = themeElements.get(i);
       if (o instanceof LabelDisplayElement) {
         LabelDisplayElement element = (LabelDisplayElement) o;
         TextSymbolizer symbolizer = (TextSymbolizer) element.getSymbolizer();
         // only add element if "auto" is set
         if (symbolizer.getLabelPlacement() != null) {
           if (symbolizer.getLabelPlacement().getPointPlacement() != null
               && symbolizer.getLabelPlacement().getPointPlacement().isAuto()) {
             displayElements.add(o);
             //					} else if (symbolizer.getLabelPlacement().getLinePlacement() != null) {
             //						displayElements.add (o);
           }
         }
       }
     }
     themes.add(theme);
   }
 }
Example #2
0
  /**
   * Finds optimized <tt>Label</tt> representations for the registered
   * <tt>LabelDisplayElement</tt>s.
   *
   * <p>
   *
   * @param g
   */
  public void optimize(Graphics2D g) throws Exception {

    choices.clear();
    double scale = mapView.getScale(g);
    GeoTransform projection = mapView.getProjection();

    // used to signal the LabelDisplayElement that it should
    // not create Labels itself (in any case)
    Label[] dummyLabels = new Label[0];

    // collect LabelChoices for all LabelDisplayElements
    for (int i = 0; i < displayElements.size(); i++) {

      LabelDisplayElement element = (LabelDisplayElement) displayElements.get(i);
      if (!element.doesScaleConstraintApply(scale)) continue;

      element.setLabels(dummyLabels);
      choices.addAll(LabelChoiceFactory.createLabelChoices(element, g, projection));
    }

    buildCollisionMatrix();

    // do the magic
    try {
      anneal();
    } catch (Exception e) {
      e.printStackTrace();
    }

    // sets the optimized labels to the LabelDisplayElements, so
    // they are considered when the DisplayElements are painted the next
    // time

    for (int i = 0; i < choices.size(); i++) {
      LabelChoice choice = (LabelChoice) choices.get(i);
      choice.getElement().addLabel(choice.getSelectedLabel());
    }
  }