-
Notifications
You must be signed in to change notification settings - Fork 2
/
GConversions.pas
314 lines (251 loc) · 8.81 KB
/
GConversions.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Summary
// Geographic coordinate conversion Classes
//
// Description
// TGCoordConversion
//
// Author
// Graham Knight (tglobe@tglobe.com)
//
{$I GLOBE5.INC}
unit GConversions;
interface
uses GSysUtils;
type
TGCoordConversion = class( TGRoot )
private
FScaleFactor: Extended;
FLatitudeShift: TGDecimalDegree;
FFirstParallel: TGDecimalDegree;
FLongitudeShift: TGDecimalDegree;
FSecondParallel: TGDecimalDegree;
FSpheroid: TGSpheroid;
FInputUnits: TGUnitTypes;
FModified: Boolean;
FCentralMeridian: TGDecimalDegree;
procedure SetFirstParallel(const Value: TGDecimalDegree);
procedure SetInputUnits(const Value: TGUnitTypes);
procedure SetLatitudeShift(const Value: TGDecimalDegree);
procedure SetLongitudeShift(const Value: TGDecimalDegree);
procedure SetScaleFactor(const Value: Extended);
procedure SetSecondParallel(const Value: TGDecimalDegree);
procedure SetSpheroid(const Value: TGSpheroid);
procedure SetCentralMeridian(const Value: TGDecimalDegree);
protected
FEquitorialRadius, FEccentricity, FEccentricitySquared : Extended;
public
constructor Create; virtual;
procedure ReadE00_PARAMS( const params : array of string ); virtual;
function ToPointLL( long, lat : Extended ) : TGPointLL; virtual;
property CentralMeridian : TGDecimalDegree read FCentralMeridian write SetCentralMeridian;
property LongitudeShift : TGDecimalDegree read FLongitudeShift write SetLongitudeShift;
property LatitudeShift : TGDecimalDegree read FLatitudeShift write SetLatitudeShift;
property FirstParallel : TGDecimalDegree read FFirstParallel write SetFirstParallel;
property SecondParallel : TGDecimalDegree read FSecondParallel write SetSecondParallel;
property ScaleFactor : Extended read FScaleFactor write SetScaleFactor;
property InputUnits : TGUnitTypes read FInputUnits write SetInputUnits;
property Spheroid : TGSpheroid read FSpheroid write SetSpheroid;
property Modified : Boolean read FModified write FModified;
end;
TGPolarCoordConversion = class( TGCoordConversion )
private
FPhi1, FLambda0, FX1, FM1 : Extended;
protected
procedure Initialise;
public
procedure ReadE00_PARAMS( const params : array of string ); override;
function ToPointLL( long, lat : Extended ) : TGPointLL; override;
end;
TGLambertCoordConversion = class( TGCoordConversion )
private
FPhi0, FPhi1, FPhi2, FLambda0, FN, FF, FRho0 : Extended;
protected
procedure Initialise;
public
procedure ReadE00_PARAMS( const params : array of string ); override;
function ToPointLL( long, lat : Extended ) : TGPointLL; override;
end;
TGTransverseCoordConversion = class( TGCoordConversion )
protected
procedure Initialise;
public
procedure ReadE00_PARAMS( const params : array of string ); override;
function ToPointLL( long, lat : Extended ) : TGPointLL; override;
end;
implementation
uses SysUtils, Classes;
{ TGCoordConversion }
constructor TGCoordConversion.Create;
begin
ScaleFactor := 1.0;
InputUnits := guDegree;
Spheroid := WGS84;
end;
procedure TGCoordConversion.ReadE00_PARAMS(const params: array of string);
begin
// Does nothing.
end;
procedure TGCoordConversion.SetCentralMeridian(
const Value: TGDecimalDegree);
begin
FCentralMeridian := Value;
Modified := true;
end;
procedure TGCoordConversion.SetFirstParallel(const Value: TGDecimalDegree);
begin
FFirstParallel := Value;
Modified := true;
end;
procedure TGCoordConversion.SetInputUnits(const Value: TGUnitTypes);
begin
FInputUnits := Value;
Modified := true;
end;
procedure TGCoordConversion.SetLatitudeShift(const Value: TGDecimalDegree);
begin
FLatitudeShift := Value;
Modified := true;
end;
procedure TGCoordConversion.SetLongitudeShift( const Value: TGDecimalDegree);
begin
FLongitudeShift := Value;
Modified := true;
end;
procedure TGCoordConversion.SetScaleFactor(const Value: Extended);
begin
FScaleFactor := Value;
Modified := true;
end;
procedure TGCoordConversion.SetSecondParallel( const Value: TGDecimalDegree);
begin
FSecondParallel := Value;
Modified := true;
end;
procedure TGCoordConversion.SetSpheroid(const Value: TGSpheroid);
begin
FSpheroid := Value;
FEquitorialRadius := SpheroidData[Ord(Value)].r * GU_TORADIANS;
FEccentricitySquared := 2 * SpheroidData[Ord(Spheroid)].f - Sqr(SpheroidData[Ord(Spheroid)].f);
FEccentricity := Sqrt(FEccentricitySquared);
Modified := true;
end;
function TGCoordConversion.ToPointLL(long, lat: Extended): TGPointLL;
begin
Result.iLongX := GUFrom(long * ScaleFactor, InputUnits) + Round( LongitudeShift * GU_DEGREE );
Result.iLatY := GUFrom(lat * ScaleFactor, InputUnits) + Round( LatitudeShift * GU_DEGREE );
Result.iHeightZ := 0;
end;
{ TGPolarCoordConversion }
procedure TGPolarCoordConversion.Initialise;
begin
FPhi1 := LatitudeShift * DD_TORADIANS;
FLambda0 := LongitudeShift * DD_TORADIANS;
FM1 := Cos(FPhi1) / Sqrt(1 - FEccentricitySquared * Sqr(Sin(FPhi1)));
FX1 := 2 * ArcTan(Tan(QuarterPi + FPhi1 / 2) * Power((1 - FEccentricity * Sin(FPhi1))
/ (1 + FEccentricity * Sin(FPhi1)), FEccentricity / 2)) - HalfPi;
Modified := false;
end;
procedure TGPolarCoordConversion.ReadE00_PARAMS( const params: array of string);
begin
LongitudeShift := StrToExtendedDef( Trim(Copy(params[0], 1, 4)), 0.0);
LatitudeShift := -90;
end;
function TGPolarCoordConversion.ToPointLL(long, lat: Extended): TGPointLL;
var
X, Y, Rho, Phi, C, cappa, Tmp : Extended;
begin
if Modified then
Initialise;
X := GUFrom(long, InputUnits) * GU_TORADIANS;
Y := GUFrom(lat, InputUnits) * GU_TORADIANS;
Rho := Sqrt(X * X + Y * Y);
if Rho = 0 then
begin
Result.iLongX := Round(FPhi1 * GU_FROMRADIANS);
Result.iLatY := Round(FLambda0 * GU_FROMRADIANS);
end
else
begin
C := 2 * ArcTan2(Tan(Rho * Cos(FX1)), (2 * FEquitorialRadius * ScaleFactor * FM1));
cappa := ArcSin(Cos(C) * Sin(FX1) + (Y * Sin(C) * Cos(FX1) / Rho));
Phi := cappa;
repeat
Tmp := Phi;
Phi := 2 * ArcTan(Tan(QuarterPi + cappa / 2) *
Power((1 + FEccentricity * Sin(Phi)) / (1 - FEccentricity * Sin(Phi)),
FEccentricity * 0.5)) - HalfPi;
until Abs(Phi - Tmp) < 0.00000005;
Result.iLongX := Round((FLambda0 + ArcTan2(X * Sin(C), (Rho * Cos(FX1)
* Cos(C) - Y * Sin(FX1) * Sin(C)))) * GU_FROMRADIANS);
Result.iLatY := Round(Phi * GU_FROMRADIANS);
end;
end;
{ TGLambertCoordConversion }
procedure TGLambertCoordConversion.Initialise;
var
m1, m2, t1, t2, t0 : Extended;
function Tx(Phi : Extended) : Extended;
begin
Result := Tan(QuarterPi - Phi * 0.5) /
Power(
(1 - FEccentricity * Sin(Phi)) / (1 + FEccentricity * Sin(Phi)),
FEccentricity * 0.5);
end;
begin
FPhi1 := FirstParallel * DD_TORADIANS;
FPhi2 := SecondParallel * DD_TORADIANS;
FPhi0 := LatitudeShift * DD_TORADIANS;
FLambda0 := LongitudeShift * DD_TORADIANS;
m1 := Cos(FPhi1) / Sqrt(1 - FEccentricitySquared * Sqr(Sin(FPhi1)));
m2 := Cos(FPhi2) / Sqrt(1 - FEccentricitySquared * Sqr(Sin(FPhi2)));
t0 := Tx(FPhi0);
t1 := Tx(FPhi1);
t2 := Tx(FPhi2);
FN := Ln(m1 / m2) / ln(t1 / t2);
FF := m1 / (FN * Power(t1, FN));
FRho0 := FEquitorialRadius * FF * Power(t0, FN);
Modified := false;
end;
function TGLambertCoordConversion.ToPointLL(long, lat: Extended): TGPointLL;
var
Tmp, Phi, Rho, Theta, t : Extended;
X, Y : Extended;
begin
if Modified then
Initialise;
X := GUFrom(long, InputUnits) * GU_TORADIANS;
Y := GUFrom(lat, InputUnits) * GU_TORADIANS;
Rho := Sign(FN) * Sqrt(X * X + Sqr(FRho0 - Y));
Theta := ArcTan2(X, (FRho0 - Y));
t := Power(Rho / (FEquitorialRadius * FF), 1 / FN);
Phi := HalfPi - 2 * ArcTan(t);
repeat
Tmp := Phi;
Phi := HalfPi - 2 * ArcTan(t * Power((1 - FEccentricity * Sin(Tmp))
/ (1 + FEccentricity * Sin(Tmp)), FEccentricity * 0.5));
until Abs(Phi - tmp) < 0.00000005;
Result.iLongX := Round((Theta / FN + FLambda0) * GU_FROMRADIANS) + Round( CentralMeridian * GU_DEGREE );
Result.iLatY := Round(Phi * GU_FROMRADIANS) + Round( LatitudeShift * GU_DEGREE );
end;
procedure TGLambertCoordConversion.ReadE00_PARAMS( const params : array of string );
begin
FirstParallel := StrToExtendedDef(Trim(Copy(params[0], 1, 3)), 0.0);
SecondParallel := StrToExtendedDef(Trim(Copy(params[1], 1, 3)), 0.0);
CentralMeridian := StrToExtendedDef(Trim(Copy(params[2], 1, 4)), 0.0);
end;
{ TGTransverseCoordConversion }
procedure TGTransverseCoordConversion.Initialise;
begin
end;
procedure TGTransverseCoordConversion.ReadE00_PARAMS( const params: array of string);
begin
end;
function TGTransverseCoordConversion.ToPointLL(long, lat: Extended): TGPointLL;
begin
if Modified then
Initialise;
end;
initialization
// Register the Coord Conversion class so it can be created as needed
RegisterClasses( [TGPolarCoordConversion, TGLambertCoordConversion, TGTransverseCoordConversion] );
end.