I just stumbled upon the following problem :

OpenLayers is unable to retrieve a projection definition even though it has been previously registered with Proj4JS. Code below :

   import proj4 from 'proj4/dist/proj4'; // Proj4JS version 2.4.4

   import proj from 'ol/proj'; // OpenLayers version 4.6.5

   proj4.defs('EPSG:2154', '+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');

   console.log(proj.get('EPSG:2154')); // displays null

I can not figure out anything but a version mismatch as reported in OpenLayers 3 not recognising proj4 custom projection


Just found the answer on OpenLayers Github

The reason is the 'import' syntax used to bring OL and Proj4JS into scope. With this syntax, one has to make sure OL is aware of Proj4JS by using the ol.proj.setProj4 function.

Correct code is :

import proj4 from 'proj4/dist/proj4';
import proj from 'ol/proj';

proj.setProj4(proj4);    // <=== Add this line

proj4.defs('EPSG:2154', '....whathever....');
var proj2154 = proj.get('EPSG:2154'); // not null now

Previous answer is right. I want only note that in Openlayers 5 this is changed. As noted at https://openlayers.org/en/latest/apidoc/module-ol_proj_Projection-Projection.html, in similar cases you must use

proj4.defs('EPSG:2154', '+proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');

ol.proj.proj4.register(proj4); // register proj4 in openlayer after a proj4.defs

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.