-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGM96Geoid.swift
545 lines (420 loc) · 17.3 KB
/
EGM96Geoid.swift
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// class EGM96Geoid.swift
// ported to Swift by ChatGPT-4
// originally from Java package
// org.matthiaszimmermann.location.egm96;
// make sure to include EGM96complete.dat[.gz]
// and GeoidHeights.dat[.gz] in the bundle
// so they can be read
// mods and tweaks by Matthew Krupczak, Bobby Krupczak
import Foundation
import Compression
class EGM96Geoid {
static let OFFSET_INVALID = -9999.99
static let OFFSET_MISSING = 9999.99
static let ROWS = 719
static let COLS = 1440
static let LATITUDE_MAX = 90.0
static let LATITUDE_MAX_GRID = 89.74
static let LATITUDE_ROW_FIRST = 89.50
static let LATITUDE_ROW_LAST = -89.50
static let LATITUDE_MIN_GRID = -89.74
static let LATITUDE_MIN = -90.0
static let LATITUDE_STEP = 0.25
static let LONGITUDE_MIN = 0.0
static let LONGITUDE_MIN_GRID = 0.0
static let LONGITUDE_MAX_GRID = 359.75
static let LONGITUDE_MAX = 360.0
static let LONGITUDE_STEP = 0.25
static let INVALID_OFFSET = "-9999.99"
static let COMMENT_PREFIX = "//"
//private static var offset = Array(repeating: Array(repeating: 0.0, count: COLS), count: ROWS)
private static var offset = [[Double]]()
private static var offset_north_pole = 0.0
private static var offset_south_pole = 0.0
public static var s_model_ok = false
// call this function to set up the model and load it into RAM
static func initEGM96Geoid() -> Bool {
if s_model_ok {
return true
}
// instead of declaring using repeating, try this and see if it changes
// results XXX
for _ in 0..<ROWS {
var row = [Double]()
for _ in 0..<COLS {
row.append(0.0)
}
offset.append(row)
}
// EGM96complete.bin is gzip'd data file
if let filePath = Bundle.main.path(forResource: "EGM96complete", ofType: "bin") {
do {
var fileData = try Data(contentsOf: URL(fileURLWithPath: filePath))
//print("initEGM96Geoid: read data of size \(fileData.count) bytes")
// eat the first two bytes 0x78, 0x9c as a test as that might
// be screwing up the zlib decompression
// eat the first two bytes 0x78, 0x9c as a test
// zlib on iOS may not like these two bytes? XXX
fileData.removeFirst(2)
// remove last 4 bytes which is a checksum
fileData.removeLast(4)
//print("initEGM96Geoid: calling readEGM96GeoidOffsets with \(fileData.count) bytes")
s_model_ok = readEGM96GeoidOffsets(data: fileData)
} catch {
s_model_ok = false
print("Failed to read file: \(error)")
}
} else {
s_model_ok = false
print("File 'EGM96complete.dat' not found in the bundle.")
}
return s_model_ok
}
// return offset for lat/lng w/o caller creating EGM96Location object
// too many location classes already :(
static func getOffset(lat: Double, lng: Double) -> Double {
let l = EGM96Location(lat: lat, lng: lng)
return getOffset(location: l)
}
static func getOffset(location: EGM96Location) -> Double {
let lat = location.latitude
let lng = location.longitude
if latIsGridPoint(lat: lat) && lngIsGridPoint(lng: lng) {
return getGridOffset(lat: lat, lng: lng)
}
var q = Array(repeating: Array(repeating: EGM96Location(), count: 4), count: 4)
q[1][1] = getGridFloorLocation(lat: lat, lng: lng)
q[1][2] = getUpperLocation(location: q[1][1])
q[2][1] = getRightLocation(location: q[1][1])
q[2][2] = getUpperLocation(location: q[2][1])
if q[1][1].latitude >= LATITUDE_MIN_GRID && q[1][2].latitude <= LATITUDE_MAX_GRID {
// left column
q[0][1] = getLeftLocation(location: q[1][1])
q[0][2] = getUpperLocation(location: q[0][1])
q[0][3] = getUpperLocation(location: q[0][2])
// top row
q[1][3] = getRightLocation(location: q[0][3])
q[2][3] = getRightLocation(location: q[1][3])
q[3][3] = getRightLocation(location: q[2][3])
// bottom row
q[0][0] = getLowerLocation(location: q[0][1])
q[1][0] = getRightLocation(location: q[0][0])
q[2][0] = getRightLocation(location: q[1][0])
// right column
q[3][0] = getRightLocation(location: q[2][0])
q[3][1] = getUpperLocation(location: q[3][0])
q[3][2] = getUpperLocation(location: q[3][1])
return bicubicSplineInterpolation(target: location, grid: q)
} else {
return bilinearInterpolation(target: location, q11: q[1][1], q12: q[1][2], q21: q[2][1], q22: q[2][2])
}
}
static func bilinearInterpolation(target: EGM96Location, q11: EGM96Location, q12: EGM96Location, q21: EGM96Location, q22: EGM96Location) -> Double {
let fq11 = getGridOffset(location: q11)
let fq12 = getGridOffset(location: q12)
let fq21 = getGridOffset(location: q21)
let fq22 = getGridOffset(location: q22)
let x1 = q11.longitude
var x2 = q22.longitude
let y1 = q22.latitude
let y2 = q11.latitude
if x1 == 359.75 && x2 == 0.0 {
x2 = 360.0
}
let x = target.longitude
let y = target.latitude
let f11 = fq11 * (x2 - x) * (y2 - y)
let f12 = fq12 * (x2 - x) * (y - y1)
let f21 = fq21 * (x - x1) * (y2 - y)
let f22 = fq22 * (x - x1) * (y - y1)
return (f11 + f12 + f21 + f22) / ((x2 - x1) * (y2 - y1))
}
static func bicubicSplineInterpolation(target: EGM96Location, grid: [[EGM96Location]]) -> Double {
var G = Array(repeating: Array(repeating: 0.0, count: 4), count: 4)
for i in 0..<4 {
for j in 0..<4 {
G[i][j] = getGridOffset(location: grid[i][j])
}
}
let u1 = grid[1][1].latitude
let v1 = grid[1][1].longitude
let u = (target.latitude - u1 + LATITUDE_STEP) / (4 * LATITUDE_STEP)
let v = (target.longitude - v1 + LONGITUDE_STEP) / (4 * LONGITUDE_STEP)
let c = Cubic( matrix2D: Cubic.BEZIER, G: G)
return c.eval(u: u, v: v)
}
static func getUpperLocation(location: EGM96Location) -> EGM96Location {
var lat = location.latitude
let lng = location.longitude
if lat == LATITUDE_MAX_GRID {
lat = LATITUDE_MAX
} else if lat == LATITUDE_ROW_FIRST {
lat = LATITUDE_MAX_GRID
} else if lat == LATITUDE_MIN {
lat = LATITUDE_MIN_GRID
} else if lat == LATITUDE_MIN_GRID {
lat = LATITUDE_ROW_LAST
} else {
lat += LATITUDE_STEP
}
return EGM96Location(lat: lat, lng: lng)
}
static func getLowerLocation(location: EGM96Location) -> EGM96Location {
var lat = location.latitude
let lng = location.longitude
if lat == LATITUDE_MIN_GRID {
lat = LATITUDE_MIN
} else if lat == LATITUDE_ROW_FIRST {
lat = LATITUDE_MIN_GRID
} else if lat == LATITUDE_MAX {
lat = LATITUDE_MAX_GRID
} else if lat == LATITUDE_MAX_GRID {
lat = LATITUDE_ROW_FIRST
} else {
lat -= LATITUDE_STEP
}
return EGM96Location(lat: lat, lng: lng)
}
static func getLeftLocation(location: EGM96Location) -> EGM96Location {
let lat = location.latitude
var lng = location.longitude
lng -= LONGITUDE_STEP
return EGM96Location(lat: lat, lng: lng)
}
static func getRightLocation(location: EGM96Location) -> EGM96Location {
let lat = location.latitude
var lng = location.longitude
lng += LONGITUDE_STEP
return EGM96Location(lat: lat, lng: lng)
}
static func getGridFloorLocation(lat: Double, lng: Double) -> EGM96Location {
let floor = EGM96Location(lat: lat, lng: lng).floorLocation(step: LATITUDE_STEP)
var latFloor = floor.latitude
if lat >= LATITUDE_MAX_GRID && lat < LATITUDE_MAX {
latFloor = LATITUDE_MAX_GRID
} else if lat < LATITUDE_MIN_GRID {
latFloor = LATITUDE_MIN
} else if lat < LATITUDE_ROW_LAST {
latFloor = LATITUDE_MIN_GRID
}
return EGM96Location(lat: latFloor, lng: floor.longitude)
}
static func getGridOffset(location: EGM96Location) -> Double {
return getGridOffset(lat: location.latitude, lng: location.longitude)
}
static func getGridOffset(lat: Double, lng: Double) -> Double {
if !s_model_ok {
return OFFSET_INVALID
}
if !latIsGridPoint(lat: lat) || !lngIsGridPoint(lng: lng) {
return OFFSET_INVALID
}
if latIsPole(lat: lat) {
if lat == LATITUDE_MAX {
return offset_north_pole
} else {
return offset_south_pole
}
}
let i = latToI(lat: lat)
let j = lngToJ(lng: lng)
return offset[i][j]
}
// handle reading with dos \r\n In this version, the lines are
// split based on the DOS carriage return (\r) character, and each
// line is trimmed of any leading or trailing newlines.
private static func readEGM96GeoidOffsets(data: Data) -> Bool {
var numLines = 0
var numLinesLatLongOK = 0
var numLinesOK = 0
assignMissingOffsets()
var decompressedData: Data
do {
//print("Uncompressing \(data.count) bytes")
//print("First few bytes \(data.bytes[0..<10])")
decompressedData = try (data as NSData).decompressed(using: .zlib) as Data
//print("Decompression returned \(decompressedData.count) bytes")
} catch {
print("Failed to decompress data: \(error)")
return false
}
// print first few bytes of decompressed data
// print("First few uncompressed bytes \(decompressedData.bytes[0..<10])")
let lines = decompressedData.split(separator: 13) // 13 is the ASCII code for carriage return (\r)
for line in lines {
let lineString = String(data: line, encoding: .utf8)?.trimmingCharacters(in: .newlines) ?? ""
numLines += 1
if lineIsOk(line: lineString) {
numLinesOK += 1
let tokens = lineString.split(separator: " ")
if tokens.count != 3 {
print("Error: Found \(tokens.count) tokens on line: \(lineString)")
}
if let lat = Double(tokens[0]), let lng = Double(tokens[1]), let off = Double(tokens[2]) {
if latLongOk(lat: lat, lng: lng) {
numLinesLatLongOK += 1
var i_lat = 0
var j_lng = 0
if lat == LATITUDE_MAX {
offset_north_pole = off
} else if lat == LATITUDE_MIN {
offset_south_pole = off
} else {
if lat == LATITUDE_MAX_GRID {
i_lat = 0
} else if lat == LATITUDE_MIN_GRID {
i_lat = ROWS - 1
} else {
i_lat = Int((LATITUDE_MAX - lat) / LATITUDE_STEP) - 1
}
j_lng = Int(lng / LONGITUDE_STEP)
offset[i_lat][j_lng] = off
}
}
} else {
print("Error parsing line: \(lineString)")
}
}
}
print("EGM96Geod: parsed \(numLines) lines \(numLinesOK) valid lines \(numLinesLatLongOK) valid lat/lng lines")
return !hasMissingOffsets()
}
private static func readEGM96GeoidOffsetsUNIX(data: Data) -> Bool {
assignMissingOffsets()
let decompressedData: Data
do {
decompressedData = try (data as NSData).decompressed(using: .zlib) as Data
} catch {
print("Failed to decompress data: \(error)")
return false
}
let lines = decompressedData.split(separator: 10) // 10 is the ASCII code for line feed (\n)
for line in lines {
let lineString = String(data: line, encoding: .utf8) ?? ""
if lineIsOk(line: lineString) {
let tokens = lineString.split(separator: " ")
if tokens.count != 3 {
print("Error: Found \(tokens.count) tokens on line: \(lineString)")
}
if let lat = Double(tokens[0]), let lng = Double(tokens[1]), let off = Double(tokens[2]) {
if latLongOk(lat: lat, lng: lng) {
var i_lat = 0
var j_lng = 0
if lat == LATITUDE_MAX {
offset_north_pole = off
} else if lat == LATITUDE_MIN {
offset_south_pole = off
} else {
if lat == LATITUDE_MAX_GRID {
i_lat = 0
} else if lat == LATITUDE_MIN_GRID {
i_lat = ROWS - 1
} else {
i_lat = Int((LATITUDE_MAX - lat) / LATITUDE_STEP) - 1
}
j_lng = Int(lng / LONGITUDE_STEP)
offset[i_lat][j_lng] = off
}
}
} else {
print("Error parsing line: \(lineString)")
}
}
}
return !hasMissingOffsets()
}
private static func lineIsOk(line: String) -> Bool {
if line.hasPrefix(COMMENT_PREFIX) {
return false
}
if line.hasSuffix(INVALID_OFFSET) {
return false
}
// eat blank links or lines with space at beginning -- rdk
if line.isEmpty {
print("EGM96Geoid: Skipping parsing empty line")
return false
}
return true
}
private static func assignMissingOffsets() {
offset_north_pole = OFFSET_MISSING
offset_south_pole = OFFSET_MISSING
for i in 0..<ROWS {
for j in 0..<COLS {
offset[i][j] = OFFSET_MISSING
}
}
}
private static func hasMissingOffsets() -> Bool {
if offset_north_pole == OFFSET_MISSING { return true }
if offset_south_pole == OFFSET_MISSING { return true }
for i in 0..<ROWS {
for j in 0..<COLS {
if offset[i][j] == OFFSET_MISSING {
return true
}
}
}
return false
}
// ChatGPT forgot to include this function!
// ChatGPT botched line arg so we dropped it; only used for error printing
private static func latLongOk(lat: Double, lng: Double) -> Bool
{
if latOk(lat: lat) == false {
return false
}
if lngOkGrid(lng: lng) == false {
return false
}
return true
}
private static func latOk(lat: Double) -> Bool {
let lat_in_bounds = lat >= LATITUDE_MIN && lat <= LATITUDE_MAX
return lat_in_bounds
}
private static func lngOk(lng: Double) -> Bool {
let lng_in_bounds = lng >= LONGITUDE_MIN && lng <= LONGITUDE_MAX
return lng_in_bounds
}
private static func lngOkGrid(lng: Double) -> Bool {
let lng_in_bounds = lng >= LONGITUDE_MIN_GRID && lng <= LONGITUDE_MAX_GRID
return lng_in_bounds
}
private static func latIsGridPoint(lat: Double) -> Bool {
if !latOk(lat: lat) {
return false
}
if latIsPole(lat: lat) {
return true
}
if lat == LATITUDE_MAX_GRID || lat == LATITUDE_MIN_GRID {
return true
}
if lat <= LATITUDE_ROW_FIRST && lat >= LATITUDE_ROW_LAST && lat / LATITUDE_STEP == Double(Int(lat / LATITUDE_STEP)) {
return true
}
return false
}
private static func lngIsGridPoint(lng: Double) -> Bool {
if !lngOkGrid(lng: lng) {
return false
}
if lng / LONGITUDE_STEP == Double(Int(lng / LONGITUDE_STEP)) {
return true
}
return false
}
private static func latIsPole(lat: Double) -> Bool {
return lat == LATITUDE_MAX || lat == LATITUDE_MIN
}
private static func latToI(lat: Double) -> Int {
if lat == LATITUDE_MAX_GRID { return 0 }
if lat == LATITUDE_MIN_GRID { return ROWS - 1 }
return Int((LATITUDE_ROW_FIRST - lat) / LATITUDE_STEP) + 1
}
private static func lngToJ(lng: Double) -> Int {
return Int(lng / LONGITUDE_STEP)
}
} // EGM96Geoid