Skip to content

Instantly share code, notes, and snippets.

@Artoria2e5
Last active July 14, 2021 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Artoria2e5/bbcfdf468781be38e9aa6b6d65f1f16c to your computer and use it in GitHub Desktop.
Save Artoria2e5/bbcfdf468781be38e9aa6b6d65f1f16c to your computer and use it in GitHub Desktop.
Baidu's Twist on Web Mercator (as shown at https://github.com/GeeBar/coord/pull/3)
// Reference value:
// http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
// type 5 (lon,lat) -> type 6 (x,y)
// js source, presumably same as ref?
// https://github.com/GeeBar/coord/blob/master/mercator.js
var BD_EXTENT_LON = 20037726.372144807 // ? - lon only; lat goes less
var BD_EXTENT_LAT = 12474104.1741418
// Their js truncated at 74, but included taylor terms up to 75~90 (82.5).
// These terms are severly screwed thanks to their misunderstanding of taylor.
// I mean, what can I say? They asked for it.
// Probably they did not use Taylor but used some regression....
var MAXLAT = 74
// They chose to do some more stretching. Wow.
// This still gives some systematic error of ~60m north over China.
// Wonder if I should hack it to only look at [20, 50],
// Or go for quartic.
var bd__lat_k = function(lat) {
return lat === undefined?
0.99553200024739:
6.32801629192256e-7 * Math.pow(lat, 2) + 0.993256068032876 // R^2 = 0.996
}
// bdMerc(35,105)
// --> {x: 11688673.717084471, y: 4139073.0156218447}
// ref {x: 11688673.714402, y: 4139145.6551144}
// bdMerc(31,121)
// --> {x: 13469804.950164009, y: 3610498.571939349}
// ref {x:13469804.946737, y:3610540.1614335}13469804.946737
// bdMerc(0, 0)
// --> {x: 0, y: -7.033476257922071e-10}
// ref {x: -0.00032181358786131, y: 0.014750554316643}
function bdMerc(lat, lon) {
return {
x: lon * BD_EXTENT_LON / 180,
y: Math.log(Math.tan((45 + lat / 2) * Math.PI / 180)) / (Math.PI) * BD_EXTENT_LON * bd__lat_k(lat),
}
}
// mercBD(11688673.717084471, 4139073.0156218447)
// --> {lat: 34.99999991392119, lon: 105}
// mercBD(11688673.714402, 4139145.6551144)
// --> {lat: 35.00053690531776, lon: 104.99999997590322}
// ref {lon: 105, lat: 34.999999945371}
function mercBD(x, y) {
if (Math.abs(y) > BD_EXTENT_LAT)
console.warn("Traveller, what are you doing with this arctic land? Go back to your 74°-minus zone!")
var lon = x / BD_EXTENT_LON * 180
var lat = (Math.atan(Math.exp(y / bd__lat_k() / BD_EXTENT_LON * Math.PI)) / Math.PI * 180 - 45) * 2
lat = (Math.atan(Math.exp(y / bd__lat_k(lat) / BD_EXTENT_LON * Math.PI)) / Math.PI * 180 - 45) * 2
lat = (Math.atan(Math.exp(y / bd__lat_k(lat) / BD_EXTENT_LON * Math.PI)) / Math.PI * 180 - 45) * 2
return {
lat: lat,
lon: lon,
}
}
@Artoria2e5
Copy link
Author

okay the real answer is at gumblex/cntms@bbde400

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