Search code examples
javascriptleafletcoordinatesgeoserverepsg

Proj4: How to convert coordinates from EPSG 4326 to EPSG 31370


Need to convert coordinates of Leaflet.js Map from EPSG 4326 to EPSG 31370.

What I did till now: Defining EPSG 31370

proj4.defs["EPSG:31370"] = "+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-106.8686,52.2978,-103.7239,0.3366,-0.457,1.8422,-1.2747 +units=m +no_defs";

And Calling it:

console.log(proj4('EPSG:31370', [map.getCenter().lat,map.getCenter().lng]));

However, it results into '31370 undefined' error in console.


Solution

  • The problem is with your initial definitions for EPSG 31370. The following works.

    // Define the source and target projections
    proj4.defs("EPSG:4326", "+proj=longlat +datum=WGS84 +no_defs");
    proj4.defs("EPSG:31370", "+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-106.8686,52.2978,-103.7239,0.3366,-0.457,1.8422,-1.2747 +units=m +no_defs");
    
    // Define the coordinates to convert
    const sourceCoord = [1, 1];
    
    // Convert the coordinates
    const targetCoord = proj4("EPSG:4326", "EPSG:31370", sourceCoord);
    
    console.log(targetCoord);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.8.1/proj4.min.js" integrity="sha512-JToqu2hzfiepDTg9LjkVzYhUhTcYGJIj2aGOn3Q+7sPVSi2n+vmpxISCbOJ2b3I4OQmqG0KQYovX6f3Rx+etAQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

    After getting these conversion results, you can verify them here.