1

I'm having a problem with visualizing a WFS layer in Openlayers. I can see I get the response, but nothing shows up on my map. I tried more potential solutions from stackexchange, but couldn't get it working. The WFS layers are shown correctly in QGIS without any special configuration. The address of the WFS in question is: http://eagri.cz/public/app/wms/plpis_wfs.fcgi - open data for the Czech Republic.

My code (basic version, I tried more solutions which didn't work):

var vectorSource = new ol.source.Vector({
    format: new ol.format.WFS(),
    url: function (extent) {
        return (
            'http://eagri.cz/public/app/wms/plpis_wfs.fcgi?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=LPIS_DPB_UCINNE&SRSNAME=EPSG:102067&' +
            'bbox=' +
            extent.join(',') +
            ',EPSG:3857'
        );
    },
    strategy: ol.loadingstrategy.bbox
});

var vector = new ol.layer.Vector({
    source: vectorSource,
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#FF0000',
            width: 2
        }),
        projection: new ol.proj.Projection('EPSG:102067')
    }),
});

var map = new ol.Map({
    target: 'map',
    pixelRatio: 1,
    layers: [
        vector
    ],
    view: new ol.View({
        center: ol.proj.fromLonLat(cw_global_initCoordinates),
        zoom: 15
    })
});

Generated request example: http://eagri.cz/public/app/wms/plpis_wfs.fcgi?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=LPIS_DPB_UCINNE&SRSNAME=EPSG:102067&bbox=1885304.656759394,6323618.39452889,1892413.3003899155,6326422.678004101,EPSG:3857 - follow the request link to get the example response

1 Answer 1

2

First you must specify the WFS (or GML) version new ol.format.WFS({version: '1.0.0'}) otherwise OpenLayers will assume WFS 1.1.0 (GML3). Before OpenLayers version 6.5.0 it had to be specified using new ol.format.WFS({gmlFormat: new ol.format.GML2()})

Since your source does not support EPSG:3857 output you will also need to transform the result from EPSG:102067 to the view projection. You could do that with a loader function, or simply do it when the features are added:

proj4.defs("EPSG:102067","+proj=krovak +lat_0=49.5 +lon_0=24.83333333333333 +alpha=30.28813975277778 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel +units=m +no_defs");
ol.proj.proj4.register(proj4);

var vectorSource = new ol.source.Vector({
    format: new ol.format.WFS({version: '1.0.0'}),
  //  before OpenLayers 6.5.0 use
  //  format: new ol.format.WFS({gmlFormat: new ol.format.GML2()}),
    url: function (extent) {
        return (
            'http://eagri.cz/public/app/wms/plpis_wfs.fcgi?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=LPIS_DPB_UCINNE&SRSNAME=EPSG:102067&' +
            'bbox=' +
            extent.join(',') +
            ',EPSG:3857'
        );
    },
    strategy: ol.loadingstrategy.bbox
});

vectorSource.on('addfeature', function(event){
  event.feature.getGeometry().transform('EPSG:102067', 'EPSG:3857');
});

projection is not a valid option for ol.style.Style

3
  • Just a remark: the above code works only in OL 6.5.0. I tried it also in 6.3.1 and 6.4.3, but it doesn't work there, event.feature.getGeometry() gives undefined value.
    – TomazicM
    Jan 19, 2021 at 16:02
  • 1
    Thanks. version is a new option for OL 6.5.0 so I have updated the answer to mention the alternative gmlFormat option.
    – Mike
    Jan 19, 2021 at 17:15
  • Can confirm, solution working with Openlayers 6.5.0, with version 6.1.1 gives error described by @TomazicM. Thanks!
    – loro2
    Jan 20, 2021 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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