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
9
10
11
12
13
14
15
16 public class CoordinateTransformLonLatToXy implements CoordinateTransform, Serializable
17 {
18
19 private static final long serialVersionUID = 20151130L;
20
21
22 private double centerX = 0.0;
23
24
25 private double centerY = 0.0;
26
27
28 private final double lonCenter;
29
30
31 private final double latCenter;
32
33
34 private double latToM;
35
36
37 private double lonToM;
38
39
40 private static final double RE = 6378137;
41
42
43 private static final double RP = 6356752.31424518;
44
45
46
47
48
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);
56 this.lonToM = 111412.84 * Math.cos(lr) - 93.5 * Math.cos(3.0 * lr) - 0.118 * Math.cos(5.0 * lr);
57 }
58
59
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
69
70
71
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
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
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 }