OpenLayers OpenLayers

Ticket #1373 (closed bug: fixed)

Opened 10 months ago

Last modified 9 months ago

Style and Rule should have separate context properties

Reported by: ahocevar Assigned to: ahocevar
Priority: minor Milestone: 2.6 Release
Component: Style Version: 2.5
Keywords: Cc:
State: Complete

Description

The Rule.context property was intended to allow for evaluating a rule against a different object than the attributes of the feature about to be rendered. But it might be even more useful to define a Style.context to pull property values from.

Example: I have features with an attribute. let's say type. I do not want to do complex rule-based styling, I just want to have a lookup table for the type to choose an externalGraphic:

layer.features[0].attributes = {
    type: "scenic"
}
var lookup = {
    "recreation": "rec01.png",
    "scenic": "icon37.png"
}
var pointSymbolizer = {
    externalGraphic: "${type}"
}
var style = new OpenLayers.Style(pointSymbolizer, {context: lookup});
layer.styleMap = new OpenLayers.StyleMap(style);

This would be a first step towards convenience of complex styling.

Attachments

styleContext.patch (5.6 kB) - added by ahocevar on 02/19/08 09:44:51.
1373-r6361-A0.patch (13.4 kB) - added by ahocevar on 02/25/08 05:17:45.

Change History

02/19/08 09:44:51 changed by ahocevar

  • attachment styleContext.patch added.

02/19/08 09:45:22 changed by ahocevar

Tests added. All tests pass in FF2 and IE6.

02/20/08 18:24:26 changed by ahocevar

  • type changed from feature to bug.

changed to bug because the way it is now is unintuitive and not what the user expects

02/24/08 15:29:43 changed by tschaub

  • state changed from Review to Needs More Work.

Andreas, this looks great - and looks like a good step towards easier usage. It's not immediately clear from the patch that the example you give above works. I can spend more time confirming this for myself, but it would be nice to have an example that demonstrates this usage. People are going to be coming in with many questions about the new style stuff. A very simple example (without popups or other distractions) would be nice.

I don't think that should block this from getting in 2.6, but I'm changing the state in hopes that you (or someone else) can cook up a good example.

02/24/08 15:32:21 changed by ahocevar

Tim, thanks for looking into it. I am starting work on a simple example right now.

02/24/08 18:31:19 changed by ahocevar

Tim, you are absolutely right: the example above does not work with the patch. The context thing is good, but it does not help us in this case. I will commit a new patch as soon as I have one.

02/24/08 19:27:27 changed by ahocevar

1373-r6361-A0.patch adds a few important things:

  • a new convenience method addUniqueValueRules to OL.StyleMap. This one can actually be used to achieve what I was trying to show in the example of this ticket's description. The original patch is not appropriate for that.
  • some refactoring of OpenLayers.Style to remove duplicate code (with tests).
  • a new example showing how to add a "unique value" legend to a point layer using the new addUniqueValueRules method.

02/24/08 19:56:45 changed by ahocevar

  • state changed from Needs More Work to Review.

Tests pass in IE6 and FF2.

02/25/08 05:17:45 changed by ahocevar

  • attachment 1373-r6361-A0.patch added.

02/25/08 05:19:38 changed by ahocevar

Forgot to mention one thing introduced during the refactoring of Style.js: Rule.symbolizer can now also be just a symbolizer, instead of a hash of symbolizers keyed by "Point", "Line", "Polygon". This will make things even simpler (as can be seen in the styles-unique.html example).

(follow-up: ↓ 11 ) 02/28/08 11:44:15 changed by tschaub

  • state changed from Review to Commit.

Nice! Now this is really getting useful. I look forward to more of this (easy support for graduated symbolizers/class breaks).

I think this is good to go. Only minor comments are:

  • The typeof == "object" check is a bit sketchy. I know it's got to be fast, but OL.Util.indexOf(OpenLayers.Style.SYMBOLIZER_PREFIXES, key) would be more explicit (~line 229 Style.js). typeof null == "object" and the default symbolizer is null so the check is correct in this context. No idea if a symbolizer value will ever be set to null by anyone, but the check would be incorrect in this context. No need to change anything now though.
  • The second arg for addPropertyStyles is a symbolizer, right? Just trying to keep the language straight. At some point it will be nice to narrow down our vocabulary.

Thanks for putting this together Andreas. Please commit.

02/28/08 12:57:37 changed by ahocevar

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

(In [6396]) * Style and Rule now have separate context properties

  • new convenience method addUniqueValueRules in OL.StyleMap. This can actually be used to achieve what I was trying to show in the example of this ticket's description.
  • some refactoring of OL.Style to remove duplicate code (with tests)
  • a new example showing how to add a "unique value" legend to a point layer using the new addUniqueValueRules method
  • Rule.symbolizer can now also be just a symbolizer, instead of a hash of symbolizers keyed by "Point", "Line", "Polygon". This will make things even simpler (as can be seen in the styles-unique.html example)

r=tschaub (closes #1373)

(in reply to: ↑ 9 ) 02/28/08 13:00:27 changed by ahocevar

Replying to tschaub:

Nice! Now this is really getting useful. I look forward to more of this (easy support for graduated symbolizers/class breaks).

Yes, that will be next :-).

* The second arg for addPropertyStyles is a symbolizer, right? Just trying to keep the language straight. At some point it will be nice to narrow down our vocabulary.

You are absolutely right. Renamed to "symbolizer" before committing.