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?

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

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,
});
  • I tried your code, but unfortunately the problem remains: layer WFS is'nt displayed !! – TonyGis Jan 12 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 Jan 12 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 Jan 12 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 Jan 12 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 Jan 12 at 15:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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