Reputation: 35
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 which gives me the following result:
The geometry should be at the bottom of the country. I think VectorSource has a default srsname, but I don't 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 doesn't 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 don't understand how.
My VectorSource has format -> dataProjection -> code : "EPSG:4326" , units : degrees
Now I'm trying to figure how to put everything with the same projection but I'm struggling 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
});
Upvotes: 0
Views: 798
Reputation: 677
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',
})
});
Upvotes: 0