Saturday, 30 November 2019

openlayers - Save features and attributes as WFS in GeoServer


I want to create some squared features with OpenLayers 4, add attributes to each feature and want to save them on the GeoServer to load them later on to another map. I am able to create the features, add attributes to them, read the attributes again and load a layers as WFS again.


My only problem is to save the created features to the GeoServer. I figured out that saving and loading should be possible with WFS-t. I found some examples online but they don't seem to work properly.


When i send the feature to the GeoServer with the function transactWFS at line 133 I get the Error "org.geoserver.wfs.WFSTransactionException: Feature type 'Stairways' is not available" in the GeoServer log file. The Layer Stairways exists on the GeoServer. This is the XML file that is sent:











1528915.8154546698
6627851.731194374 1528917.9110709063

6627858.932471524 1528925.1123480562
6627856.836855288 1528923.0167318196
6627849.635578138 1528915.8154546698
6627851.731194374










This is my whole Code:


   GridSource.addFeature(NewSquare);

var attr = {};
var key = "TestAttr";
attr[key] = "This is a Test";

NewSquare.attributes = attr;

transactWFS('insert', NewSquare);
}



var formatWFS = new ol.format.WFS();

var formatGML = new ol.format.GML({
featureNS: gs.wfs,
featurePrefix: 'HTW_Erd',

featureType: 'Stairways',
srsName: 'EPSG:3857'
});

var xs = new XMLSerializer();


var transactWFS = function (mode, f) {
var node;
switch (mode) {

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;
}

var payload = xs.serializeToString(node);
$.ajax(gs.ows, {
service: 'WFS',
type: 'POST',
dataType: 'xml',
processData: false,
contentType: 'text/xml',
data: payload,
error: function(e) {
var errorMsg = e? (e.status + ' ' + e.statusText) : "";

alert('Error saving this feature to GeoServer. \n\n'
+ errorMsg);
}
}).done();

};

Edit:
I saw in the log file that i wasnt allowed to write on that layer so i set the permissions. I also changed the projection of the GridLayer to 3857 and the name of the feature to geometry. But that didn't help either. I now don't get any errors and the log looks like this:


2017-07-30 23:47:07,676 INFO [geoserver.wfs] - 

Request: getServiceInfo
2017-07-30 23:47:07,709 INFO [geoserver.wfs] -
Request: getServiceInfo
2017-07-30 23:47:07,744 INFO [geoserver.gwc] - DataStoreChange: {https://localhost:8080/geoserver/htw_erd}Stairways PreInsert
2017-07-30 23:47:07,749 INFO [geoserver.gwc] - DataStoreChange: {https://localhost:8080/geoserver/htw_erd}Stairways PostInsert
2017-07-30 23:47:07,751 INFO [geoserver.wfs] -
Request: getFeature
service = WFS
version = 1.1.0
baseUrl = http://localhost:8080/geoserver/

query[0]:
filter = [ bbox POLYGON ((1528628.1984057308 6627469.038650171, 1528628.1984057308 6628141.798427672, 1529548.9368749668 6628141.798427672, 1529548.9368749668 6627469.038650171, 1528628.1984057308 6627469.038650171)) ]
srsName = EPSG:3857
typeName[0] = {https://localhost:8080/geoserver/htw_erd}Stairways
outputFormat = application/json
resultType = results
2017-07-30 23:47:07,754 INFO [wfs.json] - about to encode JSON
2017-07-30 23:47:07,801 INFO [geoserver.wfs] -
Request: transaction
service = WFS

version = 1.1.0
baseUrl = http://localhost:8080/geoserver/
group[0] = wfs:insert=net.opengis.wfs.impl.InsertElementTypeImpl@e314ac (feature: [SimpleFeatureImpl:Stairways=[SimpleFeatureImpl.Attribute: the_geom=null, SimpleFeatureImpl.Attribute: id=null, SimpleFeatureImpl.Attribute: indoor_are=null, SimpleFeatureImpl.Attribute: indoor_lev=null, SimpleFeatureImpl.Attribute: indoor_l_1=null]], handle: null, idgen: , inputFormat: , srsName: null)
insert[0]:
feature[0] = SimpleFeatureImpl:Stairways=[SimpleFeatureImpl.Attribute: the_geom=null, SimpleFeatureImpl.Attribute: id=null, SimpleFeatureImpl.Attribute: indoor_are=null, SimpleFeatureImpl.Attribute: indoor_lev=null, SimpleFeatureImpl.Attribute: indoor_l_1=null]
idgen = GenerateNew
inputFormat = text/xml; subtype=gml/3.1.1
releaseAction = ALL

All the features that are already in the Layer named 'the_geom', renaming the feature to the_geom doesn't help.



Edit 2: This is the Output




No service: ( wfs\ )



I have to add that I dont have the default layers anymore, so i dont have the topp workgroup and the layer states anymore.


Edit 3: This is the Output for this link: http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=DescribeFeatureType&typeName=HTW_Erd:Stairways






















Answer



You are telling GeoServer that the namespace for your feature is xmlns="http://localhost:8080/geoserver/wfs" (or gs.wfs) when it should be the URI associated with your workspace that contains the Stairways featuretype.


I would also check to be sure that the layer name really starts with a capital S as it is case sensitive.


EDIT



You will need to set the geometry field name using


 geometryName: "the_geom",

in your GML format.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...