0

i am using geoserver 2.5.2 and openlayers3 i can show geojson wfs data from geoserver with geojson and edit with interactions on map

(function () {

    showData= function () {
        var format = new ol.format.GeoJSON();
        vectorLayer = new ol.layer.Vector({
            source: new ol.source.StaticVector({
                format: format,
                projection: 'EPSG:3857'
            }),
            //style: mapping.customStyleFunction
        });
        var select = new ol.interaction.Select();
        var modify = new ol.interaction.Modify({
            features: select.getFeatures()
        });
        // Create the map
        map = new ol.Map({
            interactions: ol.interaction.defaults().extend([select, modify]),
            target: 'map',  // The DOM element that will contains the map
            renderer: 'canvas', // Force the renderer to be used
            layers: [
                // Add a new Tile layer getting tiles from OpenStreetMap source
                new ol.layer.Tile({ source: new ol.source.MapQuest({ layer: 'osm' }) }),
                vectorLayer
            ],
            view: new ol.View({
                center: ol.proj.transform([28.86820725, 41.14670434], 'EPSG:4326', 'EPSG:3857'),
                zoom: 10,
            })
        });
        //map.on("")
    },
    setData= function (geojsondata) {
        var source = vectorLayer.getSource();
        var text = geojsondata;

        if (text === '') {
            source.clear();
            return;
        }
        var json = JSON.parse(text);
        var features = source.readFeatures(json);
        source.addFeatures(features);

    }
})

enter image description here but I do not know how to save the updated data on geoserver using openlayers3 in other words how to post updated data to geoserver?

3
  • I believe you must use WFS format instead of GeoJSON. Start reading from boundlessgeo.com/2014/06/openlayers-editing-wfs-t.
    – user30184
    Jan 16, 2015 at 15:39
  • what is different? and how can i make operations(insert update delete) using WFS format independently of the database on geoserver. i need openlayers3 code example Jan 16, 2015 at 17:15
  • You do not need to care what is behind WFS on the GeoServer side. Without complete code examples you must probably study this and make trials. There are lots of OpenLayers 2 examples and they can probably help with learning the basics.
    – user30184
    Jan 16, 2015 at 22:12

1 Answer 1

1

I personally could not get WFS-T working in OL3.
Instead I reverted to manually crafting an XML statement and sending that to GeoServer.
My situation was somewhat simpler. I had to insert new features, currently just points, instead of modifying them. But if you knew the the ID of the feature you wanted to modify, you can send an update XML statement. Try looking at the WFS Implementation Specification 1.1.0, also GeoServer has some inbuilt demos to demonstrate how to create and text XML statements.

Here is my current single feature insert code,

function createInsertXML(){
var geomType;
drawSource.addFeatures(draw);
var a = drawSource.getFeatures();
var coords = a[0].f.Wc.p.geometry.j;
if (coords.length == 2){
    geomType = 'Point';
} else if (coords.length > 2) {
    if (coords.lastIndexOf(coords[1]) == (coords.length - 1)) {
        geomType = 'Polygon';
    } else {
        geomType = 'LineString';
    }
}

var featNS = 'YourNS';
var featName = 'YourGeoServerLayer';
var featType =  geomType;
var featGeom =  coords;
var XMLCompleteString = '';
var XMLInsertHeaderString =     '<wfs:Transaction service="WFS" version="1.0.0" ' +
                                'xmlns:wfs="http://www.opengis.net/wfs" ' +
                                'xmlns:' + featNS  + '="' + featNS + '" ' +
                                'xmlns:gml="http://www.opengis.net/gml" ' +
                                'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' +
                                '<wfs:Insert>';
var XMLInsertContentHeaderString =      '<' + featNS + ':' + featName + '>';

switch (featType) {
    case 'Point':
        var XMLInsertGeometryHeaderString =     '<' + featNS + ':' + 'Geometry' + '>';
        var XMLInsertFeatureTypeHeaderString =  '<gml:Point>';
        var XMLInsertCoordinateString =     '<gml:coordinates decimal="." cs="," ts=" ">' +
                                            featGeom[0] + "," + featGeom[1] +
                                            '</gml:coordinates>';
        var XMLInsertFeatureTypeCloseString =   '</gml:Point>';
        var XMLInsertGeometryCloseString =   '</' + featNS + ':' + 'Geometry' + '>';
        break;
    case 'PolyLine':
        //TODO Implement PolyLines
        break;
    case 'Polygon':
        //TODO Implement Polygons
        break;
}

var XMLInsertJobNumberString =  '<' + featNS + ':' + 'JobNumber' + '>' + $('#jobNumber').val() + '</' + featNS + ':' + 'JobNumber' + '>';
var XMLInsertContentCloseString =   '</' + featNS + ':' + featName + '>';
var XMLInsertCloseString =  '</wfs:Insert>' +
                            '</wfs:Transaction>';
XMLCompleteString = XMLCompleteString.concat(
    XMLInsertHeaderString,
    XMLInsertContentHeaderString,
    XMLInsertGeometryHeaderString,
    XMLInsertFeatureTypeHeaderString,
    XMLInsertCoordinateString,
    XMLInsertFeatureTypeCloseString,
    XMLInsertGeometryCloseString,
    XMLInsertJobNumberString,
    XMLInsertContentCloseString,
    XMLInsertCloseString
);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST",'http://' + GeoServerHost + GeoServerPort + GeoServerWFSPost,true);
xmlhttp.send(XMLCompleteString);

}

It's rough, but works for me.

1
  • I've done something similar before but you might want to skip the manual gml generation, there is a function that works pretty well gml = new OpenLayers.Format.GML(); var polygonGML = gml.buildGeometry.polygon.apply(gml,[feat.geometry]); (OR POINT) var GMLString = (new XMLSerializer()).serializeToString(polygonGML); Mar 30, 2015 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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