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-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  8.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  9.  * </p>
  10.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  11.  * initial version Nov 30, 2015 <br>
  12.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  13.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  14.  */
  15. public class CoordinateTransformLonLatToXY implements CoordinateTransform, Serializable
  16. {
  17.     /** */
  18.     private static final long serialVersionUID = 20151130L;

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

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

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

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

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

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

  31.     /** Earth constants. */
  32.     private static final double RE = 6378137;

  33.     /** Earth constants. */
  34.     private static final double RP = 6356752.31424518;

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

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

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

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

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

  70.         double term2 = lat * coslat + term1;

  71.         double x = coslon * term2 - this.centerX;
  72.         double y = sinlon * term2 - this.centerY;

  73.         return new double[] {x, y};
  74.     }

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

  81.         return new double[] {x, y};
  82.     }

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

  91. }