Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, February 20, 2005

Using BeanShell to limit labels to one per street

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.

1. Click View > BeanShell. The BeanShell window will appear.
2. 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();
}

3. Now street names will be drawn once per screen!

0 Comments:

Post a Comment

<< Home