[MapServer-users] How to fetch a wfs layer in openLayers

Thomas Gratier osgeo.mailinglist at gmail.com
Sun Oct 30 07:11:54 PDT 2022


Hello,

A least, you seem to got issues from 2 parts:

First,

```
const featureRequest = new ol.format.WFS().writeGetFeature({
        srsName: 'EPSG:3857',
        featureNS: 'http://openstreemap.org',
        featurePrefix: 'osm',
        featureTypes: ['emprise_3857'],
        outputFormat: 'application/json',
        filter: new ol.format.filter.equalTo('numoa', '25366'),
})
```

You want to use namespace whereas I'm not sure Mapserver use the same
namespace or use any at all. The original server in the OpenLayers sample
alias `http://openstreemap.org` to `osm` as you can see at
https://ahocevar.com/geoserver/wfs?request=GetCapabilities&service=WFS&version=2.0.0


2nd issue: as you guessed, maybe, your Mapserver may not return JSON. You
send it a POST with XML e.g `new
XMLSerializer().serializeToString(featureRequest)` where your
`featureRequest` contains `outputFormat: 'application/json'`. You may try
using `outputFormat: 'application/gml+xml; version=3.2'`. I suppose your
Mapserver support WFS 2.0 otherwise you may need `text/xml;
subtype=gml/2.1.2` or `text/xml; subtype=gml/3.1.1`

In all case, before you try the part

```
.then(function (json) {
  ...
  ...
})
```

To troubleshot, you may change you code to

```
fetch(`http://blabla/cgi-bin/mapserv.exe?MAP=c:/.../naasp_ol.map`, {
    method: 'POST',
    body: new XMLSerializer().serializeToString(featureRequest),
})
.then(function (resp) {
    return resp.text();
})
.then(function (text) {
    // See potential errors message
    console.log(text)
})
```

To troubleshot, most of the time, I go "outside" of my OpenLayers code.
OpenLayers generates magically a payload to send to a Mapserver endpoint.
In fact, except, the syntax is in JS, the first issue is about sending the
"right" content to this endpoint.

If you go to
https://openlayers.org/en/latest/examples/vector-wfs-getfeature.html, open
your browser Developper Tools, use the Network panel, filter to get the
post http call and use the "Copy as curl"
https://everything.curl.dev/usingcurl/copyas, you will see the exact POST
content send to Mapserver endpoint

With this, you can simply debug by changing the content and run CURL in the
command line. Not sure it's more easy than JS but at least you will avoid
the JS language barrier to make your http calls to Mapserver endpoint.

Regards,

Thomas Gratier


Le mer. 26 oct. 2022 à 12:30, mathias cunault <mathias.cunault at inrap.fr> a
écrit :

> Mapserver 7.6.0 & OL6
>
> My goal is to find a feature in a WFS layer and zoom on it using
> openLayers.
> Following this example
> https://openlayers.org/en/latest/examples/vector-wfs-getfeature.html, I
> am trying to do the same with Mapserver.
> I am not very familiar with Javascript so I feel uncomfortable in this
> exercise.
> I wrote :
> const featureRequest = new ol.format.WFS().writeGetFeature({
>         srsName: 'EPSG:3857',
>         featureNS: 'http://openstreemap.org',
>         featurePrefix: 'osm',
>         featureTypes: ['emprise_3857'],
>         outputFormat: 'application/json',
>         filter: new ol.format.filter.equalTo('numoa', '25366'),
> })
>
> console.log(featureRequest)
>
> fetch(`http://blabla/cgi-bin/mapserv.exe?MAP=c:/.../naasp_ol.map`, {
>     method: 'POST',
>     body: new XMLSerializer().serializeToString(featureRequest),
> })
> .then(function (rep) {
>     return rep.json();
> })
> .then(function (json) {
>     const features = new ol.format.GeoJSON().readFeatures(json)
>     new ol.source.Vector.addFeatures(features)
>     map.getView().fit(new ol.source.Vector.getExent())
> })
> But the chrome console says that there is an issue with the promise that
> it is not a valid JSON.
> Is it coming from the fetch I wrote in a bad way or a problem with
> application/json ?
> Indeed I read in Mapserver doc that only mapserver 8+ could handle
> application/json (from what I understood)
>
> Maybe it is both? I feel a bit lost.
> I tried to find more examples and doc on the net, but nothing clear for
> me. Maybe you can help?
> Thanks
>
>
> *----------*
> *Mathias Cunault*
>
> *référent SIG / Admin Caviar*
>
> *Inrap Tours - 148 av. Maginot37000 TOURS06 32 05 98 96*
> abonnez-vous à la lettre d'information de l'Inrap : http://ww
> <http://www.inrap.fr/newsletter.php>w.inrap.fr/newsletter.php
> <http://www.inrap.fr/newsletter.php>
> _______________________________________________
> MapServer-users mailing list
> MapServer-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/mapserver-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-users/attachments/20221030/a88b6d09/attachment.htm>


More information about the MapServer-users mailing list