-2

I know a lot of issues have already been posted about this tricky situation, but I can't figure out where is the problem in my script and most of the discussions about OpenLayers are outdated.

I'm using OpenLayers 5.2.0 and GeoServer 2.4.0.

I'm trying to retrieve a geojson from a WFS query and display it using OpenLayers, here's my script:

var vectorSource = new ol.source.Vector({
    format: new ol.format.GeoJSON(),
    url: 'http://host/geoserver/project/ows?service=WFS&' +
            'version=2.0.0&request=GetFeature&typename=Project:Countries_110m&' +
            'outputFormat=application/json&srs=EPSG:3857',
    wrapX: false
});

var vector = new ol.layer.Vector({
    source: vectorSource,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: 'rgba(0, 0, 255, 1.0)',
            width: 2
        })
    })
});

try {map.addLayer(vector);}
catch (error) {}

I already modified the layer in GeoServer so that it sends the dataset in EPSG:3857 (the source is in EPSG:4326). When I copy/paste the url in Firefox, it returns a clean GeoJSON, but when I try to display it in OpenLayers, it returns:

uncaught exception: AssertionError: Assertion failed. See https://openlayers.org/en/5.2.0/doc/errors/#36 for details.

The doc indicates a projection issue but the map and the GeoJSON srs are the same, I can't seem to figure out where the problem comes from.

EDIT

I've tried some other solutions, mainly gathered on this post, but OpenLayers doesn't display the WFS response:

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

var vectorSource = new ol.source.Vector({
    loader: function (extent) {
        $.ajax('http://host/geoserver/project/ows', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '2.0.0',
                request: 'GetFeature',
                typename: 'Project:Countries_110m',
                srsname: 'EPSG:3857',
                bbox: extent.join(',') + ',EPSG:3857'
            }
        }).done(function (response) {
            vectorSource.addFeatures(formatWFS.readFeatures(response));
        });
        },
    strategy: ol.loadingstrategy.bbox
});

vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#fff2fe',
            width: 1
        })
    })
});

try {map.addLayer(vector);}
catch (error) {}

I tried changing the WFS version (with 1.0.0 and 1.1.0) but without luck. With this method, I don't get any javascript error, I have no idea where the problem lies.

  • removed GeoServer tag as that seems not to be the problem – Ian Turton May 20 at 15:12
  • try setting the data (server) and feature (view) projections in the format format: new ol.format.GeoJSON({ dataProjection: 'EPSG:3857', featureProjection: 'EPSG:3857' }), – Mike May 20 at 15:28
  • What are the previous Q&As that you have reviewed? If they need updating and/or clarifying then your question could help to improve them. – PolyGeo May 20 at 20:09
  • @Mike I tried this solution but I get the same error message. Thanks for the idea though. – BFlat May 21 at 8:52
  • @PolyGeo I may have been a bit harsh about those ol questions, I was mainly refering to the over representation of openlayers 2/3 Q&A. I'll edit my question to add some variations of my script I already tried without luck. – BFlat May 21 at 8:56
0

I solved the issue by upgrading GeoServer to version 2.15.1 as the previous version (2.4.0) was returning by default a GeoJSON with a crs properties that OpenLayers 5 couldn't read :

OpenLayers 2.4.0 crs property

"crs": {
  "type": "EPSG",
  "properties": {
    "code": "4326"
  }
}

OpenLayers 2.15.1 crs property

"crs": {
  "type": "name",
  "properties": {
    "name": "urn:ogc:def:crs:EPSG::4326"
  }
}

Hence the error #36 OpenLayers was throwing : Unknown SRS type. Expected "name".

  • GeoJSON no longer has a crs member and uses only one projection urn:ogc:def:crs:OGC::CRS84 which is not the same as EPSG:4326 – nmtoken Jun 11 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.