1

I have two layers in my OpenLayers map, the background in WMS and GeoJSON features in a vector layer, both in ESPG:3006. (And a standard OpenStreetMap background just to see which one of the layers that shows correct).

The problem is that the default projection is ESPG:4326, and if I specify the projection in the view of the map-object to ESPG:3006, the vector layer is correct but the WMS doesn't appear at all. To see the WMS layer, the projection needs to be specified for the WMS layer and the map will automatically be in correct projection, but then the vector layer appears in wrong position at the map.

What can I do to get both layers in correct projection and position?

const standardmap = new ol.layer.Tile({
            source: new ol.source.OSM()
        });

const WMSmap = new ol.layer.Tile({
            source: new ol.source.TileWMS({
                url: 'xxxxxxx',
                params: {
                    'LAYERS': '0',
                    'CRS': 'EPSG:3006',
                    'VERSION': '1.3.0',
                    'FORMAT': 'image/png'
                }
            })
        });

const geoJsonFeatures = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
            }),
            style: styleFunction
        });
let map = new ol.Map({
            target: 'map',
            layers: [standardmap, WMSmap, geoJsonFeatures],
            view: new ol.View({
                center: center,
                zoom: 5
            })
        });

The small red figures should be somewhere in "Region Mi...", not in Netherlands.

enter image description here

2

OpenLayers sets the CRS/SRS of a WMS from the projection specified in the source options, or if none is specified from the view projection so anything you put in params would be overriden (the API says WIDTH, HEIGHT, BBOX and CRS (SRS for WMS version < 1.3.0) will be set dynamically):

        source: new ol.source.TileWMS({
            url: 'xxxxxxx',
            params: {
                'LAYERS': '0',
                'VERSION': '1.3.0',
                'FORMAT': 'image/png'
            },
            projection: 'EPSG:3006',
        })

When using readFeatures dataProjection and featureProjection must be specified if the view projection is different from the data in the geojson

const geoJsonFeatures = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(geojsonObject, {
                    dataProjection: 'EPSG:3006',
                    featureProjection: map.getView().getProjection()
                })
            }),
            style: styleFunction
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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