projection.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. export interface LonLatPoint {
  2. lon: number
  3. lat: number
  4. }
  5. export interface WebMercatorPoint {
  6. x: number
  7. y: number
  8. }
  9. export interface WorldTilePoint {
  10. x: number
  11. y: number
  12. }
  13. const MAX_LATITUDE = 85.05112878
  14. const EARTH_RADIUS = 6378137
  15. function clampLatitude(lat: number): number {
  16. return Math.max(-MAX_LATITUDE, Math.min(MAX_LATITUDE, lat))
  17. }
  18. export function lonLatToWebMercator(point: LonLatPoint): WebMercatorPoint {
  19. const latitude = clampLatitude(point.lat)
  20. const lonRad = point.lon * Math.PI / 180
  21. const latRad = latitude * Math.PI / 180
  22. return {
  23. x: EARTH_RADIUS * lonRad,
  24. y: EARTH_RADIUS * Math.log(Math.tan(Math.PI / 4 + latRad / 2)),
  25. }
  26. }
  27. export function webMercatorToLonLat(point: WebMercatorPoint): LonLatPoint {
  28. return {
  29. lon: point.x / EARTH_RADIUS * 180 / Math.PI,
  30. lat: (2 * Math.atan(Math.exp(point.y / EARTH_RADIUS)) - Math.PI / 2) * 180 / Math.PI,
  31. }
  32. }
  33. export function lonLatToWorldTile(point: LonLatPoint, zoom: number): WorldTilePoint {
  34. const latitude = clampLatitude(point.lat)
  35. const scale = Math.pow(2, zoom)
  36. const latRad = latitude * Math.PI / 180
  37. return {
  38. x: (point.lon + 180) / 360 * scale,
  39. y: (1 - Math.log(Math.tan(latRad) + 1 / Math.cos(latRad)) / Math.PI) / 2 * scale,
  40. }
  41. }
  42. export function worldTileToLonLat(point: WorldTilePoint, zoom: number): LonLatPoint {
  43. const scale = Math.pow(2, zoom)
  44. const lon = point.x / scale * 360 - 180
  45. const latRad = Math.atan(Math.sinh(Math.PI * (1 - 2 * point.y / scale)))
  46. return {
  47. lon,
  48. lat: latRad * 180 / Math.PI,
  49. }
  50. }
  51. const CHINA_AXIS = 6378245
  52. const CHINA_EE = 0.00669342162296594323
  53. function isOutsideChina(point: LonLatPoint): boolean {
  54. return point.lon < 72.004 || point.lon > 137.8347 || point.lat < 0.8293 || point.lat > 55.8271
  55. }
  56. function transformLat(x: number, y: number): number {
  57. let result = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
  58. result += (20 * Math.sin(6 * x * Math.PI) + 20 * Math.sin(2 * x * Math.PI)) * 2 / 3
  59. result += (20 * Math.sin(y * Math.PI) + 40 * Math.sin(y / 3 * Math.PI)) * 2 / 3
  60. result += (160 * Math.sin(y / 12 * Math.PI) + 320 * Math.sin(y * Math.PI / 30)) * 2 / 3
  61. return result
  62. }
  63. function transformLon(x: number, y: number): number {
  64. let result = 300 + x + 2 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
  65. result += (20 * Math.sin(6 * x * Math.PI) + 20 * Math.sin(2 * x * Math.PI)) * 2 / 3
  66. result += (20 * Math.sin(x * Math.PI) + 40 * Math.sin(x / 3 * Math.PI)) * 2 / 3
  67. result += (150 * Math.sin(x / 12 * Math.PI) + 300 * Math.sin(x / 30 * Math.PI)) * 2 / 3
  68. return result
  69. }
  70. export function gcj02ToWgs84(point: LonLatPoint): LonLatPoint {
  71. if (isOutsideChina(point)) {
  72. return point
  73. }
  74. const dLat = transformLat(point.lon - 105, point.lat - 35)
  75. const dLon = transformLon(point.lon - 105, point.lat - 35)
  76. const radLat = point.lat / 180 * Math.PI
  77. const magic = Math.sin(radLat)
  78. const sqrtMagic = Math.sqrt(1 - CHINA_EE * magic * magic)
  79. const latOffset = (dLat * 180) / ((CHINA_AXIS * (1 - CHINA_EE)) / (sqrtMagic * sqrtMagic * sqrtMagic) * Math.PI)
  80. const lonOffset = (dLon * 180) / (CHINA_AXIS / sqrtMagic * Math.cos(radLat) * Math.PI)
  81. return {
  82. lon: point.lon - lonOffset,
  83. lat: point.lat - latOffset,
  84. }
  85. }