|
| 1 | +using ProjNet.CoordinateSystems.Transformations; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +namespace ProjNet.CoordinateSystems.Projections |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Implements the Mercator Auxiliary Sphere projection (Web Mercator). |
| 9 | + /// This projection uses a spherical model with a constant radius. |
| 10 | + /// </summary> |
| 11 | + [Serializable] |
| 12 | + internal class MercatorAuxiliarySphere : MapProjection |
| 13 | + { |
| 14 | + // Scale factor – for the spherical (auxiliary) Mercator this is 1. |
| 15 | + private const double _k0 = 1.0; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes the MercatorAuxiliarySphere projection with the specified parameters. |
| 19 | + /// </summary> |
| 20 | + /// <param name="parameters">List of projection parameters.</param> |
| 21 | + public MercatorAuxiliarySphere(IEnumerable<ProjectionParameter> parameters) |
| 22 | + : this(parameters, null) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes the MercatorAuxiliarySphere projection with the specified parameters. |
| 28 | + /// </summary> |
| 29 | + /// <param name="parameters">List of projection parameters.</param> |
| 30 | + /// <param name="isInverse">Reference to the inverse projection.</param> |
| 31 | + protected MercatorAuxiliarySphere(IEnumerable<ProjectionParameter> parameters, MercatorAuxiliarySphere isInverse) |
| 32 | + : base(parameters, isInverse) |
| 33 | + { |
| 34 | + Authority = "EPSG"; |
| 35 | + Name = "Mercator_Auxiliary_Sphere"; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Converts geographic coordinates (in radians) to projected coordinates (in meters). |
| 40 | + /// </summary> |
| 41 | + /// <param name="lon">Longitude in radians.</param> |
| 42 | + /// <param name="lat">Latitude in radians.</param> |
| 43 | + /// <remarks> |
| 44 | + /// It is assumed that _semiMajor and central_meridian (as well as other parameters like false_easting/false_northing) |
| 45 | + /// are already set in the base class. |
| 46 | + /// </remarks> |
| 47 | + protected override void RadiansToMeters(ref double lon, ref double lat) |
| 48 | + { |
| 49 | + if (double.IsNaN(lon) || double.IsNaN(lat)) |
| 50 | + { |
| 51 | + lon = double.NaN; |
| 52 | + lat = double.NaN; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + double dLon = lon; |
| 57 | + double dLat = lat; |
| 58 | + |
| 59 | + if (Math.Abs(Math.Abs(dLat) - HALF_PI) <= EPSLN) |
| 60 | + { |
| 61 | + throw new ArgumentException("Transformation cannot be computed at the poles."); |
| 62 | + } |
| 63 | + |
| 64 | + // Forward equations for the Spherical (Auxiliary) Mercator Projection: |
| 65 | + // X = semiMajor * k0 * (lon - central_meridian) |
| 66 | + // Y = semiMajor * k0 * ln( tan(PI/4 + lat/2) ) |
| 67 | + lon = _semiMajor * _k0 * (dLon - central_meridian); |
| 68 | + lat = _semiMajor * _k0 * Math.Log(Math.Tan((PI * 0.25) + (dLat * 0.5))); |
| 69 | + // Note: false_easting and false_northing can be added here if necessary. |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Converts projected coordinates (in meters) to geographic coordinates (in radians). |
| 74 | + /// </summary> |
| 75 | + /// <param name="x">X coordinate in meters.</param> |
| 76 | + /// <param name="y">Y coordinate in meters.</param> |
| 77 | + /// <remarks> |
| 78 | + /// Uses the inverse transformation of the Spherical Mercator Projection. |
| 79 | + /// </remarks> |
| 80 | + protected override void MetersToRadians(ref double x, ref double y) |
| 81 | + { |
| 82 | + double dX = x; |
| 83 | + double dY = y; |
| 84 | + |
| 85 | + // Inverse equations: |
| 86 | + // lon = central_meridian + X / (semiMajor * k0) |
| 87 | + // lat = PI/2 - 2 * atan( exp( -Y / (semiMajor * k0) ) ) |
| 88 | + double ts = Math.Exp(-dY / (_semiMajor * _k0)); |
| 89 | + double dLat = HALF_PI - (2 * Math.Atan(ts)); |
| 90 | + double dLon = central_meridian + (dX / (_semiMajor * _k0)); |
| 91 | + |
| 92 | + x = dLon; |
| 93 | + y = dLat; |
| 94 | + // Note: false_easting/false_northing can be subtracted here if provided in the parameter list. |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Returns the inverse transformation of this projection. |
| 99 | + /// </summary> |
| 100 | + /// <returns>The inverse projection as MathTransform.</returns> |
| 101 | + public override MathTransform Inverse() |
| 102 | + { |
| 103 | + if (_inverse is null) |
| 104 | + { |
| 105 | + _inverse = new MercatorAuxiliarySphere(_Parameters.ToProjectionParameter(), this); |
| 106 | + } |
| 107 | + return _inverse; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments