|
| 1 | +""" |
| 2 | + WebMercatorfromLLA(datum=wgs84) |
| 3 | +
|
| 4 | +Convert from LLA to Web Mercator / Pseudo Mercator, following the convention of |
| 5 | +proj, which uses a scaling factor of the semi-major axis of the ellipsoid |
| 6 | +https://proj.org/operations/projections/webmerc.html. |
| 7 | +
|
| 8 | +!!! warning |
| 9 | + For web mapping applications this projection is ubiquitous due to its |
| 10 | + simplicity of implementation, but this simplicity gives rise to poor |
| 11 | + mathematical properties: it doesn't preserve angles away from the equator. |
| 12 | + Other projections should be preferred if possible, and especially when |
| 13 | + measurement is important. See |
| 14 | + https://earth-info.nga.mil/GandG/wgs84/web_mercator/(U)%20NGA_SIG_0011_1.0.0_WEBMERC.pdf |
| 15 | + for an extended discussion. |
| 16 | +""" |
| 17 | +struct WebMercatorfromLLA <: Transformation |
| 18 | + el::Ellipsoid |
| 19 | +end |
| 20 | + |
| 21 | +WebMercatorfromLLA(d::Datum=wgs84) = WebMercatorfromLLA(ellipsoid(d)) |
| 22 | + |
| 23 | +""" |
| 24 | + LLAfromWebMercator |
| 25 | +
|
| 26 | +Inverse of WebMercatorfromLLA — see the docs for that transformation. |
| 27 | +""" |
| 28 | +struct LLAfromWebMercator <: Transformation |
| 29 | + el::Ellipsoid |
| 30 | +end |
| 31 | + |
| 32 | +LLAfromWebMercator(d::Datum=wgs84) = LLAfromWebMercator(ellipsoid(d)) |
| 33 | + |
| 34 | + |
| 35 | +function (trans::WebMercatorfromLLA)(lla::LLA) |
| 36 | + xy = trans(LatLon(lla.lat, lla.lon)) |
| 37 | + SA[xy[1], xy[2], lla.alt] |
| 38 | +end |
| 39 | + |
| 40 | +function (trans::WebMercatorfromLLA)(ll::LatLon) |
| 41 | + lat_lim = 85.06 # according to https://epsg.io/3857 |
| 42 | + if abs(ll.lat) > lat_lim |
| 43 | + throw(ArgumentError("Exceeded Web Mercator latitude bounds")) |
| 44 | + end |
| 45 | + x, y = web_mercator_forward(ll.lat, ll.lon, trans.el.a) |
| 46 | + SA[x, y] |
| 47 | +end |
| 48 | + |
| 49 | +function (trans::LLAfromWebMercator)(point::AbstractVector) |
| 50 | + x = point[1] |
| 51 | + y = point[2] |
| 52 | + lat, lon = web_mercator_reverse(x, y, trans.el.a) |
| 53 | + if length(point) == 2 |
| 54 | + LatLon(lat, lon) |
| 55 | + elseif length(point) == 3 |
| 56 | + LLA(lat, lon, point[3]) |
| 57 | + else |
| 58 | + throw(ArgumentError("Expected input vector of length 2 or 3")) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +Base.inv(trans::WebMercatorfromLLA) = LLAfromWebMercator(trans.el) |
| 63 | +Base.inv(trans::LLAfromWebMercator) = WebMercatorfromLLA(trans.el) |
| 64 | + |
| 65 | +# Web / Psuedo Mercator. Following the scaling convention of proj, the scaling |
| 66 | +# will be set to the ellipsoid semi-major axis. |
| 67 | +# https://proj.org/operations/projections/webmerc.html |
| 68 | +function web_mercator_forward(lat, lon, scaling) |
| 69 | + x = scaling * deg2rad(lon) |
| 70 | + y = scaling * log(tand((90 + lat)/2)) |
| 71 | + (x,y) |
| 72 | +end |
| 73 | + |
| 74 | +function web_mercator_reverse(x, y, scaling) |
| 75 | + lon = rad2deg(x / scaling) |
| 76 | + lat = 2 * atand(exp(y / scaling)) - 90 |
| 77 | + (lat,lon) |
| 78 | +end |
0 commit comments