CoordinateTransformLonLatToXy.java

  1. package org.opentrafficsim.core.gis;

  2. import java.io.Serializable;

  3. import nl.tudelft.simulation.dsol.animation.gis.transform.CoordinateTransform;

  4. /**
  5.  * Transformation from lat-lon to X-Y in meters. Source: https://en.wikipedia.org/wiki/Geographic_coordinate_system.
  6.  * <p>
  7.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  11.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  12.  */
  13. public class CoordinateTransformLonLatToXy implements CoordinateTransform, Serializable
  14. {
  15.     /** */
  16.     private static final long serialVersionUID = 20151130L;

  17.     /** The x-center of the center point (0, 0). */
  18.     private double centerX = 0.0;

  19.     /** The y-center of the center point (0, 0). */
  20.     private double centerY = 0.0;

  21.     /** The x-center of the center point (0, 0). */
  22.     private final double lonCenter;

  23.     /** The y-center of the center point (0, 0). */
  24.     private final double latCenter;

  25.     /** One percent of a lat degree to y meters. */
  26.     private double latToM;

  27.     /** One percent of a lon degree to x meters. */
  28.     private double lonToM;

  29.     /** Earth constants. */
  30.     private static final double RE = 6378137;

  31.     /** Earth constants. */
  32.     private static final double RP = 6356752.31424518;

  33.     /**
  34.      * Transformation from: https://en.wikipedia.org/wiki/Geographic_coordinate_system.
  35.      * @param latCenter double; the latitude of the center point (0, 0)
  36.      * @param lonCenter double; the longitude of the center point (0, 0)
  37.      */
  38.     public CoordinateTransformLonLatToXy(final double lonCenter, final double latCenter)
  39.     {
  40.         this.latCenter = latCenter;
  41.         this.lonCenter = lonCenter;
  42.         double lr = Math.toRadians(latCenter);
  43.         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;
  44.         this.lonToM = 111412.84 * Math.cos(lr) - 93.5 * Math.cos(3.0 * lr) - 0.118 * Math.cos(5.0 * lr); // 88.32;
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public final float[] floatTransform(final double lon, final double lat)
  49.     {
  50.         double[] dt = doubleTransform(lon, lat);
  51.         return new float[] {(float) dt[0], (float) dt[1]};
  52.     }

  53.     /**
  54.      * Convert WGS84 coordinates to Cartesian coordinates.
  55.      * @param lon double; longitude in degrees
  56.      * @param lat double; latitude in degrees
  57.      * @return double[]
  58.      */
  59.     public final double[] doubleTransformWgs84ToCartesianXy(final double lon, final double lat)
  60.     {
  61.         double latrad = lat / 180.0 * Math.PI;
  62.         double lonrad = lon / 180.0 * Math.PI;

  63.         double coslat = Math.cos(latrad);
  64.         double sinlat = Math.sin(latrad);
  65.         double coslon = Math.cos(lonrad);
  66.         double sinlon = Math.sin(lonrad);

  67.         double term1 = (RE * RE * coslat) / Math.sqrt(RE * RE * coslat * coslat + RP * RP * sinlat * sinlat);

  68.         double term2 = lat * coslat + term1;

  69.         double x = coslon * term2 - this.centerX;
  70.         double y = sinlon * term2 - this.centerY;

  71.         return new double[] {x, y};
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public final double[] doubleTransform(final double lon, final double lat)
  76.     {
  77.         double x = (lon - this.lonCenter) * this.lonToM;
  78.         double y = (lat - this.latCenter) * this.latToM;

  79.         return new double[] {x, y};
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public final String toString()
  84.     {
  85.         return "CoordinateTransformLonLatToXY [centerX=" + this.centerX + ", centerY=" + this.centerY + ", lonCenter="
  86.                 + this.lonCenter + ", latCenter=" + this.latCenter + ", latToM=" + this.latToM + ", lonToM=" + this.lonToM
  87.                 + "]";
  88.     }

  89. }