[Featureserver] OpenLayers Rules and PostGIS Columns from FeatureServer

Tom A. Cox tomc at hot.rr.com
Wed Aug 26 14:06:03 EDT 2009


I had a similar problem.  Most of my features have the same style, but some 
needed a different style, both based on a status field.  In addition, I let 
the user change the status interactively, but I don't update the database 
immediately.  I'm fairly new at this, so there may be a better way, but this 
is what I have right now.

// Use a styleMap for drawing most features.  Individual features
// do not have their own style unless they have a non-default status.
wfs_frontload = new OpenLayers.Layer.WFS(
    "Frontload",
    "/cgi-bin/featureserver.cgi/frontload?format=WFS",
    {maxfeatures: max_features},
    {extractAttributes: true, displayInLayerSwitcher: true,
        visibility: false, styleMap: map_styles,
        projection: new OpenLayers.Projection("EPSG:4326")}
    );

// A function called for every feature inserted.
wfs_frontload.preFeatureInsert = container_preFeatureInsert;

function container_preFeatureInsert(feature) {
    container_set_style(feature, "status");
}

// Set a feature Style to based on the 'status' field or one of the
// predetermined styles.  This function is called both internally
// and by the layer's preFeatureInsert callback.
function container_set_style(feature, which) {
    if (feature == null)
        return;

    // Set the style based on status.  This covers 99% of cases, so
    // we check status first.
    if (which == "status") {
        var style = null;
        var default_style = map_styles.styles['default'].defaultStyle;

        switch(feature.data['status']) {
            case "0":
                if (feature.style != null)
                    // Revert to layer's default style.
                    delete feature.style;
                break;
            case "1":
                // New customer
                if (feature.style)
                    style = feature.style;
                else
                    style = OpenLayers.Util.extend({}, default_style);
                style.fillColor = psw_new;
                style.strokeColor = psw_new;
                break;
             ... more status types
        }

        if (style != null) {
            // If a feature gets a style, the label must be explicitly
            // set.
            if (map.getZoom() < labelZoom)
                style.label = "";
            else
                style.label =
                    feature.data['street_num'] + "/" +
                    feature.data['service_ref'] + "-" +
                    feature.data['service_num'] + "/" +
                    feature.data['serial_number'];

            feature.style = style;
        }

        return;
    }

    // If which is null, default style is in effect.
    if (which == null || which == "default") {
        if (feature.style != null)
            // Revert to default style
            delete feature.style;
        return;
    }

    if (which == "select") {
        feature.style =
            map_styles.styles['select'].defaultStyle;
        return;
    }

    if (which == "temp") {
        feature.style =
            map_styles.styles['temp'].defaultStyle;
        return;
    }

}

-- 
Tom A. Cox
IntelliSystems
Systems Analyst and Consultant
Office: 254-848-5598  Email: Tom Cox <tomc at hot.rr.com>



More information about the Featureserver mailing list