I'm workin on an OL3 app, where the user will be able to draw, modify and delete polygons and save the changes to GeoServer via WFS-T.
For starting point I've used the solution from here: wfs-t example app
I've changed the code a little bit to use a polygon layer from my GeoServer. The draw, modify and delete polygon functions are working great, if I modify or delete a polygon it's also saved, but the new polygon creation is not saved and I can not figure out why. The original app is working without any problem.
I hope somebody also tried to use this app as a starting point and solved this problem. Could someone give me any idea what is the problem?
Tha most important part of the code:
var dirty = {};
var formatWFS = new ol.format.WFS();
var formatGML = new ol.format.GML({
featureNS: 'http://www.openplans.org/topp',
featureType: 'poly',
srsName: 'EPSG:3857'
});
var transactWFS = function(p,f) {
switch(p) {
case 'insert':
node = formatWFS.writeTransaction([f],null,null,formatGML);
break;
case 'update':
node = formatWFS.writeTransaction(null,[f],null,formatGML);
break;
case 'delete':
node = formatWFS.writeTransaction(null,null,[f],formatGML);
break;
}
s = new XMLSerializer();
str = s.serializeToString(node);
$.ajax('http://localhost:8080/geoserver/wfs',{
type: 'POST',
dataType: 'xml',
processData: false,
contentType: 'text/xml',
data: str
}).done();
}
$('.btn-floating').hover(
function() {
$(this).addClass('darken-2');},
function() {
$(this).removeClass('darken-2');}
);
$('.btnMenu').on('click', function(event) {
$('.btnMenu').removeClass('orange');
$(this).addClass('orange');
map.removeInteraction(interaction);
select.getFeatures().clear();
map.removeInteraction(select);
switch($(this).attr('id')) {
case 'btnSelect':
interaction = new ol.interaction.Select({
style: new ol.style.Style({
stroke: new ol.style.Stroke({color: '#f50057', width: 2})
})
});
map.addInteraction(interaction);
interaction.getFeatures().on('add', function(e) {
props = e.element.getProperties();
if (props.status){$('#popup-status').html(props.status);}else{$('#popup-status').html('n/a');}
if (props.tiendas){$('#popup-tiendas').html(props.tiendas);}else{$('#popup-tiendas').html('n/a');}
coord = $('.ol-mouse-position').html().split(',');
overlayPopup.setPosition(coord);
});
break;
case 'btnEdit':
map.addInteraction(select);
interaction = new ol.interaction.Modify({
features: select.getFeatures()
});
map.addInteraction(interaction);
snap = new ol.interaction.Snap({
source: layerVector.getSource()
});
map.addInteraction(snap);
dirty = {};
select.getFeatures().on('add', function(e) {
e.element.on('change', function(e) {
dirty[e.target.getId()] = true;
});
});
select.getFeatures().on('remove', function(e) {
f = e.element;
if (dirty[f.getId()]){
delete dirty[f.getId()];
featureProperties = f.getProperties();
delete featureProperties.boundedBy;
var clone = new ol.Feature(featureProperties);
clone.setId(f.getId());
transactWFS('update',clone);
}
});
break;
case 'btnDrawPoly':
interaction = new ol.interaction.Draw({
type: 'Polygon',
source: layerVector.getSource()
});
map.addInteraction(interaction);
interaction.on('drawend', function(e) {
transactWFS('insert',e.feature);
});
break;
case 'btnDelete':
interaction = new ol.interaction.Select();
map.addInteraction(interaction);
interaction.getFeatures().on('change:length', function(e) {
transactWFS('delete',e.target.item(0));
interaction.getFeatures().clear();
selectPointerMove.getFeatures().clear();
});
break;
default:
break;
}
});
I'm using a single shape file as data store.
The request to the GeoServer after finishing a polygon:
<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xsi:schemaLocation="http
://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" xmlns:xsi="http://www.w3.org/2001
/XMLSchema-instance"><Insert><poly xmlns="http://www.openplans.org/topp"><geometry><Polygon xmlns="http
://www.opengis.net/gml"><exterior><LinearRing><posList>2274170.418847337 5923526.286802612 2329612.7433635183
5979783.939620501 2373640.4716557795 5936979.203780803 2330835.735816081 5891728.483035979 2274170.418847337
5923526.286802612</posList></LinearRing></exterior></Polygon></geometry></poly></Insert></Transaction
>
The response from GeoServer:
<?xml version="1.0" encoding="UTF-8"?><wfs:TransactionResponse xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sf="http://www.openplans.org/spearfish" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http
://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows"
xmlns:tiger="http://www.census.gov" xmlns:topp="http://www.openplans.org/topp" xmlns:xlink="http://www
.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0" xsi:schemaLocation
="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd"><wfs:TransactionSummary
><wfs:totalInserted>1</wfs:totalInserted><wfs:totalUpdated>0</wfs:totalUpdated><wfs:totalDeleted>0</wfs
:totalDeleted></wfs:TransactionSummary><wfs:TransactionResults/><wfs:InsertResults><wfs:Feature><ogc
:FeatureId fid="new0"/></wfs:Feature></wfs:InsertResults></wfs:TransactionResponse>
See Question&Answers more detail:
os