0

I have a polygon dataset that I am symbolizing in OpenLayers using a context to determine the correct color, and also a label for each polygon that I want to alter depending on the zoom level.

But although my polygons get colored as they should, my labels don't change. If I test this with a point feature and no symbolization, my labels do change the way I want. So I guess this is something to do with the way rules and context are applied? How can I fix this so that the labels change size with the rules?

var ratingStyle = new OpenLayers.StyleMap({
        "default": new OpenLayers.Style({
            fillColor: "${getColor}",
            strokeColor: "#000000",
            fillOpacity: 0.7,
            graphicZIndex: 1,
            label: "${ADMINUNIT}",
            labelAlign: 'cm',
            labelOutlineColor: "white",
            labelOutlineWidth: 3,
            fontSize: 12,
            fontOpacity: 1
        }, {
            context: { //this is for the polygon color
                getColor: function (feature) {
                    return feature.attributes.RATING == 'NotRated' ? 'transparent' :
                    feature.attributes.RATING == 'Low' ? 'green' :
                    feature.attributes.RATING == 'Moderate' ? 'yellow' :
                    feature.attributes.RATING == 'High' ? 'red' :
                    feature.attributes.RATING == 'Extreme' ? 'black' :
                    'grey';
                }
            }
        }, { //rules for labels?
            rules: [
                new OpenLayers.Rule({
                    minScaleDenominator: 50000000,
                    symbolizer: {
                        fontSize: "0px"
                    }
                }),
                new OpenLayers.Rule({
                    maxScaleDenominator: 50000000,
                    minScaleDenominator: 20000000,
                    symbolizer: {
                        fontSize: "10px"
                    }
                }),
                new OpenLayers.Rule({
                    maxScaleDenominator: 20000000,
                    symbolizer: {
                        fontSize: "12px"
                    }
                })
            ]
        }),
        "select": new OpenLayers.Style({
            fillOpacity: 1,
            graphicZIndex: 1
        })
    }, {
        extendDefault: true
    });
0

Have you tried to realize the scale-dependent font size with a second context-function? You can get the scale with map.getScale()

1
  • This was the way to go, stacking up contexts. – spg Jan 28 '15 at 22:06
0

For reference:

    fillColor: "${getColor}",
    fontSize: "${fontSizer}",
 }, { context: {
        getColor : function (feature) {
            return feature.attributes.RATING == 'NotRated' ? 'transparent':
                   feature.attributes.RATING == 'Low' ? 'green':
                   feature.attributes.RATING == 'Moderate' ? 'yellow':
                   feature.attributes.RATING == 'High' ? 'red':
                   feature.attributes.RATING == 'Extreme' ? 'black':
                   'grey';
        },
        fontSizer : function () {
            if (label_bool) { //labels on
                scale = map.getScale();
                if (scale > 30000000) {
                    return "0px";
                } else if (scale > 8000000) {
                    return "8px";
                } else {
                    return "12px";
                }
            } else { //labels off
                return "0px";
            }
        }
    } 

Also, I didn't realize this was on the GIS Stack Exchange instead of the main one :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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