Leaflet GeoServer WFST – How to Insert Polygons into a PostGIS Database Using Leaflet and GeoServer WFST

geojsongeoserverleafletpostgiswfs-t

I have a GeoServer installation on top of a PostGIS database. I am trying to set up a leaflet webmap (using Leaflet WFS-T plugin) so that I can insert, update and delete polygon features. I have managed to get this to work fine for Point features but something seems to be going wrong when inserting polygons.

Below is the request payload to GeoServer:

<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0">
<wfs:Insert><schememapper:geoserver-polygons xmlns:schememapper="schememapper">
<schememapper:geom><gml:Polygon xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326" srsDimension="2">
<gml:exterior><gml:LinearRing srsDimension="2"><gml:posList>-1.3735914230346682 52.03958374014823 -1.9696426391601565 50.929466759820585 -3.7992525100708012 51.977294016543425 -1.3735914230346682 52.03958374014823</gml:posList>
</gml:LinearRing></gml:exterior></gml:Polygon></schememapper:geom></schememapper:geoserver-polygons></wfs:Insert></wfs:Transaction>

And this is the response:

<?xml version="1.0" encoding="UTF-8"?><ows:ExceptionReport xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/ows http://35.242.177.142:8080/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd">
<ows:Exception exceptionCode="InvalidParameterValue">
<ows:ExceptionText>Error performing insert: java.lang.String cannot be cast to org.locationtech.jts.geom.Geometry</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>

When I look at the geometry type in PostGIS it says it is MultiPolygon so I am conscious that the WFST request is only specifying 'polygon'.

Below is my Leaflet code (I am using Leaflet draw to create the polygons):

var wfstPoly = new L.WFST({
    url: 'http:localhost:8080/geoserver/ows',
//typeNS is the workspace in geoserver
    typeNS: 'schememapper',
    typeName: 'geoserver-polygons',
    crs: L.CRS.EPSG4326,
    geometryField: 'geom',
    style: {
      color: 'blue',
      weight: 2
    }
  }).addTo(map)
    .once('load', function () {
      // map.fitBounds(wfstPoly);
    });


// Drawing
var drawControl = new L.Control.Draw({ 
    draw:{circle:false, circlemarker:false, rectangle:false,
          },
    edit:{featureGroup: wfstPoly } });
map.addControl(drawControl);

map.on('draw:created', function (e) {
    var layer = e.layer;
    wfstPoly.addLayer(layer)});

map.on('draw:edited', function (e) {
    var layers = e.layers;
    layers.eachLayer( function (layer) {
        wfstPoly.editLayer(layer);
        });
});

// Save button
L.easyButton('fa-save', function () {
         wfstPoly.save();
     }, 'Save changes').addTo(map);

Furthermore, when I use the console to look at the original features pulled from GeoServer they are GeoJSON but when I look at the newly created features they don't seem to be. They just have an array of coordinates.

EDIT: This may have something to do with GML versions. I.E. Geoserver is expecting the XML of the request to be structured differently.

Best Answer

When you store geometry in 'Multi' postgis types, geoserver also expects this 'Multi' in transaction query. Leaflet-WFST plugin can create Multi-geometry requests on simple geometries by option 'forceMulti', this option available since v2.0.1-beta.18

var wfstPoly = new L.WFST({
    ...
    forceMulti: true
    ...
  }).addTo(map);

Related Question