Bienvenue
Bienvenue

Reputation: 11

Wfs layer registration failed

I can't solve this problem:
We have developed a webmapping application.
We use OpenLayers version 6 and Geoserver 2.18, and PostGis 3.0.2

Loading a WFS layer on openlayers is no problem.
On Openlayers, I tried to edit (add new vector) this layer (wfs-t) but save failed.

Editing this layer (wfs-t) on Qgis is going well.

here are the code blocks:

//variables
var its_typeName ='reception:fak_test';
var url_NS = 'http://localhost:8080/geoserver';
var url_wfs ='http://localhost:8080/geoserver/reception/ows?service=WFS&version=1.0.0';
var fak_draw;
var formatWFS = new ol.format.WFS();
var formatGML = new ol.format.GML3({
    featureNS: url_NS,
    featureType: its_typeName,
    srsName: 'EPSG:4326'
    //srsName: "EPSG:3857"
});
var transactWFS = function(p,f) {
    f.unset('bbox', true);
    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);
    console.log(str);
    
    // send data
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            alert(request.responseText);
        }
    }
    request.open("POST", url_wfs, true);
    request.send(str);
    
}

// parameter of layer
var PolygonStyle = [
    new ol.style.Style({
        stroke: new ol.style.Stroke({ color: '#EC1B0C', width: 3,}),
        fill: new ol.style.Fill({ color: 'rgba(0, 0, 255, 0.1)',}),
    })
];

// to load wfs on OpenLayers
function test_wfs_t(){
    var url = url_wfs
        + '&request=GetFeature'
        + '&typeName=' + its_typeName
        + '&outputFormat=application/json';
    fetch(url)
    .then((response) => response.text())
    .then((data) => {
        //console.log(data);
        getItsAttributs_db(data);
    });

}

function getItsAttributs_db(xData)
{
    //get its infos to post in the new feature
    var JSON_received = JSON.parse(xData);
    var len_draw = JSON_received.features.length;
    var xid = len_draw + 1;
    var nom = "xxx";
    var custom_lab = "next";
    var descriptio = "bla bla";
    var xcouleur= "#FF0000";
    
    // convert to GeoJSON
    const vectorSource = new ol.source.Vector({
        features: new ol.format.GeoJSON().readFeatures(xData, {
            dataProjection: 'EPSG:4326',
            featureProjection: 'EPSG:3857'
        }),
    });

    // init vector to load on map
    var fak_Vector = new ol.layer.Vector({
        source: vectorSource,
        style: PolygonStyle
    });
    var its_source = fak_Vector.getSource();

    // load on map
    map.addLayer(fak_Vector);

    //remove old draw
    map.removeInteraction(fak_draw);

    //add new draw
    draw_boo_wfst = true;
    fak_draw = new ol.interaction.Draw({
        type: 'MultiPolygon',
        source: its_source
    });
    map.addInteraction(fak_draw);
    
    // on end of draw
    fak_draw.on('drawend', function(e) {
        //add its properties
        var draw_wfst = e.feature;
        draw_wfst.setProperties(create_gfc_Attributs_view(xid, nom, descriptio, custom_lab, xcouleur));
        // try to send to database
        transactWFS('insert', draw_wfst);
    });

}

// to create attributs of the new feature
function create_gfc_Attributs_view(xid, nom, descriptio, custom_lab, xcouleur){
    var xTable = {};
    xTable.id = xid;
    xTable.name = nom;
    xTable.descriptio = descriptio;
    xTable.custom_lab = custom_lab;
    xTable.color = xcouleur;
    // callback
    return xTable;
}

PostgreSQL syntax :

-- Structure of postgis
CREATE TABLE public.fak_test
(
    id integer NOT NULL DEFAULT nextval('track_store.fak_test_id_seq'::regclass),
    geom geometry(MultiPolygon,4326),
    name character varying(254) COLLATE pg_catalog."default",
    descriptio character varying(254) COLLATE pg_catalog."default",
    custom_lab character varying(254) COLLATE pg_catalog."default",
    color character varying(254) COLLATE pg_catalog."default",
    CONSTRAINT fak_test_pkey PRIMARY KEY (id)
)

GRANT ALL ON TABLE public.fak_test TO myuser;

CREATE INDEX sidx_fak_test_geom
    ON public.fak_test USING gist
    (geom)
    TABLESPACE pg_default;
    

Output of GML

<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>
        <reception:fak_test xmlns:reception="http://localhost:8080/geoserver">
            <reception:geometry>
                <MultiPolygon xmlns="http://www.opengis.net/gml" srsName="EPSG:4326">
                    <polygonMember>
                        <Polygon srsName="EPSG:4326">
                        <exterior>
                            <LinearRing srsName="EPSG:4326">
                                <posList srsDimension="2">
                                    -2144007.8418341065 5288167.744512376 -2144040.7892421824 5289040.850826394 -2145259.8433409994 5288645.48192948 -2144007.8418341065 5288167.744512376
                                </posList>
                            </LinearRing>
                        </exterior>
                        </Polygon>
                    </polygonMember>
                </MultiPolygon>
            </reception:geometry>
            <reception:id>5</reception:id>
            <reception:name>xxx</reception:name>
            <reception:descriptio>bla blax</reception:descriptio>
            <reception:custom_lab>suite</reception:custom_lab>
            <reception:color>#FF0000</reception:color>
        </reception:fak_test>
    </Insert>
</Transaction>

Response from server:

<?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://localhost:8080/geoserver/schemas/ows/1.0.0/owsExceptionReport.xsd">
<ows:Exception exceptionCode="InvalidParameterValue">
<ows:ExceptionText>org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: -2144007.8418341065 outside of (-180.0,180.0)
Parsing failed for LinearRing: org.geoserver.wfs.WFSException: org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: -2144007.8418341065 outside of (-180.0,180.0)
org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: -2144007.8418341065 outside of (-180.0,180.0)
-2144007.8418341065 outside of (-180.0,180.0)</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>

Where is the problem? Thank you

Upvotes: 0

Views: 237

Answers (1)

Ian Turton
Ian Turton

Reputation: 10976

You are lying to GeoServer and it doesn't like it. You are sending coordinates that are not in lat/lon but claiming that they are.

                <Polygon srsName="EPSG:4326">
                    <exterior>
                        <LinearRing srsName="EPSG:4326">
                            <posList srsDimension="2">
                                -2144007.8418341065 5288167.744512376 -2144040.7892421824 5289040.850826394 -2145259.8433409994 5288645.48192948 -2144007.8418341065 5288167.744512376
                            </posList>
                        </LinearRing>
                    </exterior>
                   </Polygon>

I don't know what projection your map is in, but I'd guess it is Web Mercator (EPSG:3857) so you should either set that in the data you send or reproject the polygon to ESPG:4326 before you send it.

Upvotes: 1

Related Questions