Using OpenLayers library can be challenging for many reasons, one of them being the number of ways it can be integrated into your application depending on your framework. And there are so many versions on the web it can be difficult to search for appropriate examples or help in debugging.

Besides, GIS is an exciting domain yet also challenging, especially when combined with web development. Managing data sources in different spatial reference systems to display them in a web page is an interesting subject to discuss. For example, OpenLayers 6 embeds EPSG:3857 (WGS84/Pseudo-Mercator projected system) and EPSG:4326 (WGS 84 geographic system) systems only so you have to get your hands a bit dirty if you work with a custom projection system.

Here, I demonstrate the display of some GeoJSON data in Lambert-93 coordinate system (EPSG:2154, used for Metropolitan France), the definition of this CRS using Proj4, and the integration with OpenLayers 6 in a vanilla JS script.

The minimal HTML excerpt to load OpenLayers 6 and Proj4, while having a div to integrate map canvas:

And the vanilla JS script:

• lines 1-7 provide a wrapper for Proj4 declaration
• lines 9 and 10 are mandatory (omitting line 10 is a frequent mistake) to define and register the custom projection
• once defined and registered, one can access the new projection by calling the string code, here EPSG:2154, as demonstrated on line 12
• geojsonObject is a GeoJSON formatted string with a single feature, provided as a matter of example
• lines 22 to 25 are important: geojsonObject features are read by specifying dataProjection (i.e. input data projection system, here : EPSG:2154), and reprojecting on the fly to a destination CRS specified using featureProjection (here EPSG:3857)
• features are then loaded as a vector layer using basic OpenLayers methods (lines 27 to 41)
• finally, the map and view are instantiated while specifying the view projection, here EPSG:3857. The CRS defined here must be the same as the one declared in featureProjection above.

Note: one can test the same minimal example without using reprojection on the fly. Then, the view projection must be set as lambert_proj (aka EPSG:2154), and featureProjection can be omitted.