All files / src geo.js

0% Statements 0/36
100% Branches 0/0
0% Functions 0/13
0% Lines 0/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                                                                             
import { earthRadius } from './config'
 
export function lon2tile(lon, zoom) {
  return Math.floor(((lon + 180) / 360) * Math.pow(2, zoom))
}
export function lat2tile(lat, zoom) {
  return Math.floor(
    ((1 -
      Math.log(
        Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)
      ) /
        Math.PI) /
      2) *
      Math.pow(2, zoom)
  )
}
export function tile2long(x, z) {
  return (x / Math.pow(2, z)) * 360 - 180
}
export function tile2lat(y, z) {
  var n = Math.PI - (2 * Math.PI * y) / Math.pow(2, z)
  return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))
}
export function latLngToTileXY(lat, lng, zoom) {
  const xTile = lon2tile(lng, zoom)
  const yTile = lat2tile(lat, zoom)
  return {
    x: xTile,
    y: yTile
  }
}
 
export function tileXYZToLatLng(x, y, zoom) {
  const longitude = tile2long(x, zoom)
  const latitude = tile2lat(y, zoom)
  return { lat: latitude, lng: longitude }
}
 
export function tileToCorners(x, y, zoom, lat, lng) {
  const tileAltitude = 0
  const tileLatLng = tileXYZToLatLng(x, y, zoom)
  const deltaLat = tileLatLng.lat - lat
  const deltaLng = tileLatLng.lng - lng
  let { x: x_mm, y: y_mm } = deltaLatLngToDistance(deltaLat, deltaLng, lat)
  let size =
    ((earthRadius * 2000 * Math.PI) / Math.pow(2.0, zoom)) *
    Math.cos((lat * Math.PI) / 180)
  let corner0 = {
    x: x_mm,
    y: y_mm,
    z: tileAltitude
  }
  let corner1 = {
    x: x_mm + size,
    y: y_mm,
    z: tileAltitude
  }
  let corner2 = {
    x: x_mm + size,
    y: y_mm - size,
    z: tileAltitude
  }
  let corner3 = {
    x: x_mm,
    y: y_mm - size,
    z: tileAltitude
  }
  return [corner3, corner0, corner1, corner2]
}
export function distanceToDeltaLatLng(x, y, latitude) {
  //x,y,z in ENU coords
  let lat = ((180 / Math.PI) * (y / 1000)) / earthRadius
  let lng =
    ((180 / Math.PI) * (x / 1000)) /
    earthRadius /
    Math.cos((latitude * Math.PI) / 180)
  return { lat, lng }
}
export function deltaLatLngToDistance(deltaLat, deltaLng, latitude) {
  //x,y,z in ENU coords
  let y = (earthRadius * deltaLat * 1000) / (180 / Math.PI)
  let x =
    (earthRadius * deltaLng * 1000 * Math.cos((latitude * Math.PI) / 180)) /
    (180 / Math.PI)
  return { x, y }
}
 
export function datum2spherical(lat1, lat2, lng1, lng2) {
  const earthOrigin = Math.PI * earthRadius
 
  //const d = earthRadius * arccos[(sin(lat1) * sin(lat2)) + cos(lat1) * cos(lat2) * cos(lng2 – lng1)]
  const averageLat = (lat1 + lat2) / 2
  const x =
    1000 *
    earthRadius *
    arccos(
      sin(averageLat) * sin(averageLat) +
        cos(averageLat) * cos(averageLat) * cos(lng2 - lng1)
    )
  const y =
    1000 * earthRadius * arccos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2))
 
  // const x = longitude * earthOrigin / 180.0;
  // let y = Math.log(Math.tan((90 + latitude) * Math.PI / 360.0)) / (Math.PI / 180.0);
 
  // y = y * earthOrigin / 180.0;
 
  return { x, y }
}
function cos(deg) {
  return Math.cos((deg * Math.PI) / 180)
}
function sin(deg) {
  return Math.sin((deg * Math.PI) / 180)
}
function arccos(val) {
  return Math.acos(val)
}