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