-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEGaussKruger.php
331 lines (316 loc) · 12.1 KB
/
EGaussKruger.php
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/* -- Original copyright from http://mellifica.se/geodesi/gausskruger.js ---- */
//
// Author: Arnold Andreasson, info@mellifica.se
// Copyright (c) 2007-2013 Arnold Andreasson
// License: MIT License as follows:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// =============================================================================
// Javascript-implementation of "Gauss Conformal Projection
// (Transverse Mercator), Krügers Formulas".
// - Parameters for SWEREF99 lat-long to/from RT90 and SWEREF99
// coordinates (RT90 and SWEREF99 are used in Swedish maps).
// Source: http://www.lantmateriet.se/geodesi/
/* -- This is EGaussKruger - Yii extension port -- */
//
// Port author: Leif Linse (Leffe108 on github)
// License: MIT License (see above)
//
// The port is more or less just a translation from JavaScript
// into PHP with the code being encapsulated into a class.
// Many thanks to the original author who has made a much
// larger job in making the initial JavaScript version, than
// this quite simple port.
class EGaussKruger
{
protected $axis = null; // Semi-major axis of the ellipsoid.
protected $flattening = null; // Flattening of the ellipsoid.
protected $central_meridian = null; // Central meridian for the projection.
protected $lat_of_origin = null; // Latitude of origin.
protected $scale = null; // Scale on central meridian.
protected $false_northing = null; // Offset for origo.
protected $false_easting = null; // Offset for origo.
// Parameters for RT90 and SWEREF99TM.
// Note: Parameters for RT90 are choosen to eliminate the
// differences between Bessel and GRS80-ellipsoides.
// Bessel-variants should only be used if lat/long are given as
// RT90-lat/long based on the Bessel ellipsoide (from old maps).
// Parameter: projection (string). Must match if-statement.
public function swedish_params($projection) {
// RT90 parameters, GRS 80 ellipsoid.
if ($projection == "rt90_7.5_gon_v") {
$this->grs80_params();
$this->central_meridian = 11.0 + 18.375/60.0;
$this->scale = 1.000006000000;
$this->false_northing = -667.282;
$this->false_easting = 1500025.141;
}
else if ($projection == "rt90_5.0_gon_v") {
$this->grs80_params();
$this->central_meridian = 13.0 + 33.376/60.0;
$this->scale = 1.000005800000;
$this->false_northing = -667.130;
$this->false_easting = 1500044.695;
}
else if ($projection == "rt90_2.5_gon_v") {
$this->grs80_params();
$this->central_meridian = 15.0 + 48.0/60.0 + 22.624306/3600.0;
$this->scale = 1.00000561024;
$this->false_northing = -667.711;
$this->false_easting = 1500064.274;
}
else if ($projection == "rt90_0.0_gon_v") {
$this->grs80_params();
$this->central_meridian = 18.0 + 3.378/60.0;
$this->scale = 1.000005400000;
$this->false_northing = -668.844;
$this->false_easting = 1500083.521;
}
else if ($projection == "rt90_2.5_gon_o") {
$this->grs80_params();
$this->central_meridian = 20.0 + 18.379/60.0;
$this->scale = 1.000005200000;
$this->false_northing = -670.706;
$this->false_easting = 1500102.765;
}
else if ($projection == "rt90_5.0_gon_o") {
$this->grs80_params();
$this->central_meridian = 22.0 + 33.380/60.0;
$this->scale = 1.000004900000;
$this->false_northing = -672.557;
$this->false_easting = 1500121.846;
}
// RT90 parameters, Bessel 1841 ellipsoid.
else if ($projection == "bessel_rt90_7.5_gon_v") {
$this->bessel_params();
$this->central_meridian = 11.0 + 18.0/60.0 + 29.8/3600.0;
}
else if ($projection == "bessel_rt90_5.0_gon_v") {
$this->bessel_params();
$this->central_meridian = 13.0 + 33.0/60.0 + 29.8/3600.0;
}
else if ($projection == "bessel_rt90_2.5_gon_v") {
$this->bessel_params();
$this->central_meridian = 15.0 + 48.0/60.0 + 29.8/3600.0;
}
else if ($projection == "bessel_rt90_0.0_gon_v") {
$this->bessel_params();
$this->central_meridian = 18.0 + 3.0/60.0 + 29.8/3600.0;
}
else if ($projection == "bessel_rt90_2.5_gon_o") {
$this->bessel_params();
$this->central_meridian = 20.0 + 18.0/60.0 + 29.8/3600.0;
}
else if ($projection == "bessel_rt90_5.0_gon_o") {
$this->bessel_params();
$this->central_meridian = 22.0 + 33.0/60.0 + 29.8/3600.0;
}
// SWEREF99TM and SWEREF99ddmm parameters.
else if ($projection == "sweref_99_tm") {
$this->sweref99_params();
$this->central_meridian = 15.00;
$this->lat_of_origin = 0.0;
$this->scale = 0.9996;
$this->false_northing = 0.0;
$this->false_easting = 500000.0;
}
else if ($projection == "sweref_99_1200") {
$this->sweref99_params();
$this->central_meridian = 12.00;
}
else if ($projection == "sweref_99_1330") {
$this->sweref99_params();
$this->central_meridian = 13.50;
}
else if ($projection == "sweref_99_1500") {
$this->sweref99_params();
$this->central_meridian = 15.00;
}
else if ($projection == "sweref_99_1630") {
$this->sweref99_params();
$this->central_meridian = 16.50;
}
else if ($projection == "sweref_99_1800") {
$this->sweref99_params();
$this->central_meridian = 18.00;
}
else if ($projection == "sweref_99_1415") {
$this->sweref99_params();
$this->central_meridian = 14.25;
}
else if ($projection == "sweref_99_1545") {
$this->sweref99_params();
$this->central_meridian = 15.75;
}
else if ($projection == "sweref_99_1715") {
$this->sweref99_params();
$this->central_meridian = 17.25;
}
else if ($projection == "sweref_99_1845") {
$this->sweref99_params();
$this->central_meridian = 18.75;
}
else if ($projection == "sweref_99_2015") {
$this->sweref99_params();
$this->central_meridian = 20.25;
}
else if ($projection == "sweref_99_2145") {
$this->sweref99_params();
$this->central_meridian = 21.75;
}
else if ($projection == "sweref_99_2315") {
$this->sweref99_params();
$this->central_meridian = 23.25;
}
// Test-case:
// Lat: 66 0'0", lon: 24 0'0".
// X:1135809.413803 Y:555304.016555.
else if ($projection == "test_case") {
$this->axis = 6378137.0;
$this->flattening = 1.0 / 298.257222101;
$this->central_meridian = 13.0 + 35.0/60.0 + 7.692000/3600.0;
$this->lat_of_origin = 0.0;
$this->scale = 1.000002540000;
$this->false_northing = -6226307.8640;
$this->false_easting = 84182.8790;
// Not a valid projection.
} else {
$this->central_meridian = null;
}
}
// Sets of default parameters.
protected function grs80_params() {
$this->axis = 6378137.0; // GRS 80.
$this->flattening = 1.0 / 298.257222101; // GRS 80.
$this->central_meridian = null;
$this->lat_of_origin = 0.0;
}
protected function bessel_params() {
$this->axis = 6377397.155; // Bessel 1841.
$this->flattening = 1.0 / 299.1528128; // Bessel 1841.
$this->central_meridian = null;
$this->lat_of_origin = 0.0;
$this->scale = 1.0;
$this->false_northing = 0.0;
$this->false_easting = 1500000.0;
}
protected function sweref99_params() {
$this->axis = 6378137.0; // GRS 80.
$this->flattening = 1.0 / 298.257222101; // GRS 80.
$this->central_meridian = null;
$this->lat_of_origin = 0.0;
$this->scale = 1.0;
$this->false_northing = 0.0;
$this->false_easting = 150000.0;
}
// Conversion from geodetic coordinates to grid coordinates.
public function geodetic_to_grid($latitude, $longitude) {
$x_y = array(null, null);
if ($this->central_meridian == null) {
return $x_y;
}
// Prepare ellipsoid-based stuff.
$e2 = $this->flattening * (2.0 - $this->flattening);
$n = $this->flattening / (2.0 - $this->flattening);
$a_roof = $this->axis / (1.0 + $n) * (1.0 + $n*$n/4.0 + $n*$n*$n*$n/64.0);
$A = $e2;
$B = (5.0*$e2*$e2 - $e2*$e2*$e2) / 6.0;
$C = (104.0*$e2*$e2*$e2 - 45.0*$e2*$e2*$e2*$e2) / 120.0;
$D = (1237.0*$e2*$e2*$e2*$e2) / 1260.0;
$beta1 = $n/2.0 - 2.0*$n*$n/3.0 + 5.0*$n*$n*$n/16.0 + 41.0*$n*$n*$n*$n/180.0;
$beta2 = 13.0*$n*$n/48.0 - 3.0*$n*$n*$n/5.0 + 557.0*$n*$n*$n*$n/1440.0;
$beta3 = 61.0*$n*$n*$n/240.0 - 103.0*$n*$n*$n*$n/140.0;
$beta4 = 49561.0*$n*$n*$n*$n/161280.0;
// Convert.
$deg_to_rad = pi() / 180.0;
$phi = $latitude * $deg_to_rad;
$lambda = $longitude * $deg_to_rad;
$lambda_zero = $this->central_meridian * $deg_to_rad;
$phi_star = $phi - sin($phi) * cos($phi) * ($A +
$B*pow(sin($phi), 2) +
$C*pow(sin($phi), 4) +
$D*pow(sin($phi), 6));
$delta_lambda = $lambda - $lambda_zero;
$xi_prim = atan(tan($phi_star) / cos($delta_lambda));
$eta_prim = atanh(cos($phi_star) * sin($delta_lambda));
$x = $this->scale * $a_roof * ($xi_prim +
$beta1 * sin(2.0*$xi_prim) * cosh(2.0*$eta_prim) +
$beta2 * sin(4.0*$xi_prim) * cosh(4.0*$eta_prim) +
$beta3 * sin(6.0*$xi_prim) * cosh(6.0*$eta_prim) +
$beta4 * sin(8.0*$xi_prim) * cosh(8.0*$eta_prim)) +
$this->false_northing;
$y = $this->scale * $a_roof * ($eta_prim +
$beta1 * cos(2.0*$xi_prim) * sinh(2.0*$eta_prim) +
$beta2 * cos(4.0*$xi_prim) * sinh(4.0*$eta_prim) +
$beta3 * cos(6.0*$xi_prim) * sinh(6.0*$eta_prim) +
$beta4 * cos(8.0*$xi_prim) * sinh(8.0*$eta_prim)) +
$this->false_easting;
$x_y[0] = round($x * 1000.0) / 1000.0;
$x_y[1] = round($y * 1000.0) / 1000.0;
// x_y[0] = x;
// x_y[1] = y;
return $x_y;
}
// Conversion from grid coordinates to geodetic coordinates.
public function grid_to_geodetic($x, $y) {
$lat_lon = array(null, null);
if ($this->central_meridian == null) {
return $lat_lon;
}
// Prepare ellipsoid-based stuff.
$e2 = $this->flattening * (2.0 - $this->flattening);
$n = $this->flattening / (2.0 - $this->flattening);
$a_roof = $this->axis / (1.0 + $n) * (1.0 + $n*$n/4.0 + $n*$n*$n*$n/64.0);
$delta1 = $n/2.0 - 2.0*$n*$n/3.0 + 37.0*$n*$n*$n/96.0 - $n*$n*$n*$n/360.0;
$delta2 = $n*$n/48.0 + $n*$n*$n/15.0 - 437.0*$n*$n*$n*$n/1440.0;
$delta3 = 17.0*$n*$n*$n/480.0 - 37*$n*$n*$n*$n/840.0;
$delta4 = 4397.0*$n*$n*$n*$n/161280.0;
$Astar = $e2 + $e2*$e2 + $e2*$e2*$e2 + $e2*$e2*$e2*$e2;
$Bstar = -(7.0*$e2*$e2 + 17.0*$e2*$e2*$e2 + 30.0*$e2*$e2*$e2*$e2) / 6.0;
$Cstar = (224.0*$e2*$e2*$e2 + 889.0*$e2*$e2*$e2*$e2) / 120.0;
$Dstar = -(4279.0*$e2*$e2*$e2*$e2) / 1260.0;
// Convert.
$deg_to_rad = pi() / 180;
$lambda_zero = $this->central_meridian * $deg_to_rad;
$xi = ($x - $this->false_northing) / ($this->scale * $a_roof);
$eta = ($y - $this->false_easting) / ($this->scale * $a_roof);
$xi_prim = $xi -
$delta1*sin(2.0*$xi) * cosh(2.0*$eta) -
$delta2*sin(4.0*$xi) * cosh(4.0*$eta) -
$delta3*sin(6.0*$xi) * cosh(6.0*$eta) -
$delta4*sin(8.0*$xi) * cosh(8.0*$eta);
$eta_prim = $eta -
$delta1*cos(2.0*$xi) * sinh(2.0*$eta) -
$delta2*cos(4.0*$xi) * sinh(4.0*$eta) -
$delta3*cos(6.0*$xi) * sinh(6.0*$eta) -
$delta4*cos(8.0*$xi) * sinh(8.0*$eta);
$phi_star = asin(sin($xi_prim) / cosh($eta_prim));
$delta_lambda = atan(sinh($eta_prim) / cos($xi_prim));
$lon_radian = $lambda_zero + $delta_lambda;
$lat_radian = $phi_star + sin($phi_star) * cos($phi_star) *
($Astar +
$Bstar*pow(sin($phi_star), 2) +
$Cstar*pow(sin($phi_star), 4) +
$Dstar*pow(sin($phi_star), 6));
$lat_lon[0] = $lat_radian * 180.0 / pi();
$lat_lon[1] = $lon_radian * 180.0 / pi();
return $lat_lon;
}
}