1

I've taken the Geotools example for showing map data. There is a function setting a color (yellow) for selected items. The function sets the rest of the items to a default color even though the layer has another defined color. I would like to have the color unchanged for the not-selected items, but the yellow color for the selected items. How can I change the code to keep the original color setting while setting yellow for the selected items? See example:

public void displaySelectedFeatures(Set<FeatureId> IDs) {
    Style style;

    if (IDs.isEmpty()) {
        style = createDefaultStyle();

    } else {
        style = createSelectedStyle(IDs);
    }

    Layer layer = mapFrame.getMapContent().layers().get(0);
    ((FeatureLayer) layer).setStyle(style);
    mapFrame.getMapPane().repaint();
}
private Style createDefaultStyle() {
    Rule rule = createRule(LINE_COLOUR, FILL_COLOUR);

    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(rule);

    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
}

private Style createSelectedStyle(Set<FeatureId> IDs) {
    Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
    selectedRule.setFilter(ff.id(IDs));
    Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
    otherRule.setElseFilter(true);
    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(selectedRule);
    fts.rules().add(otherRule);
    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
}
0

1 Answer 1

1

The code as shown in your question should work fine, unless you are changing the LINE_COLOR and FILL_COLOR variables between calls. But you could store the default rule in a field and then reuse it as the else rule in the selection. So something like this should work:

Rule rule = createRule(LINE_COLOUR, FILL_COLOUR);
private Style createDefaultStyle() {
    rule.setElseFilter(false); // it's the only rule here
    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(rule);

    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
}

private Style createSelectedStyle(Set<FeatureId> IDs) {
    Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
    selectedRule.setFilter(ff.id(IDs));

    rule.setElseFilter(true); // only match the non-selected features
    FeatureTypeStyle fts = sf.createFeatureTypeStyle();
    fts.rules().add(selectedRule);
    fts.rules().add(otherRule);
    Style style = sf.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
}
2
  • Do I understand you right: I should add a field in the featureSource with the rule?
    – Paul
    Commented Jun 8, 2021 at 13:35
  • provided that you use the same rule in both styles the unselected elements will look the same (which I think your existing code should do, so may be you can edit your question to make it clearer)
    – Ian Turton
    Commented Jun 8, 2021 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.