Lightweight coordinate transformations in JavaScript

We (as in: people that work with geospatial JavaScript) have all been there: We have coordinates as “latlons” (i.e. Geographic coordinates in WGS84), but we need to transform them to some other coordinate system (as a Norwegian: replace “some other” with UTM Zone 33N). Ok, that shouldn’t be that difficult, just include proj4.js. It’s a mere 73.275 kb, and handles almost all coordinate transformations (given that you know their definitions).

But, sometime you just care about one single transformation (say: EPSG:4326 to EPSG:32633), and you are really concerned about file size (maybe you are targeting a mobile site), because size does matter and you feel that proj4.js weighs in a bit too heavy. Well, that was what Bjørn Sandvik thought yesterday:

Then I got around to thinkning: how hard can this be? After a bit of thinking I realized that, yes, this is actually rather complicated in terms of maths. But, after a bit more thinking, I remembered this project from a couple of years ago (blog post in Norwegian). Basically, my old department at NTNU had some fortran code from around 1990 written by professor Jon Holsen in Fortran.

A couple of years ago I ported that code to JavaScript (on GitHub), but did not think much more about it (although I heard reports that the department has used it some).

After looking at old notes and books on coordinate transformations it dawned on me that Holsen.js has a “bl_to_xy”-function, which transforms Geographic to planar coordinates. I then ripped out the relevant code, removed things that wasn’t needed for my simple WGS84 to UTM33 conversion, looked up the right params for UTM33N and made a gist in the middle of the night.

I did some testing against proj4js, and Bjørn did some more (reporting: “similar down to the centimetre”), and after figuring out that the original code used X for North and Y for East it seems to do the work. Result: a js file of about 100 lines, and that is rather readable lines as well. Running this code through a minifier (uglify.js) results in a 8 kb file (that is, about 10% of proj4js).

The code is tailored to transforming to one specific UTM Zone, but it should be easy to modify to work for other zones as well. Another idea is to make a similar script for transforming the other way, from UTM33 to Geographic coordinates. Holsen.js has support for this, so it shouldn’t be that difficult.

In closing: I think that small, simple scripts like this (which is small in size and does _one_ thing) is much more needed in JavaScript than in other languages. Download size matters for web apps, and the idea of one, monolithic library to solve all conceivable tasks does not work for JavaScript the way it does for Java and similar languages. Also: beeing able to provide a fellow geo-geek with working code in the course of a couple of hours is always cool! In addition: nice to see that work I did mostly for fun two years ago can be of use to someone.

Leave a Reply

Your email address will not be published. Required fields are marked *