Toggle menu

proj4js

Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.

This library is distributed under the MIT license, its source and full documentation can be found at https://github.com/proj4js/proj4js (opens new window).

Example

This example takes long/lat coordinates in Plymouth and converts them to British National Grid.

It uses the proj4.defs method to define a projection (you can find definitions in open source projects online, for example https://epsg.io/27700 includes Proj4js in its list of export examples).

Note that by default proj4 uses the axis order [x=longitude,y=latitude] for geographic coordinates.

See the documentation linked to above for lots more information.

function(params, credentials) {
    const proj4 = require("proj4");
    proj4.defs("EPSG:27700", "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs");
    var osgb = [];
    var coordinates = [-4.091939, 50.423508];
    osgb = proj4("EPSG:27700", coordinates);
    return osgb;
}

Which returns:

{
    "jsonrpc": "2.0",
    "id": 38,
    "result": [251496.02605062764, 60274.98882692904]
}

Last modified on June 10, 2022

Share this page

Facebook icon Twitter icon email icon

Print

print icon