0

I try OpenLayers.Control.GetFeature to get feature from WFS layer :

  function editTool() {
        var edit = new OpenLayers.Control.GetFeature({
            protocol: OpenLayers.Protocol.WFS.fromWMSLayer(Vidagis.CurrentMap.map.layers[1]),
            click: true,
            box: true,
            multipleKey: "shiftKey",
            toggleKey: "ctrlKey"
        }, {
            displayClass: 'pan no-follow',
            allowDepress: true
        });
        edit.events.register("featureselected", this, function (e) {
            currentFeature = e.feature; // get the current handler feature 
            changeHtmlForm(storeFeature(e));
            unlockUpdate(storeFeature(e).data);
            $(function () {
                $("#smalldialog").dialog({
                    position: { my: 'right', at: 'right' },
                    maxHeight: 400,
                    maxWidth: 300,

                    autoOpen: false,
                    show: {
                        effect: "blind",
                        duration: 1000
                    },
                    hide: {
                        effect: "explode",
                        duration: 1000
                    }
                });
                $("#smalldialog").dialog("open");
            });

        });
        return edit; // return edit Control
    }

it work fine and i can get feature attributes from featureselected event .now i assign values to currentFeature

currentFeature = e.feature;

Then i write a function to edit currentFeature attributes

 // Edit feature attributes
    function setFeatureAttributes(e) {
         e.attributes['code_2004'] = 124;
         return e;
    }

finally , save function

   function UpdateFeature() {
        var feature=setFeatureAttribute(currentFeature);
        feature.state = OpenLayers.State.UPDATE;
        saveStrategy.save([feature]);
    }

Declare variable :

  var currentFeature;
    var saveStrategy = new OpenLayers.Strategy.Save();
// trigger save event 
    saveStrategy.events.on({
        'success': function (event) {
            alert('Changes saved');
        },
        'fail': function (event) {
            alert('Error! Changes not saved');
        },
        scope: this
    });

But when i call UpdateFeature() browser report the error

Uncaught TypeError: Cannot read property 'projection' of null

How can i fix it?

  • Use you browser's debugger to figure our where and why the error is thrown. Apparently something is not initialised correctly but it's impossible to figure out what exactly from your code alone. – kryger Oct 5 '15 at 12:18
  • thank for your comment , i forgot setup protocol for wfs layer :D – Ihenry Oct 5 '15 at 14:32
0

You have a typo in your code:

var feature=setFeatureAttribute(currentFeature);

you call setFeatureAttribute but your function is called setFeatureAttributes

function setFeatureAttributes(e) {

Apart from that I haven't used saveStrategy without using a WFS-Layer before. What definetly works is to add the feature you get from your control to a filtered WFS-Layer and use savesStrategy for this layer. I just gave it a quick try and this works:

//var wms=    // your wms-layer....

    var QueryFilter=new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.EQUAL_TO,
        property: "code_2004",
        value: "-1"  // something that does not exist in your dataset
    });


    wfs=
  new OpenLayers.Layer.Vector("Editable Features", {
        strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy],
        projection: // your projection
        protocol: // your protocol,
        filter:QueryFilter  // Important: filter to have an empty layer
    });

    map.addLayers([wms_layer ,wfs]);



    function editTool() {
        var edit = new OpenLayers.Control.GetFeature({
            protocol: OpenLayers.Protocol.WFS.fromWMSLayer(wms_layer),
            click: true,
            box: true,
            multipleKey: "shiftKey",
            toggleKey: "ctrlKey",

                                   formatOptions: {
                           outputFormat: "text/xml"
                       },
                       featureType: 'polygons',
                       featurePrefix: 'your prefix',
                       geometryName: 'geom',
                       maxFeatures: 10


        }, {
            displayClass: 'pan no-follow',
            allowDepress: true
        });

        edit.events.register("clickout", this, function(e) {

              // ADD FEATURE TO WFS LAYER
              wfs.removeFeatures([e.feature]);

        });

        edit.events.register("featureselected", this, function (e) {
            currentFeature = e.feature; // get the current handler feature 

            // REMOVE LAYER FROM WFS_LAYER
            wfs.addFeatures([currentFeature]);


            //changeHtmlForm(storeFeature(e));
            //unlockUpdate(storeFeature(e).data);
           //console.log(currentFeature.attributes);
            //setFeatureAttributes(currentFeature);

            $(function () {
                $("#smalldialog").dialog({
                    position: { my: 'right', at: 'right' },
                    maxHeight: 400,
                    maxWidth: 300,

                    autoOpen: false,
                    show: {
                        effect: "blind",
                        duration: 1000
                    },
                    hide: {
                        effect: "explode",
                        duration: 1000
                    }
                });
                $("#smalldialog").dialog("open");
            });

        });



         map.addControl(edit);

        edit.activate();
        //return edit; // return edit Control
    }


    function setFeatureAttributes(e) {
         //e.attributes['gfname'] = 124;
         e.attributes['gfname'] = prompt("which attribute do you want to save?");
         //console.log(e);
         return e;
    }

     function UpdateFeature() {
        var feature=setFeatureAttributes(currentFeature);
        console.log(feature);
        feature.state = OpenLayers.State.UPDATE;
        saveStrategy.save(
        //[feature]
        );
    }

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.