Beanshell:Limit labels to one per street

From OpenJUMP Wiki
Jump to: navigation, search

Hi everyone,

I'm trying to show a label (name) of a street that is formed by many segments. When I enable this label, the name is repeated through the entire street (as shown in the attached image).

Is it possible to have only one label for each street? Is there any plugin that perform this fuctionality?

Thanks in advance, Adriana

Hi Adriana: BeanShell to the rescue! The BeanShell script below modifies the Label style to draw each street name once per screen. This is a clever way to meet your requirement.


  • Click View > BeanShell. The BeanShell window will appear.
  • Copy the following code, then paste it into the BeanShell window using Ctrl+V:
{
layerName = "Adriana's Street Layer";
streetNameAttribute = "STREETNAME";
import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.ui.Viewport;
import com.vividsolutions.jump.workbench.ui.renderer.style.LabelStyle;
layer = wc.getLayerManager().getLayer(layerName);
oldLabelStyle = layer.getLabelStyle();
layer.addStyle(new LabelStyle() {
  streetNamesDrawn = new HashSet();
  public void initialize(Layer layer) {
    streetNamesDrawn.clear();
    super.initialize(layer);
  }
  streetName(feature) {
    return feature.getAttribute(streetNameAttribute);
  }
  public void paint(Feature f, Graphics2D g, Viewport viewport) {
    if (streetNamesDrawn.contains(streetName(f))) { return; }
    streetNamesDrawn.add(streetName(f));
    super.paint(f, g, viewport);
  }
});
layer.removeStyle(oldLabelStyle);
layer.getLabelStyle().setEnabled(true);
layer.getLabelStyle().setAttribute(streetNameAttribute);
wc.getLayerViewPanel().repaint();
}


  • Now street names will be drawn once per screen!