Coordinate Reference System Transformations in Javascript

Yes, you can do coordinate reference system transformations in Javascript. (I know, I’m shocked also.) I found proj4js on GitHub, which is a port of an older project, PROJ.4. The proj4js library is very easy to use. All you need to do is find the specification strings at of the two projections/datums you are interested in and you’re ready to go.


Assuming you’ve already installed node, installation is super easy:

$ node install proj4js -g

This will install proj4js globally on your system. If you don’t want that, then leave off the -g flag.

A Working Example

The only weird thing is that you need to pass a longitude first, and then latitude, but I’ve noticed this behavior in other GIS software. This makes sense when you think of Cartesian coordinates where the x-axis is specified first, and then the y-axis.

// import proj4js
var proj4 = require("proj4")

// define the projections
var nad27 = "+proj=longlat +ellps=clrk66 +datum=NAD27 +no_defs"
var wgs84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

// define the coordinate
var lat = 32.0
var lon = -102.6

// then you can pass the point as a list 
var result = proj4( wgs84, nad27, [ lon, lat ] )
var result = proj4( wgs84, nad27 ).forward( [ lon, lat ] )
// [ -102.6, 32.0019256199253 ]

// or you can pass the point as an object
var result = proj4( wgs84, nad27, { x:lon, y:lat } )
var result = proj4( wgs84, nad27 ).forward( { x:lon, y:lat } )
// { x: -102.6,
//   y: 32.0019256199253,
//   z: -2.7488031471148133 }