OpenLayers – How to Edit Using a WFS Service Without Displaying on Map

editingopenlayers-2wfswfs-t

I have a service with about thousand points. I wish to enable the user to edit the attributes of the features.

Since there are so many points, I don't want to display them as a vector layer (using WFS Protocol) on the map. I am using a WMS layer to display it on the map.

Using a WMSGetFeatureInfo control, I am getting the attributes of a selected point and showing them in a form to the user.

How can I now save the edited attributes to the database? Is there some other way of achieving it?

Best Answer

One possible workaround could be to pull only the selected point as a feature in an additional layer. For instance, after the user selected a point in the WMS throuhgh the WMSGetFeatureInfo control, add a WFS-T layer that only contains the selected point, and remove it again after editing it. Then you only need o display one point at a time in a vector layer.

You can instantiate the map with a Vector layer, which has a filter, like this:

//selected consumers being shown as Vector (WFS) layer
saveStrategy = new OpenLayers.Strategy.Save({auto:false});
saveStrategy.events.register('success', saveStrategy, onDataSuccesfullySaved);
saveStrategy.events.register('fail', saveStrategy, onDataSaveFail);

QueryFilter=new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.EQUAL_TO,
        property: "customerid",
        value: "-1"
    }),


wfslayer = new OpenLayers.Layer.Vector("WFS", 
            {
            strategies: [new OpenLayers.Strategy.BBOX(),  saveStrategy],
            protocol: new OpenLayers.Protocol.WFS({
                      url:  "http://localhost:8080/geoserver/Test/wfs",
                      featureType: "consumers",
                      featureNS: "http://www.example.com/Test",
                      srsName: "EPSG:4326",
                      version: "1.1.0",
                      visible:false,
                      }),
            filter:QueryFilter
            }); 
map.addLayer(wfslayer);

Include the following function to update the filter:

//function to set the wmslayer with the new filter
function SetFilter(id)
    {
        QueryFilter.value=id;
        //now Force refesh the vector layer
        wfslayer.refresh({force: true}); 
    }

You can change the subset, by calling the SetFilter function, with particular customerid, like this:

SetFilter(26570); or 

SetFilter(selId);

If your data is stored in a database (such as PostGIS), you could also consider writing a PHP script that updates the feature attribute table based on the feature ID you get from the WMSGetFeatureInfo request.