Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Loading polygons from geoserver via OpenLayers using GeoJSON. My plan is to symbolize using attributes in the GeoJSON. I am struggling to access the attributes. Any insight appreciated:

The geometries are drawing, so I know that the GeoJSON is being passed from the backend to the client.

Wrapped in an init() function and called on page load:

        map = new OpenLayers.Map('map', options);

        var protocol = new OpenLayers.Protocol.WFS({ 
        url: "http://osgeostack:8080/geoserver/fjtests/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=fjtests:counties", 
        featureNS: "fj_tests", 
        featureType: "counties", 
        outputFormat: 'json', 
        readFormat: new OpenLayers.Format.GeoJSON() 

        layer = new OpenLayers.Layer.Vector("GML", {
            isBaseLayer: true,
            //styleMap: new OpenLayers.StyleMap(style),
            protocol: protocol,
            strategies: [new OpenLayers.Strategy.Fixed()]
            alert(layer.attributes) \\undefined
            alert(layer.features.length) \\ 0
share|improve this question
up vote 1 down vote accepted

You layer.attributes is undefined because attributes are on features (this means that among other things a features in a layer may have different attributes).

To style your map the simplest way would be to create a Style for your layer which contains OpenLayers Rules. Take a look at the OpenLayers Rule Based Style for a complete example, but I've written a simple one below that you could use for a polygon set (I'm assuming that's what "counties" will be). Note that the first argument of a Style is the default Symbolizer object.

var style = new OpenLayers.Style(
        strokeColor: '#000000', //hex colour, in this case black
        strokeOpacity: 1,
        strokeWidth: 1,
        strokeLinecap: "butt", //or "round" or "square"
        fillColor: "#ffffff",
        fillOpacity: 0.3
    // sample rule
        rules: [
            new OpenLayers.Rule({
                filter: new OpenLayers.Filter.Comparison({
                    type: OpenLayers.Filter.Comparison.EQUAL_TO,
                    property: "name", // geojson property
                    value: "Unknown" //property value
                symbolizer: { //Colour Unknown counties red
                    strokeColor: '#ff0000',
                    strokeOpacity: 1,
                    strokeWidth: 1,
                    strokeLinecap: "butt",
                    fillColor: "#ff0000",
                    fillOpacity: 1,
                    label: "${name}" //Label based on name property
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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