[OpenLayers-Users] addUniqueValueRules causes feature to disappear instead of changing it's style

Christian Benke benkokakao at gmail.com
Fri Aug 10 06:00:06 PDT 2012


Alexandre, thanks for your quick answer! Since you where pointing out
that the rule uses feature.attributes, i was able to apply the rule by
using a json-property instead of id, an example is at the end of the
mail.

However, i'm interested to get the filter-example working as well. I
find it difficult to understand how the FeatureId-Class is supposed to
work. I'm trying to apply this rule:

 var rulePoint = new OpenLayers.Rule({
                filter: new OpenLayers.Filter.FeatureId({
                        type: OpenLayers.Filter.Function,
                        fids: ['999'],
                        evaluate:function(fid) {
                            console.log(feature.fid);
                            var x=0;
                            return true;}

                        }),
                    symbolizer: {pointRadius: 20, fillColor: "red",
                    fillOpacity: 0.7, strokeColor: "black"}
                });

This is how i understand it works(Please correct me, i'm sure i'm wrong):
When a feature is added to the vector_layer, the styleMap is applied
to the feature and the feature is matched against the rule
"rulePoint". "rulePoint" is defined with a filter by FeatureId. This
filter matches against an array of values and returns True if there's
a match. If the filter returns true, the rule applies the style given
in symbolizer to the feature. What's the point of the
evaluate-function?
The whole filter doesn't seem to be called though, as the
evaluate-function doesn't return anything when i select a feature. Do
i miss anything else but "addRules" to make the rule/filter work?


#### Working Rule with addUniqueValueRules and feature.attributes:

Thanks for pointing out the issue with feature.attributes for
addUniqueValueRules! Now i understand the the feature.attributes are
the values in the json-properties, which i can use as the property in
addUniqueValueRules(e.g. "type"). Here's a example so this thread
includes the solution in case someone stumbles over this problem too:

var Point1 = {                      "type": "Feature",
                                    "geometry": {
                                    "type": "Point",
                                    "coordinates": [3.0855166,45.7798048] },
                                    "properties": {
                                             "name": "Point 001",
                                             "style": {
                                                 color: "#004070",
                                                 weight: 4,
                                                 opacity: 0.9
                                             },
                                            "type": "imagemarker",
                                             "popupContent": "Date
2012-06-26 18:06:37, Track 001, Distance 26.793km, Total time was
9:52:26h"
                                         },
                                     };

var lookup = {
            "imagemarker": {strokeColor: 'green', fillColor: "red",
strokeWidth:10},
            "line": {strokeColor: 'blue'}
        };

styleMap.addUniqueValueRules('select', 'type', lookup);

Best regards,
Christian


On 9 August 2012 19:54, Alexandre Dubé <adube at mapgears.com> wrote:
>
> Hi Christian,
>
>   I would seem that even thought you send the feature itself as context,
> it will ends up using the feature.attributes hash anyway.  You can see for
> yourself by loading an uncompressed version of OpenLayers, put a breakpoint
> inside your context method until you get to that line :
>
>     OpenLayers/Filter/Comparison.js :
>
>     evaluate: function(context) {
>         if (context instanceof OpenLayers.Feature.Vector) {
>             context = context.attributes;
>         }
>     ...
>
>   So, because 'fid' is not one of the attributes, the evaluation fails and
> no style is applied.
>
>   In order to evaluate on the feature fid, you can use the
> OpenLayers.Filter.FeatureId instead and create your own rules.  If you're
> not familiar with filters, you can take a look at this example:
>     http://openlayers.org/dev/examples/filter.html
>
>   Once you get that filter working, you'll see your point turn green when
> selected.
>
>   Lastly, when you use rules, you need to cover all possibilities for all
> features to render.  If you only specify one rule with "property equal
> something", all features that evaluates the rule correctly will render, but
> not the others.  A quick way to make sure the other at least render, you can
> specify an elseFilter :
>
>     styleMap.styles["select"].addRules([new OpenLayers.Rule({
>         symbolizer: {strokeColor:"black",strokeWidth: 10},
>         elseFilter: true
>     })]);
>
>   Try that and see how it goes.
>
> HTH,
>
> Alexandre
>
>
>
> On 12-08-09 12:46 PM, Christian Benke wrote:
>
> Hello!
>
> I'm having difficulties to understand how addUniqueValueRules is applied.
>
> http://poab.org/openlayers/stylerule.html fetches data from a GeoJSON-file
> and draws a point and a line according to the styling in styleMap.
>
> My goal is to have the Point and the Line drawn in different colors. To
> achieve this goal i try to add a "Unique Value Rule" with the property set
> to "fid" and a symbolizer matching the value "999", which should draw the
> strokeColor of the point as green when selected.
>
> However, the Point simply disappears when selected.
>
> I've been banging my head against this problem for hours, i just don't see
> why the rule doesn't seem to match any attribute i try to feed it, while the
> attribute exists in the vector-object i'm matching it against.
> I also don't understand why the feature i'm selecting actually disappears
> - if the rule does not match, the selected feature should actually not be
> changed?!
>
> How can i debug the addUniqueValueRules-function?
>
> I'm not asking to solve the issue for me, but if anyone could take a look
> at my example and help me understand where i'm misunderstanding the concept,
> it would help me sleep better tonight :-)
>
> Best regards,
> Christian
>
>
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
>
>
> --
> Alexandre Dubé
> Mapgears
> www.mapgears.com
>
>
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>


More information about the Users mailing list