mapbox / mapbox-scenekit

Other
231 stars 51 forks source link

metersToDegreesForLat paremeter must be a latitude instead of the longitude #91

Open Filyus opened 9 months ago

Filyus commented 9 months ago

Compare this from Understanding terms in Length of Degree formula?:

  m1 = 111132.95255
  m2 = -559.84957
  m3 = 1.17514
  m4 = -0.00230
  p1 = 111412.87733
  p2 = -93.50412
  p3 = 0.11774
  p4 = -0.000165
m.per.deg <- function(lat) {
  m1 = 111132.92;     # latitude calculation term 1
  m2 = -559.82;       # latitude calculation term 2
  m3 = 1.175;         # latitude calculation term 3
  m4 = -0.0023;       # latitude calculation term 4
  p1 = 111412.84;     # longitude calculation term 1
  p2 = -93.5;         # longitude calculation term 2
  p3 = 0.118;         # longitude calculation term 3

  m1 = 111132.95255
  m2 = -559.84957
  m3 = 1.17514
  m4 = -0.00230
  p1 = 111412.87733
  p2 = -93.50412
  p3 = 0.11774
  p4 = -0.000165

  latlen = m1 + m2 * cos(2 * lat) + m3 * cos(4 * lat) + m4 * cos(6 * lat);
  longlen = p1 * cos(lat) + p2 * cos(3 * lat) + p3 * cos(5 * lat);
  return(cbind(M.approx=latlen, r.approx=longlen))
}

with this:

    static func metersToDegreesForLat(atLongitude longitude: CLLocationDegrees) -> CLLocationDistance {
        let a = cos(2 * Math.degreesToRadians(longitude))
        let b = cos(4 * Math.degreesToRadians(longitude))
        let c = cos(6 * Math.degreesToRadians(longitude))

        return 1.0 / fabs(111132.95255 - 559.84957 * a + 1.17514 * b - 0.00230 * c)
    }

    static func metersToDegreesForLon(atLatitude latitude: CLLocationDegrees) -> CLLocationDistance {
        let a = cos(Math.degreesToRadians(latitude))
        let b = cos(3 * Math.degreesToRadians(latitude))
        let c = cos(5 * Math.degreesToRadians(latitude))

        return 1.0 / fabs(111412.87733 * a - 93.50412 * b + 0.11774 * c)
    }

You can also look at the Geographic coordinate system wiki article:

On the WGS 84 spheroid, the length in meters of a degree of latitude at latitude ϕ (that is, the number of meters you would have to travel along a north–south line to move 1 degree in latitude, when at latitude ϕ), is about:

$111132.92 - 559.82\, \cos 2\phi + 1.175\, \cos 4\phi - 0.0023\, \cos 6\phi$

The returned measure of meters per degree latitude varies continuously with latitude. Similarly, the length in meters of a degree of longitude can be calculated as

$111412.84\, \cos \phi - 93.5\, \cos 3\phi + 0.118\, \cos 5\phi$

(Those coefficients can be improved, but as they stand the distance they give is correct within a centimeter.) The formulae both return units of meters per degree.