Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created August 24, 2011 19:03
Show Gist options
  • Save springmeyer/1168887 to your computer and use it in GitHub Desktop.
Save springmeyer/1168887 to your computer and use it in GitHub Desktop.
var wgs2merc = function(lon,lat) {
lon = parseFloat(lon);
lat = parseFloat(lat);
var x = lon * 20037508.34 / 180;
var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return [x,y];
}
@AdrianRossouw
Copy link

I am running into some issues with this.

console.log(
     [doc.lon, doc.lat],  // the original wgs points
    wgs2merc(doc.lon, doc.lat),  // the merc points with this function
    proj.forward([doc.lon, doc.lat]) // the merc points from mapnik
);

These are some of the results.

[ '-104.9923', '39.74001' ] [ -11687689.371587677, -6491423.869377617 ] [ -11687689.373214617, 4828232.857062782 ]
[ '-103.1974', '40.63698' ] [ -11487882.017590644, -6336684.282449202 ] [ -11487882.01918977, 4958942.857859804 ]
[ '-103.5433', '37.97146' ] [ -11526387.429450678, -6805287.497430911 ] [ -11526387.431055164, 4575394.852208842 ]
[ '-105.0303', '39.7877' ] [ -11691919.512237232, -6483123.962506243 ] [ -11691919.513864761, 4835139.212464451 ]
[ '-104.5809', '38.30399' ] [ -11641892.5330817, -6745345.698014055 ] [ -11641892.534702264, 4622458.858854637 ]
[ '-122.0542', '37.97832' ] [ -13587011.391289044, -6804046.4251634665 ] [ -13587011.393180372, 4576363.608526837 ]
[ '-104.5137', '37.17283' ] [ -11634411.863301434, -6951092.813871358 ] [ -11634411.864920955, 4463224.517928745 ]
[ '-104.8895', '39.01105' ] [ -11676245.727935722, -6619339.337465406 ] [ -11676245.729561068, 4723254.513887428 ]
[ '-105.2677', '40.00346' ] [ -11718346.759347878, -6445676.65063133 ] [ -11718346.760979084, 4866445.090013317 ]
[ '-104.9594', '39.67668' ] [ -11684026.960341088, -6502458.5960280355 ] [ -11684026.961967519, 4819068.9359239 ]
[ '-106.9178', '38.54679' ] [ -11902035.051080288, -6701856.2786959605 ] [ -11902035.052737065, 4656959.487782303 ]
[ '-72.92149', '41.33078' ] [ -8117583.133556815, -6218920.194063545 ] [ -8117583.134686791, 5061254.750090784 ]
[ '115.8815', '-31.93685' ] [ 12899869.570565056, NaN ] [ 12899869.57236073, -3755024.0598698533 ]
value larger than maximum allowed value

@AdrianRossouw
Copy link

i resolved it with parseFloat, but maybe that should be part of the function itself ?

@springmeyer
Copy link
Author

sure, parseFloat sounds good.

@springmeyer
Copy link
Author

also, heads up that mapnik's proj.forward may not be fully reliable. Ideally you should use mapnik's ProjTransform.forward not Projection.forward. Only the latter however is currently exposed in node-mapnik. I need to expose the ProjTransform. The problem with Projection.forward is that is makes assumptions about the source projection that may not be as robust as setting up the from->to relationship that is done in ProjTransform.

@springmeyer
Copy link
Author

I'll knock out getting this exposed now: mapnik/node-mapnik#57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment