OpenLayers OpenLayers

Ticket #1548 (closed feature: fixed)

Opened 7 months ago

Last modified 3 months ago

add support for a context in StyleMap.addUniqueValueRules

Reported by: pgiraud Assigned to:
Priority: minor Milestone: 2.7 Release
Component: StyleMap Version: 2.6
Keywords: Cc:
State: Complete

Description

Currently, it's not possible to add unique value rules for properties that aren't in the feature attributes.

What we would like to have is something like :

var lookup = {
                "Insert": {fillColor: "green"},
                "Delete": {fillColor: "red"}
            }
            
            var context = {
                getState: function(feature) {
                    return feature.state;
                }
            }
            
            styleMap.addUniqueValueRules("default", "${getState}", lookup, context);

I will propose a patch soon if can get to something working.

Attachments

1548_r1790_A0.patch (4.9 kB) - added by pgiraud on 05/16/08 04:06:31.
1548_r1790_A1.patch (5.0 kB) - added by elemoine on 05/17/08 16:26:07.

Change History

05/15/08 11:11:42 changed by ahocevar

Actually, this is not a problem of the StyleMap.addUniqueValueRules method, but of the Rule class in general, because it's context property is static.

An efficient solution would be a modified getContext method in Rule.js:

    getContext: function(feature) {
        var context = this.context;
        if (!context) {
            context = feature.attributes || feature.data;
        }
        if (typeof this.context == "function") {
            context = this.context(feature);
        }
        return context;
    },

With that, the use case could be fulfilled with a slightly modified addUniqueValueRules method in StyleMap.js:

    addUniqueValueRules: function(renderIntent, property, symbolizers,
            context) {
        var rules = [];
        for (var value in symbolizers) {
            rules.push(new OpenLayers.Rule({
                symbolizer: symbolizers[value],
                context: context,
                filter: new OpenLayers.Filter.Comparison({
                    type: OpenLayers.Filter.Comparison.EQUAL_TO,
                    property: property,
                    value: value
                })
            }));
        }
        this.styles[renderIntent].addRules(rules);
    },

To achieve what Pierre wants, his application code would just have to look like the following:

var context = function(feature) {
    return feature;
}

var lookup = {
    "Insert": {fillColor: "green"},
    "Delete": {fillColor: "red"}
}

styleMap.addUniqueValueRules("default", "state", lookup, context);

Although the usage of this Rule context is different than the one of the Style context, this way of doing it feels more intuitive to me.

05/16/08 04:04:12 changed by pgiraud

Andreas, you rock. It also feels more intuitive to me.

Here's a patch including a new test for rule.getContext().

Please review.

Tests pass on FF2. Couldn't really test on IE as tests/Layer/Text.html currently fail.

05/16/08 04:06:31 changed by pgiraud

  • attachment 1548_r1790_A0.patch added.

05/16/08 04:10:25 changed by pgiraud

  • state set to Review.
  • milestone set to 2.7 Release.

05/17/08 14:41:56 changed by ahocevar

  • state changed from Review to Commit.

I can confirm that

  • the tests check for the correct things and are valid
  • the example is easy to understand and shows the developer how to work with the new feature

Thanks for putting this together in a patch with tests and examples Pierre! Please commit.

05/17/08 16:26:07 changed by elemoine

  • attachment 1548_r1790_A1.patch added.

05/17/08 16:28:34 changed by elemoine

1548_r1790_A1.patch brings a little change to pgiraud's patch. The styles-unique.html is modified not to use the strings "Unknown", "Update", "Delete" and "Create" as keys in the lookup object. The constants OpenLayers.State.UNKNOWN and friends must be used instead.

05/19/08 04:28:08 changed by pgiraud

You're right, your changes to my patch are worth. I just didn't find the way to write this down.

05/19/08 09:34:35 changed by pgiraud

  • status changed from new to closed.
  • state changed from Commit to Complete.
  • resolution set to fixed.

(In [7216]) context can now be given as argument in StyleMap.addUniqueValueRules() this, for example, allows user to access to properties that are not part of the feature attributes. r=elemoine (closes #1548)

09/05/08 02:34:07 changed by euzuro

  • component changed from general to StyleMap.