UNPKG

wgs2mars

Version:

a little function to transform longitude and latitude from WGS-84 To GCJ-02

88 lines (69 loc) 2.6 kB
/** * Usage: let gcjloc = transformFromWGSToGCJ(lng,lat); * Source: https://github.com/hiwanz/wgs2mars.js.git */ import chinaBorderData from './border-data'; function transformFromWGSToGCJ(wgLon, wgLat) { // const PI let PI = 3.14159265358979324; // Krasovsky 1940 // // a = 6378245.0, 1/f = 298.3 // b = a * (1 - f) // ee = (a^2 - b^2) / a^2; let a = 6378245.0; let ee = 0.00669342162296594323; function isInChina(point, vs) { // ray-casting algorithm based on // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html let x = point[0], y = point[1]; let inside = false; for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) { let xi = vs[i][0], yi = vs[i][1]; let xj = vs[j][0], yj = vs[j][1]; let intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi); if (intersect) inside = !inside; } return inside; }; function transformLat(x, y) { let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0; return ret; } function transformLon(x, y) { let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0; return ret; } // World Geodetic System ==> Mars Geodetic System function transform(wgLon, wgLat) { let mgLoc = {}; if (!isInChina([wgLon, wgLat], chinaBorderData)) { mgLoc = { lat: wgLat, lng: wgLon }; return mgLoc; } let dLat = transformLat(wgLon - 105.0, wgLat - 35.0); let dLon = transformLon(wgLon - 105.0, wgLat - 35.0); let radLat = wgLat / 180.0 * PI; let magic = Math.sin(radLat); magic = 1 - ee * magic * magic; let sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI); dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI); mgLoc = { lat: wgLat + dLat, lng: wgLon + dLon }; return mgLoc; } return transform(wgLon, wgLat); } export default transformFromWGSToGCJ;