Flexberry / Leaflet-WFST

OGC WFS-T client layer for Leaflet.
http://flexberry.github.io/Leaflet-WFST/
MIT License
151 stars 50 forks source link

Inserting Polygons to PostGIS database #90

Open jamieodonnell opened 4 years ago

jamieodonnell commented 4 years ago

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.

Aasked on stackexchange here with bounty: https://gis.stackexchange.com/questions/348819/leaflet-geoserver-wfst-inserting-polygons-to-postgis-database

kuzkok commented 4 years ago

It is known bug of creating simple geometry when it stores in 'Multi' type. Was fixed there. You should use one of beta versions since v2.0.1-beta.18 and initialize layer with special option forceMulti.

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