View Javadoc
1   package org.opentrafficsim.animation.gis;
2   
3   import java.io.Serializable;
4   
5   import nl.tudelft.simulation.dsol.animation.gis.transform.CoordinateTransform;
6   
7   /**
8    * Transformation from lat-lon to X-Y in meters. Source: https://en.wikipedia.org/wiki/Geographic_coordinate_system.
9    * <p>
10   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
15   */
16  public class CoordinateTransformLonLatToXy implements CoordinateTransform, Serializable
17  {
18      /** */
19      private static final long serialVersionUID = 20151130L;
20  
21      /** The x-center of the center point (0, 0). */
22      private double centerX = 0.0;
23  
24      /** The y-center of the center point (0, 0). */
25      private double centerY = 0.0;
26  
27      /** The x-center of the center point (0, 0). */
28      private final double lonCenter;
29  
30      /** The y-center of the center point (0, 0). */
31      private final double latCenter;
32  
33      /** One percent of a lat degree to y meters. */
34      private double latToM;
35  
36      /** One percent of a lon degree to x meters. */
37      private double lonToM;
38  
39      /** Earth constants. */
40      private static final double RE = 6378137;
41  
42      /** Earth constants. */
43      private static final double RP = 6356752.31424518;
44  
45      /**
46       * Transformation from: https://en.wikipedia.org/wiki/Geographic_coordinate_system.
47       * @param latCenter double; the latitude of the center point (0, 0)
48       * @param lonCenter double; the longitude of the center point (0, 0)
49       */
50      public CoordinateTransformLonLatToXy(final double lonCenter, final double latCenter)
51      {
52          this.latCenter = latCenter;
53          this.lonCenter = lonCenter;
54          double lr = Math.toRadians(latCenter);
55          this.latToM = 111132.92 - 559.82 * Math.cos(2.0 * lr) + 1.175 * Math.cos(4.0 * lr) - 0.0023 * Math.cos(6.0 * lr); // 111.31;
56          this.lonToM = 111412.84 * Math.cos(lr) - 93.5 * Math.cos(3.0 * lr) - 0.118 * Math.cos(5.0 * lr); // 88.32;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final float[] floatTransform(final double lon, final double lat)
62      {
63          double[] dt = doubleTransform(lon, lat);
64          return new float[] {(float) dt[0], (float) dt[1]};
65      }
66  
67      /**
68       * Convert WGS84 coordinates to Cartesian coordinates.
69       * @param lon double; longitude in degrees
70       * @param lat double; latitude in degrees
71       * @return double[]
72       */
73      public final double[] doubleTransformWgs84ToCartesianXy(final double lon, final double lat)
74      {
75          double latrad = lat / 180.0 * Math.PI;
76          double lonrad = lon / 180.0 * Math.PI;
77  
78          double coslat = Math.cos(latrad);
79          double sinlat = Math.sin(latrad);
80          double coslon = Math.cos(lonrad);
81          double sinlon = Math.sin(lonrad);
82  
83          double term1 = (RE * RE * coslat) / Math.sqrt(RE * RE * coslat * coslat + RP * RP * sinlat * sinlat);
84  
85          double term2 = lat * coslat + term1;
86  
87          double x = coslon * term2 - this.centerX;
88          double y = sinlon * term2 - this.centerY;
89  
90          return new double[] {x, y};
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public final double[] doubleTransform(final double lon, final double lat)
96      {
97          double x = (lon - this.lonCenter) * this.lonToM;
98          double y = (lat - this.latCenter) * this.latToM;
99  
100         return new double[] {x, y};
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String toString()
106     {
107         return "CoordinateTransformLonLatToXY [centerX=" + this.centerX + ", centerY=" + this.centerY + ", lonCenter="
108                 + this.lonCenter + ", latCenter=" + this.latCenter + ", latToM=" + this.latToM + ", lonToM=" + this.lonToM
109                 + "]";
110     }
111 
112 }