0

I have to display a layer from a WFS service (http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Bacini_idrografici.map) using OpenLayers 3.x API (..there's a reason). The WFS service is exposed in EPSG:4326.

In similar cases I have always used the following code successfully:

var sourceWFS = new ol.source.Vector({
    loader: function (extent) {
        $.ajax('http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Bacini_idrografici.map', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'ID.ACQUEFISICHE.BACINIIDROGRAFICI.PRINCIPALI',
                srsname: 'EPSG:4326',
                bbox: extent.join(',') + ',EPSG:4326'
            }
        }).done(loadFeatures)
        .fail(function () {
        alert("error loading vector layer");
        });
    },
    strategy: ol.loadingstrategy.bbox,
});

function loadFeatures(response) {
var formatWFS = new ol.format.WFS();
  sourceWFS.addFeatures(formatWFS.readFeatures(response));
}

var vectorLayerWFS = new ol.layer.Vector({
  source: sourceWFS,
});

But now, this layer WFS isn't displayed on the map and I do not understand why?

What's wrong?

4
  • Is your view projection also EPSG:4326?
    – Mike
    Commented Jan 11, 2019 at 21:11
  • Yes of course, in ol.view I set : projection: 'EPSG:4326'
    – TonyGis
    Commented Jan 12, 2019 at 7:48
  • check your axis orders match
    – Ian Turton
    Commented Jan 12, 2019 at 9:55
  • 1
    axis orders are right too
    – TonyGis
    Commented Jan 12, 2019 at 11:49

1 Answer 1

2

I presume your view is in a diferent projection because your code works when the view is in EPSG:4326. When the view projection differs from the WFS you will need to transform the bbox extent for the WFS request from view projection to EPSG:4326, then transform the features from EPSG:4326 to view projection when reading them.

var sourceWFS = new ol.source.Vector({
    loader: function (extent, resolution, projection) {
        var extent4326 = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
        var extent4326x = [extent4326[1], extent4326[0], extent4326[3], extent4326[2]];
        $.ajax('http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/wfs/Bacini_idrografici.map', {
            type: 'GET',
            data: {
                service: 'WFS',
                version: '1.1.0',
                request: 'GetFeature',
                typename: 'ID.ACQUEFISICHE.BACINIIDROGRAFICI.PRINCIPALI',
                srsname: 'EPSG:4326',
                bbox: extent4326x.join(',') + ',EPSG:4326'
            }
        }).done(loadFeatures)
        .fail(function () {
        alert("error loading vector layer");
        });
        function loadFeatures(response) {
            var formatWFS = new ol.format.WFS();
            sourceWFS.addFeatures(formatWFS.readFeatures(response, {
                dataProjection: 'EPSG:4326',
                featureProjection: projection
            }));
        }
    },
    strategy: ol.loadingstrategy.bbox,
});

var vectorLayerWFS = new ol.layer.Vector({
  source: sourceWFS,
});
5
  • I tried your code, but unfortunately the problem remains: layer WFS is'nt displayed !!
    – TonyGis
    Commented Jan 12, 2019 at 8:11
  • There was misplaced bracket, now corrected. There's no reason why you can't use a different view projection such as EPSG:3857 regardless of the vector data projection. I didn't use your exact code, to simplify the test I used fetch instead of your jquery ajax mikenunn.16mb.com/demo/wfs-italy.html The source only produces data when the bbox includes the whole of Italy, so if you want to open at a higher zoom it would be better to use the default loading strategy (all) mikenunn.16mb.com/demo/wfs-italy-all.html
    – Mike
    Commented Jan 12, 2019 at 11:59
  • Here are working links using jquery ajax mikenunn.16mb.com/demo/wfs-italy-ajax.html mikenunn.16mb.com/demo/wfs-italy-all-ajax.html
    – Mike
    Commented Jan 12, 2019 at 14:08
  • Awesome ... now it's work fine. I think your working example will be very useful, because many WFS layer are exposed in EPSG 4326. Many thanks !!
    – TonyGis
    Commented Jan 12, 2019 at 14:35
  • I've realised the bbox loading strategy problem was due to the axis order, it is a standard WFS after all. I've updated the links and answer. However it is still probably more efficient to load all in cases such as this with a relatively small amount of data covering a single country.
    – Mike
    Commented Jan 12, 2019 at 15:35

Your Answer

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

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