[GIS] Problems saving moved points via OpenLayers

geoserveropenlayers-2postgiswfs-t

I am trying to implement what is described by @underdark in this post but have hit a snag. I have a point layer stored in PostGIS and served via GeoServer to a webpage using Openlayers. The data displays as expected. I want to move the point based on geolocation and save the new position back to the PostGIS database and have seen some similar posts (and this unsolved one) but unfortunately they don't solve my issue.

I am using a save strategy on my point layer set to auto: true, which according to the documentation is supposed to automatically save any modified feature. The move function is supposed to automatically redraw the feature.

var gpsLayer = new OpenLayers.Layer.Vector('gpsLayer', {
    strategies: [new OpenLayers.Strategy.BBOX(), 
                 new OpenLayers.Strategy.Save({auto: true})],   
    projection: new OpenLayers.Projection("EPSG:900913"),
    protocol: new OpenLayers.Protocol.WFS({
        version: "1.1.0",
        url:  "http://localhost/geoserver/wfs",
        featureType: "gpslocs",
        srsName: "EPSG:900913",
        featureNS: "www.testServer.gpsTests/data",
        featurePrefix: "Geolocation_Test",
        geometryName: "geom",
        schema: "http://localhost/geoserver/wfs/DescribeFeatureType?version=1.1.0&;typename=cite:gpslocs",
    }),
});

var geolocate = new OpenLayers.Control.Geolocate({
    geolocationOptions: {
        enableHighAccuracy: false,
        maximumAge: 0,
        timeout: 7000,
        bind: true,
        watch: true
    }
});

geolocate.events.register("locationupdated",geolocate,function(e) { 
    var selectedFeatures = gpsLayer.getFeaturesByAttribute('name','Fred');
    var newloc = new OpenLayers.LonLat(e.point.x, e.point.y).transform(epsg4326, epsg900913);
    alert(selectedFeatures.length + '\n' + newloc); \\gives me the correct new location in a dialog box as a visual check
    selectedFeatures[0].move(newloc);           
});

geolocate.events.register("locationfailed",this,function() {
    OpenLayers.Console.log('Location detection failed');
});

geolocate.activate();

Everything works as expected except that the feature is not moved, no changes are saved and no errors are generated. I have tried doing the process manually in Firebug and the geometry of the point appears to have been altered correctly but it has not moved on the map and is definitely not saved. However, I notice in Firebug that the object bounds remain as they were originally before the move, which strikes me as odd.

I have tried adding a refresh strategy, manually redrawing the layer and changing the feature.state to UPDATE. None of these things give an error but neither do they make any difference.

Any help would be appreciated.

UPDATE
OK – I can get the point to move manually in Firebug by NOT projecting the coordinates (i.e. using the map projection (EPSG:4326) rather than the layer's native projection (EPSG:900913). However, this doesn't work when I edit my code to match and there is still no save.

UPDATE 2
I have changed a portion of the code as follows (see above update):

geolocate.events.register("locationupdated",geolocate,function(e) {     
    var selectedFeatures = playerLayer.getFeaturesByAttribute('name','Fred');
    var newloc = new OpenLayers.LonLat(e.point.x, e.point.y);
    alert(selectedFeatures.length + '\n' + e.point.x + '\n' + e.point.y);
    selectedFeatures[0].move(newloc);
    alert(selectedFeatures[0].geometry);
});

The second alert tells me that the point has apparently been moved to the correct new location – but it is still neither redrawn or saved as per what seems to be suggested in the documentation. Adding the line selectedFeatures[0].State = OpenLayers.State.Update as per this post doesn't help. I see the point disappear (as though it has been moved – the new location is many miles away and not visible initially) but when I pan the map, the point reappears back where it was before. So, I conclude that my point IS being moved but temporarily and not saved back to PostGIS. I feel I'm getting closer!

UPDATE 3
I have updated to Geoserver 2.3.3 (just in case). It makes no difference. I have seen some references to Geoserver not playing nicely with PostGIS 2 and wonder if this is the problem?

UPDATE 4
I have tried adding explicit authentication using JQuery (which works nicely) with a matching user and role to allow writing to the database in question. I have tried using a shapefile instead of PostGIS (see comments above). I have tried forcing a save with saveStrategy.save() and gpsLayer.commit. None of this works. The point still is moved but not saved.

Saving edits back to the server must work for some people (though I have read hundreds of posts where it does not work – but no solutions – so this is not a localized issue but apparently quite widespread). Surely somebody can shed some light on this issue, please? Is there some known bug with Geoserver?

UPDATE 5
I have added the following code because of the lack of any report in the GeoServer logs:

saveStrategy.events.register('start', null, saveStart); 
saveStrategy.events.register('success', null, saveSuccess); 
saveStrategy.events.register('fail', null, saveFail); 

function saveSuccess(event) { 
    alert('Your changes have been successfully saved.'); 
} 
function saveFail(event) { 
    alert('Error! Your changes could not be saved. '); 
} 
function saveStart(event) { 
    alert('Starting to save... '); 
} 

The saveStart and saveSuccess functions are both called and popup alert messages! So, OpenLayers thinks that the save is successful, when clearly it isn't.

Best Answer

After a lot of digging, it turns out that this is an authentication issue. A lot of people seem to be having a problem with this. However I have followed some examples (eg [this one])for authenticating from the client side1 by calling GeoServer's j_spring_security_check. The authentication appears to work and be accepted but only temporarily, which is why I didn't see it as the problem initially. Anyway, by removing all authentication, the code works - which solves the problem in this post (though I will have to dig down to see what was going wrong with the authentication).