0
votes

I'm currently using WFS with Openlayers but the layers is at the wrong position, if i'm right the problem come from the srsName but how to change it ?

Here is my code for the WFS :

var WFSSource = new VectorSource({
  format: new GeoJSON(),
  url: function(extent) {
    return 'http://localhost:8081/geoserver/occi/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=occi%3Aocci&maxFeatures=5&outputFormat=application%2Fjson';
  },
  strategy: bboxStrategy,
});

var WFSLayer = new VectorLayer({
  source: WFSSource
});

I had the same issue with WMS but i just changed the SRC natif on Geoserver ( EPSG:2154 ) and everything went fine. Now i have the same issue wich give me the following result Image

The geometry should be at the bottom of the country. I think VectorSource has a default srsname but i dont know how to change it. Btw on the this link : https://openlayers.org/en/latest/examples/vector-wfs.html they show how to use WFS and he have this line of code :

url: function(extent) {
return 'https://ahocevar.com/geoserver/wfs?service=WFS&' +
    'version=1.1.0&request=GetFeature&typename=osm:water_areas&' +
    'outputFormat=application/json&srsname=EPSG:3857&' +
    'bbox=' + extent.join(',') + ',EPSG:3857';

},

where he define the srsName but dosnt work for me.

----- Update -----

After checking the projection here is what i found :

  • My Map has default projection -> code : "EPSG:3857" , units : meters

  • My TileWMS has no settings but the documentation say that the params are set dynamically. The things is on Geoserver i use "EPSG:2154" but the Map and this TileWMS match perfectly i dont understand how.

  • My VectorSource has format -> dataProjection -> code : "EPSG:4326" , units : degrees

Now im trying to figure how to put everything with the same projection but im strugeling a lot.

If i change the map projection with "EPSG:2154" ( the native SRC of my layer in Geoserver ) i have the following error : Cannot read property 'getExtent' of null.

And if i try to change my VectorSource projection nothing happen, my VectorSource stay in "EPSG:4326"

var WFSSource = new VectorSource({
  format: new GeoJSON(),
  url: function(extent) {
    return 'http://localhost:8081/geoserver/occi/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=occi%3Aocci&maxFeatures=5&outputFormat=application%2Fjson';
  },
  projection : "EPSG:2154",
  strategy: bboxStrategy
});
1
could you give us a clue as to what is happening, may be a picture of the map? Most likely issue is your data is in degrees and your map is in metres, if so reproject the data - Ian Turton

1 Answers

0
votes

ol/Source and ol/View have projection. Default projection is EPSG:3857. If your data or basemap is diffrent then EPSG:3857 you should define it.

import Map from 'ol/Map';
import View from 'ol/View';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';

const map = new Map({
  layers: [
    new VectorLayer({
        source: new VectorSource({
            projection: 'EPSG:3857',
        })
    })
  ],
  target: 'map',
  view: new View({
    projection: 'EPSG:3857',
  })
});