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 @Override
60 public final float[] floatTransform(final double lon, final double lat)
61 {
62 double[] dt = doubleTransform(lon, lat);
63 return new float[] {(float) dt[0], (float) dt[1]};
64 }
65
66
67
68
69
70
71
72 public final double[] doubleTransformWgs84ToCartesianXy(final double lon, final double lat)
73 {
74 double latrad = lat / 180.0 * Math.PI;
75 double lonrad = lon / 180.0 * Math.PI;
76
77 double coslat = Math.cos(latrad);
78 double sinlat = Math.sin(latrad);
79 double coslon = Math.cos(lonrad);
80 double sinlon = Math.sin(lonrad);
81
82 double term1 = (RE * RE * coslat) / Math.sqrt(RE * RE * coslat * coslat + RP * RP * sinlat * sinlat);
83
84 double term2 = lat * coslat + term1;
85
86 double x = coslon * term2 - this.centerX;
87 double y = sinlon * term2 - this.centerY;
88
89 return new double[] {x, y};
90 }
91
92 @Override
93 public final double[] doubleTransform(final double lon, final double lat)
94 {
95 double x = (lon - this.lonCenter) * this.lonToM;
96 double y = (lat - this.latCenter) * this.latToM;
97
98 return new double[] {x, y};
99 }
100
101 @Override
102 public final String toString()
103 {
104 return "CoordinateTransformLonLatToXY [centerX=" + this.centerX + ", centerY=" + this.centerY + ", lonCenter="
105 + this.lonCenter + ", latCenter=" + this.latCenter + ", latToM=" + this.latToM + ", lonToM=" + this.lonToM
106 + "]";
107 }
108
109 }