0

Back in the day of OpenLayers 2 displaying a WMS layer involved something like:

var map = new OpenLayers.Map(
    'map',
    {maxExtent: new OpenLayers.Bounds(
         -20037500.000000,-13347182.804674,
          20037500.000000,13347182.804674),             
     maxResolution: 66903.171953}
);
var mslayer = new OpenLayers.Layer.MapServer( 
    "MapServer Layer",
    "http://localhost/cgi-bin/mapserv?map=/mapfiles/countries.map&",
    {layers: 'Countries'},
    {singleTile: "true", ratio:1} 
);
map.addLayer(mslayer);
map.zoomToMaxExtent();

No explicit CRS was declared, and thus the server would respond with the default CRS of the layer.

In OpenLayers 5 a View object is required, in which a projection property is available. In case this property is not set up, all requests made to server explicitly declare the CRS identified with "EPSG:3857" (whether the service supports it or not). To avoid this a CRS must be explicitly declared, registered with proj4js and then passed to the View object:

var Map                 = ol.Map;        //~ import Map from 'ol/Map.js';
var View                = ol.View;       //~ import View from 'ol/View.js';
var TileLayer           = ol.layer.Tile; //~ import TileLayer from 'ol/layer/Tile.js';
var OSM                 = ol.source.OSM; //~ import OSM from 'ol/source/OSM.js';
var TileWMS           = ol.source.TileWMS; //~ import TileWMS from 'ol/source/TileWMS.js';  
var proj4             = proj4;             //~ import proj4 from 'proj4';
var register            = ol.proj.proj4.register; //~ import {register} from 'ol/proj/proj4';
      var getProjection       = ol.proj.get;     //~ import {get as getProjection} from 'ol/proj';

proj4.defs('EPSG:152160',
        '+proj=igh +lat_0=0 +lon_0=0 +datum=WGS84 +units=m +no_defs');
register(proj4);
const homolosine = getProjection('EPSG:152160');

var layers = [
    new TileLayer({
      extent: [-1.99496e+07, -8.68326e+06, 1.99675e+07, 8.41393e+06],
      source: new TileWMS({
        url: 'http://localhost/cgi-bin/mapserv?map=/mapfiles/countries.map',
        params: {'LAYERS': 'Countries', 'TILED': true},
        serverType: 'mapserver',
        // Countries have transparency, so do not fade tiles:
        transition: 0
      })
    })
  ];
var map = new Map({
    layers: layers,
    target: 'map',
    view: new View({
      projection: homolosine,
      center: [0, 18000],
      zoom: 4
    })
  });

The problem is that proj4js does not support the same CRSs as PROJ or GDAL. It is easy to arrive at a situation where a layer provided by a server like Mapserver in a certain CRS can not be dealt with in OpenLayers 5.

I would like to understand whether this situation can be circumvented in OpenLayers 5 (the tutorials do not go further than the example above). Hypothetically, this could be achieved in two ways:

a) suppress the explicit CRS declaration and let the server reply with the default CRS of the layer;

b) declare the request CRS using a simple string, avoiding a proj4js registered CRS.

3
  • No explicit CRS was declared, and thus the server would respond with the default CRS of the layer. assuming the layer was offered by the server, otherwise you'd get no map
    – nmtoken
    Aug 12 '19 at 9:24
  • Your first example does not use MapServer as WMS but as a native CGI service dev.openlayers.org/apidocs/files/OpenLayers/Layer/…. WMS does not have default CRS but GetMap must always contain &CRS= parameter. See also stackoverflow.com/questions/38464074/….
    – user30184
    Aug 12 '19 at 15:28
  • 1
    OpenlLayers always passed a CRS to the server. You can specify projection as an option the TileWMS constructor. If none is specified OpenLayers will request the View projection (which defaults to EPSG:3857). Whatever is used needs to be known to both the server and OpenLayers (e.g. via proj4). When the TileWMS and View projections differ OpenLayers will perform client repeojection.
    – Mike
    Aug 12 '19 at 16:27

Your Answer

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

Browse other questions tagged or ask your own question.