1 package org.opentrafficsim.animation.gis;
2
3 import nl.tudelft.simulation.dsol.animation.gis.DoubleXY;
4 import nl.tudelft.simulation.dsol.animation.gis.FloatXY;
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
17 {
18
19 private double centerX = 0.0;
20
21
22 private double centerY = 0.0;
23
24
25 private final double lonCenter;
26
27
28 private final double latCenter;
29
30
31 private double latToM;
32
33
34 private double lonToM;
35
36
37 private static final double RE = 6378137;
38
39
40 private static final double RP = 6356752.31424518;
41
42
43
44
45
46
47 public CoordinateTransformLonLatToXy(final double lonCenter, final double latCenter)
48 {
49 this.latCenter = latCenter;
50 this.lonCenter = lonCenter;
51 double lr = Math.toRadians(latCenter);
52 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);
53 this.lonToM = 111412.84 * Math.cos(lr) - 93.5 * Math.cos(3.0 * lr) - 0.118 * Math.cos(5.0 * lr);
54 }
55
56 @Override
57 public final FloatXY floatTransform(final double lon, final double lat)
58 {
59 DoubleXY dt = doubleTransform(lon, lat);
60 return new FloatXY((float) dt.x(), (float) dt.y());
61 }
62
63 @Override
64 public final FloatXY floatTransform(final float lon, final float lat)
65 {
66 DoubleXY dt = doubleTransform(lon, lat);
67 return new FloatXY((float) dt.x(), (float) dt.y());
68 }
69
70
71
72
73
74
75
76 public final DoubleXY doubleTransformWgs84ToCartesianXy(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 DoubleXY(x, y);
94 }
95
96 @Override
97 public final DoubleXY doubleTransform(final double lon, final double lat)
98 {
99 double x = (lon - this.lonCenter) * this.lonToM;
100 double y = (lat - this.latCenter) * this.latToM;
101
102 return new DoubleXY(x, y);
103 }
104
105 @Override
106 public final String toString()
107 {
108 return "CoordinateTransformLonLatToXY [centerX=" + this.centerX + ", centerY=" + this.centerY + ", lonCenter="
109 + this.lonCenter + ", latCenter=" + this.latCenter + ", latToM=" + this.latToM + ", lonToM=" + this.lonToM
110 + "]";
111 }
112
113 }