Search code examples
openlayersopenlayers-6proj4js

Using a specific nadgrid with Openlayers 6 / Proj4js


I have an issue with reprojecting vector data from one coordinate system to another by using proj4js.

The data source uses a Gauss Kruger 2 Projection (EPSG: 31466) and I want to convert it to EPSG:3857 and display it. Generally it's working fine, but there is a small offset (maybe ~ 1-2 meters) compared to a WMS Service that is displaying areal photos in the background.

Adding these layers in QGIS3 has the same output, but there I can switch the datum-transformation of the vector layer from

+towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7

to

+nadgrids=BETA2007.gsb

and the output looks as expected. DE_DHDN (BeTA, 2007) nach ETRS89

But unfortunatly I'm failing at using / embedding this grid with proj4/ol. Is there any way to use it with these frameworks or an example with a similar approach (i.e. with other coordinate systems)? My googling skills have come to an end. :)

Funfacts:

  • Angular 9.1.4
  • Openlayers 6.3.1
  • Proj4 2.6.2
  • Reprojecting from an EPSG:25832 source works perfectly

tl;dr: Is it possible to use a specific nadgrid in Openlayers/Proj4js?

Greetings


Solution

  • Proj4js does support grid transforms in recent versions.

    To use +nadgrids= in a proj definition, first read your NTv2 .gsb file (e.g. from https://github.com/OSGeo/proj-datumgrid) into an ArrayBuffer, then pass it to proj4.nadgrid. E.g:

    const buffer = fs.readFileSync('ntv2.gsb').buffer
    proj4.nadgrid('key', buffer);
    

    Then use the given key in your proj4 definition string, e.g.

    +nadgrids=@key,null
    

    See https://www.npmjs.com/package/proj4#grid-based-datum-adjustments.