So I have a web app that's running and is able to add points and lines to existing Postgis tables by getting data from Geoserver. I need the following one specific bit of functionality added:
I want to allow users to also be able to add attribute information to each point on the map interface. As in, each point they draw on the map, there needs to be something that allows the user to also enter some text data to the column.
I've tried reading a few questions in here but none of them provide a solution for a point vector layer.
How to do this?
The bit I have for loading and posting the WFS point features is:
var vectorSource2 = new ol.source.Vector({
loader: function(extent, resolution, projection) {
var url2 = 'http://localhost:8080/geoserver/wfs?service=WFS&' +
'version=1.1.0&request=GetFeature&typename=BFTchambers:chamber2&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures2' +
'&bbox=' + extent.join(',');
$.ajax({url: url2, dataType: 'jsonp', jsonp: false})
.done(function(response) {
pointWFS = new ol.format.WFS(),
sourceVector2.addFeatures(pointWFS.readFeatures(response))
});
},
strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
maxZoom: 20
})),
});
window.loadFeatures2 = function(response) {
console.log("Point features were drawn");
vectorSource2.addFeatures(geojsonFormat.readFeatures(response));
};
var formatWFS2 = new ol.format.WFS();
var pointGML = new ol.format.GML({
featureNS: 'http://geoserver.org/bftchamber',
featureType: 'chamber2',
});
var pointWFS = function(p,f) {
switch(p) {
case 'insert':
node = formatWFS2.writeTransaction([f],null,null,pointGML);
break;
case 'update':
node = formatWFS2.writeTransaction(null,[f],null,pointGML);
break;
case 'delete':
node = formatWFS2.writeTransaction(null,null,[f],pointGML);
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();
console.log(" point features were posted to server");
}
case 'btnDrawPoint':
interaction = new ol.interaction.Draw({
type: 'Point',
source: layerVector.getSource()
});
map.addInteraction(interaction);
interaction.on('drawend', function(e) {
pointWFS('insert',e.feature);
});
break;
just add the attribute the way @bartvde suggests.
For example using your own example
interaction.on('drawend', function(e) {
//pass an attribute to the feature
var featureWithAttribute = e.feature.set('foo', 'bar');
pointWFS('insert',featureWithAttribute);
});
So this modification will send a feature holding the geometry, as well as, an attribute for column name foo holding the value of bar.
Now if you want your user to enter text:
interaction.on('drawend', function(e) {
//pass an attribute to the feature
var myAttrValue = prompt("Enter Attribute", "");
var myFeature= e.feature;
if (myAttrValue != null) {
myFeature.set('foo', myAttrValue);
}
pointWFS('insert',myFeature);
});
Of course this is just a sample using the promt default js function. But you may use your UI api to get a similar behaviour
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With