Ticket #1373 (closed bug: fixed)

Opened 5 years ago

Last modified 5 years ago

Style and Rule should have separate context properties

Reported by: ahocevar Owned by: 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 Download (5.6 KB) - added by ahocevar 5 years ago.
1373-r6361-A0.patch Download (13.4 KB) - added by ahocevar 5 years ago.

Change History

Changed 5 years ago by ahocevar

  Changed 5 years ago by ahocevar

Tests added. All tests pass in FF2 and IE6.

  Changed 5 years ago 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

  Changed 5 years ago 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.

  Changed 5 years ago by ahocevar

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

  Changed 5 years ago 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.

  Changed 5 years ago 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.

  Changed 5 years ago by ahocevar

  • state changed from Needs More Work to Review

Tests pass in IE6 and FF2.

Changed 5 years ago by ahocevar

  Changed 5 years ago 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   Changed 5 years ago 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.

  Changed 5 years ago 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   Changed 5 years ago 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.

Note: See TracTickets for help on using tickets.