-
Notifications
You must be signed in to change notification settings - Fork 3
/
SFCIndex.py
1449 lines (1020 loc) · 38.5 KB
/
SFCIndex.py
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Author: Neeraj Sirdeshmukh
Delft University Of Technology, Netherlands, 2018
'''
import math
from math import *
from pyproj import *
from laspy.file import File
import numpy as np
import time
import pandas
import os
import psycopg2
from psycopg2.extras import *
from io import StringIO
class isea_pt(object):
def __init__(self, x, y):
self.x = x
self.y = y
class isea_geo(object):
# in radians
def __init__(self, lon, lat):
self.lon = lon
self.lat = lat
# Set constants
M_PI = 3.14159265358979323846
E = 52.62263186
F = 10.81231696
DEG60 =1.04719755119659774614
DEG120= 2.09439510239319549229
DEG72 =1.25663706143591729537
DEG90 =1.57079632679489661922
DEG144= 2.51327412287183459075
DEG36 =0.62831853071795864768
DEG108 =1.88495559215387594306
DEG180= M_PI
ISEA_SCALE=0.8301572857837594396028083
# Degrees: 26.565051177065900134
V_LAT = 0.46364760899944494524
RAD2DEG = 180.0/M_PI
DEG2RAD = M_PI/180.0
# Icosahedron constants, radians
g =37.37736814 * DEG2RAD
G =36.0 * DEG2RAD
theta =30.0 * DEG2RAD
# vertices of triangles
vertex = [isea_geo(0.0,DEG90),
isea_geo(DEG180,V_LAT),
isea_geo(-DEG108,V_LAT),
isea_geo(-DEG36,V_LAT),
isea_geo(DEG36,V_LAT),
isea_geo(DEG108,V_LAT),
isea_geo(-DEG144,-V_LAT),
isea_geo(-DEG72,-V_LAT),
isea_geo(0.0,-V_LAT),
isea_geo(DEG72,-V_LAT),
isea_geo(DEG144,-V_LAT),
isea_geo(0.0,-DEG90)
]
E_RAD =0.91843818702186776133
F_RAD =0.18871053072122403508
# icosahedron triangle centers
icostriangles = [
isea_geo(0.0, 0.0),
isea_geo(-DEG144, E_RAD),
isea_geo(-DEG72, E_RAD),
isea_geo(0.0, E_RAD),
isea_geo(DEG72, E_RAD),
isea_geo(DEG144, E_RAD),
isea_geo(-DEG144, F_RAD),
isea_geo(-DEG72, F_RAD),
isea_geo(0.0, F_RAD),
isea_geo(DEG72, F_RAD),
isea_geo(DEG144, F_RAD),
isea_geo(-DEG108, -F_RAD),
isea_geo(-DEG36, -F_RAD),
isea_geo(DEG36, -F_RAD),
isea_geo(DEG108, -F_RAD),
isea_geo(DEG180, -F_RAD),
isea_geo(-DEG108, -E_RAD),
isea_geo(-DEG36, -E_RAD),
isea_geo(DEG36, -E_RAD),
isea_geo(DEG108, -E_RAD),
isea_geo(DEG180, -E_RAD)
]
# Parameters taken from Snyder (1992)
TABLE_G = 0.6615845383
TABLE_H = 0.1909830056
RPRIME = 0.91038328153090290025
tri_v1 = [0, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 2, 3, 4, 5, 1, 11, 11, 11, 11, 11]
NEWORIGX = -0.6022955012659694 # TABLE_G * (-1) #old:
NEWORIGY = -0.3477354703761901 # TABLE_H * (-2) #old:
TANTHETA = tan(theta)
COTTHETA = 1.0 / TANTHETA
SINUPPERG = sin(G)
COSUPPERG = cos(G)
COSLOWERG = cos(g)
TANLOWERG = tan(g)
# parameters for WGS84 ellipsoid
R = 6378137 # semi-major axis, meters
b = 6356752.314245 # semi-minor axis, meters
flattening = 1/298.257223563
def az_adjustment(triangle):
v = vertex[tri_v1[triangle]] # vertex ID of triangle
c = icostriangles[triangle] # center of triangle
# Azimuth from vertex to center of triangle
adj = atan2(cos(v.lat) * sin(v.lon - c.lon),cos(c.lat) * sin(v.lat)- sin(c.lat) * cos(v.lat) * cos(v.lon - c.lon))
return adj
def isea_snyder_forward(ll):
for i in range(1,21):
center = icostriangles[i];
# step 1 , returns z(scaled meters) and Az (in radians)
z, Az = vincentyInverse(center.lat, center.lon, ll.lat, ll.lon)
if (Az > M_PI):
Az = Az - (2*M_PI)
# not on this triangle
if (z > g):
continue
# step 2
# This calculates a vertex coordinate whose azimuth is going to be assigned 0
az_offset = az_adjustment(i)
# This gives that vertex an azimuth of 0. For south pointing triangles the range of azimuths changes
# from [-3.14 - 3.14] to [-6.28 0].
# For north pointing triangles, makes no difference.
Az -= az_offset
# Adjust Az to fall between 0 and 120 degrees, record adjustment amount
Az_adjust_multiples = 0;
while Az < 0.0:
Az += DEG120
Az_adjust_multiples-=1
while (Az > DEG120):
Az -= DEG120
Az_adjust_multiples+=1
# Calculate q from eq 9.
q = atan(TANLOWERG/(cos(Az) + (sin(Az)*COTTHETA)))
# not in this triangle
if (z > q):
continue
# Apply equations 5-8 and 10-12 in order
# eq 6
H = acos((sin(Az) * SINUPPERG * COSLOWERG) - (cos(Az) * COSUPPERG))
#eq 7
AG = (Az + G + H - DEG180)
# eq 8
Azprime = atan2((2.0 * AG), ((RPRIME * RPRIME* TANLOWERG * TANLOWERG) - (2.0 * AG * COTTHETA)))
# eq 10
dprime = (RPRIME * TANLOWERG) / (cos(Azprime) + (sin(Azprime) * COTTHETA))
# eq 11
f = dprime / (2.0 * RPRIME * sin(q / 2.0))
# eq 12
rho = 2.0 * RPRIME * f * sin(z / 2.0)
#add back the same 120 degree multiple adjustment from step 2 to Azprime
Azprime += DEG120 * Az_adjust_multiples
# calculate rectangular coordinates
x = rho * sin(Azprime)
y = rho * cos(Azprime)
out = isea_pt(x,y)
return out, i
def computeMorton2D(longitude, latitude, res):
point = isea_geo(longitude * DEG2RAD, latitude * DEG2RAD)
# out contains coordinates from center of triangle
out, face = isea_snyder_forward(point)
# Find new coordinates of point from lower left/upper left origin
newPointX = 0
newPointY= 0
if ((face >= 1 and face <=5) or (face>=11 and face <=15)):
newPointX = out.x - NEWORIGX
newPointY = out.y - NEWORIGY
else:
newPointX = (out.x + NEWORIGX) * (-1)
newPointY = (out.y - NEWORIGY) * (-1)
# Rotate the axes, round down to nearest integer since addressing begins at 0
# Scale coordinates of all dimensions to match resolution of DGGS
origX = ((newPointX - ((1/(sqrt(3))) * newPointY)) /(NEWORIGX *(-2))) * totRange
origY = ((newPointX + ((1/(sqrt(3))) * newPointY))/(NEWORIGX *(-2))) * totRange
rotatedX = int(origX)
rotatedY = int(origY)
# Convert to binary
xBin = ('{0:0' + str(res) + 'b}').format(rotatedX)
yBin = ('{0:0' + str(res) + 'b}').format(rotatedY)
# Convert binary to int and use Morton formula
morton = ('{0:0' + str(res) + '}').format(2 * int(yBin) + int(xBin))
# Convert triangle face number to rhombus face number
if face == 1 or face == 6:
face = 0
elif face == 11 or face == 16:
face = 1
elif face == 2 or face == 7:
face = 2
elif face == 12 or face == 17:
face = 3
elif face == 3 or face == 8:
face = 4
elif face == 13 or face == 18:
face = 5
elif face == 4 or face == 9:
face = 6
elif face == 14 or face == 19:
face = 7
elif face == 5 or face == 10:
face = 8
else:
face = 9
fullCode = str(face) + morton
return fullCode
def computeMorton3D(longitude, latitude, height, hrange, res):
point = isea_geo(longitude * DEG2RAD, latitude * DEG2RAD)
#out contains coordinates from center of triangle
out, face = isea_snyder_forward(point)
# Find new coordinates of point from lower left/upper left origin
newPointX = 0
newPointY= 0
if ((face >= 1 and face <=5) or (face>=11 and face <=15)):
newPointX = out.x - NEWORIGX
newPointY = out.y - NEWORIGY
else:
newPointX = (out.x + NEWORIGX) * (-1)
newPointY = (out.y - NEWORIGY) * (-1)
# Rotate the axes, round down to nearest integer since addressing begins at 0
# Scale coordinates of all dimensions to match resolution of DGGS
origX = ((newPointX - ((1/(sqrt(3))) * newPointY)) /(NEWORIGX *(-2))) * totRange
origY = ((newPointX + ((1/(sqrt(3))) * newPointY))/(NEWORIGX *(-2))) * totRange
Z = 0
if height <=0:
Z = ((hrange - ((-1) * height)) / (hrange * 2)) * totRange
else:
Z = ((hrange + height) / (hrange * 2)) * totRange
rotatedX = int(origX)
rotatedY = int(origY)
intZ = int(Z)
# Convert to binary
xBin = ('{0:0' + str(res) + 'b}').format(rotatedX)
yBin = ('{0:0' + str(res) + 'b}').format(rotatedY)
zBin = ('{0:0' + str(res) + 'b}').format(intZ)
# Convert binary to int and use Morton formula
morton = ('{0:0' + str(res) + '}').format(4 * int(zBin) + 2 * int(yBin) + int(xBin))
# Convert triangle face number to rhombus face number
if face == 1 or face == 6:
face = 0
elif face == 11 or face == 16:
face = 1
elif face == 2 or face == 7:
face = 2
elif face == 12 or face == 17:
face = 3
elif face == 3 or face == 8:
face = 4
elif face == 13 or face == 18:
face = 5
elif face == 4 or face == 9:
face = 6
elif face == 14 or face == 19:
face = 7
elif face == 5 or face == 10:
face = 8
else:
face = 9
fullCode = str(face) + morton
return fullCode
def computeMorton4D(longitude, latitude, height, timeSec, hrange, trange, res):
point = isea_geo(longitude * DEG2RAD, latitude * DEG2RAD)
#out contains coordinates from center of triangle
out, face = isea_snyder_forward(point)
# Find new coordinates of point from lower left/upper left origin
newPointX = 0
newPointY= 0
if ((face >= 1 and face <=5) or (face>=11 and face <=15)):
newPointX = out.x - NEWORIGX
newPointY = out.y - NEWORIGY
else:
newPointX = (out.x + NEWORIGX) * (-1)
newPointY = (out.y - NEWORIGY) * (-1)
# Rotate the axes, round down to nearest integer since addressing begins at 0
# Scale coordinates of all dimensions to match resolution of DGGS
origX = ((newPointX - ((1/(sqrt(3))) * newPointY)) /(NEWORIGX *(-2))) * totRange
origY = ((newPointX + ((1/(sqrt(3))) * newPointY))/(NEWORIGX *(-2))) * totRange
Z = 0
if height <=0:
Z = ((hrange - ((-1) * height)) / (hrange * 2)) * totRange
else:
Z = ((hrange + height) / (hrange * 2)) * totRange
# Subtract 18 seconds to get UTC time
T = ((timeSec - 18)/trange) * totRange
rotatedX = int(origX)
rotatedY = int(origY)
intZ = int(Z)
intT = int(T)
# Convert to binary
xBin = ('{0:0' + str(res) + 'b}').format(rotatedX)
yBin = ('{0:0' + str(res) + 'b}').format(rotatedY)
zBin = ('{0:0' + str(res) + 'b}').format(intZ)
tBin = ('{0:0' + str(res) + 'b}').format(intT)
tVal = int('0'.join(str(8 * int(tBin))))
zVal = int('0'.join(str(4 * int(zBin))))
yVal = int('0'.join(str(2 * int(yBin))))
xVal = int('0'.join(str(int(xBin))))
total = tVal + zVal + yVal + xVal
# Convert binary to int and use Morton formula
# Drop 0 (if any) after face number to save 1 bit!
morton3D = ('{0:0' + str(res) + '}').format(4 * int(zBin) + 2 * int(yBin) + int(xBin))
morton4D = ('{0:0' + str((2* res)) + '}').format(total)
# Convert triangle face number to rhombus face number
if face == 1 or face == 6:
face = 0
elif face == 11 or face == 16:
face = 1
elif face == 2 or face == 7:
face = 2
elif face == 12 or face == 17:
face = 3
elif face == 3 or face == 8:
face = 4
elif face == 13 or face == 18:
face = 5
elif face == 4 or face == 9:
face = 6
elif face == 14 or face == 19:
face = 7
elif face == 5 or face == 10:
face = 8
else:
face = 9
fullCode4D = str(face) + morton4D
fullCode3D = str(face) + morton3D
return fullCode4D, fullCode3D
def decodeMortonToXY(mortonCode):
# Get face number and morton code
face = mortonCode[0] # First number indicates rhombus face!
morton = mortonCode[1:len(mortonCode)] # String
res = len(morton) #discrete!
lastDig = int(morton[-1:])
numXValues = totRange # Amount of possible X values
numYValues = totRange # Amount of possible Y values
# Compute the X, Y, and Z values on rhombus... markers = middle value
xMarker = numXValues/2
yMarker = numYValues/2
for i in range(res-1):
if (int(morton[i]) %2 ==0):
numXValues = numXValues/2
xMarker = xMarker - numXValues/2
else:
numXValues = numXValues/2
xMarker = xMarker + numXValues/2
if (int(morton[i]) <= 1):
numYValues = numYValues/2
yMarker = yMarker - numYValues/2
else:
numYValues = numYValues/2
yMarker = yMarker + numYValues/2
# Look at last digit
if lastDig %2 ==0:
xMarker = (xMarker - 1)
else:
xMarker = (xMarker)
if lastDig <=1:
yMarker = (yMarker -1)
else:
yMarker = (yMarker)
return (xMarker,yMarker,int(face), res)
def decodeMortonToXYH(mortonCode):
# Get face number and Morton code
face = mortonCode[0] # First number indicates rhombus face!
morton = mortonCode[1:len(mortonCode)] # String
res = len(morton) # discrete!
lastDig = int(morton[-1:])
numXValues = totRange # Amount of possible X values
numYValues = totRange # Amount of possible Y values
numZValues = totRange # Amount of possible Z values
# Compute the X, Y, and Z values on rhombus... markers = middle value
xMarker = numXValues/2
yMarker = numYValues/2
zMarker = numZValues/2
yVals = [0,1,4,5]
for i in range(res-1):
if (int(morton[i]) %2 ==0):
numXValues = numXValues/2
xMarker = xMarker - numXValues/2
else:
numXValues = numXValues/2
xMarker = xMarker + numXValues/2
if (int(morton[i]) in yVals):
numYValues = numYValues/2
yMarker = yMarker - numYValues/2
else:
numYValues = numYValues/2
yMarker = yMarker + numYValues/2
if (int(morton[i]) <= 3):
numZValues = numZValues/2
zMarker = zMarker - numZValues/2
else:
numZValues = numZValues/2
zMarker = zMarker + numZValues/2
# Look at last digit
if lastDig %2 ==0: # 0,2
xMarker = (xMarker - 1)
else:
xMarker = (xMarker)
if lastDig in yVals:
yMarker = (yMarker -1)
else:
yMarker = (yMarker)
if lastDig <= 3:
zMarker = (zMarker -1)
else:
zMarker = (zMarker)
return (xMarker,yMarker, zMarker, int(face), res)
def decodeMortonToXYHT(mortonCode):
# Get face number and morton code
face = mortonCode[0] # First number indicates rhombus face!
morton = mortonCode[1:len(mortonCode)] # String
res = len(morton) /2
lastDig = int(morton[-2:])
numXValues = totRange # Amount of possible X values
numYValues = totRange# Amount of possible Y values
numZValues = totRange # Amount of possible Z values
numTValues = totRange # Amount of possible T values
# Compute the X, Y, Z, and T values on rhombus... markers = middle value
xMarker = numXValues/2
yMarker = numYValues/2
zMarker = numZValues/2
tMarker = numTValues/2
yVals = [0,1,4,5,8,9,12,13]
zVals = [0,1,2,3,8,9,10,11]
for i in range(res-1):
i = i * 2
if (int(morton[i:i+2]) %2 ==0):
numXValues = numXValues/2
xMarker = xMarker - numXValues/2
else:
numXValues = numXValues/2
xMarker = xMarker + numXValues/2
if (int(morton[i:i+2]) in yVals):
numYValues = numYValues/2
yMarker = yMarker - numYValues/2
else:
numYValues = numYValues/2
yMarker = yMarker + numYValues/2
if (int(morton[i:i+2]) in zVals):
numZValues = numZValues/2
zMarker = zMarker - numZValues/2
else:
numZValues = numZValues/2
zMarker = zMarker + numZValues/2
if (int(morton[i:i+2]) <= 7):
numTValues = numTValues/2
tMarker = tMarker - numTValues/2
else:
numTValues = numTValues/2
tMarker = tMarker + numTValues/2
# Look at last digit
if lastDig %2 ==0:
xMarker = (xMarker - 1)
else:
xMarker = (xMarker)
if lastDig in yVals:
yMarker = (yMarker -1)
else:
yMarker = (yMarker)
if lastDig in zVals:
zMarker = (zMarker -1)
else:
zMarker = (zMarker)
if lastDig <=7:
tMarker = (tMarker -1)
else:
tMarker = (tMarker)
return (xMarker,yMarker, zMarker, tMarker, int(face), res)
def MortonToLatLong2D(x,y, face, res):
# Scale coordinates to scale of Cartesian system
scaledX = (x/totRange) * (-NEWORIGX *2)
scaledY = (y/totRange)*(-NEWORIGX*2)
# Convert coordinates from skewed system to Cartesian system (origin at left)
a = np.array([[1,(-1/sqrt(3))], [1,(1/sqrt(3))]])
b = np.array([scaledX,scaledY])
x = np.linalg.solve(a, b)
xCoord = x[0]
yCoord = x[1]
# Get triangle face from rhombus face based on values of y.
# If y is negative, triangles will be downward oriented
if yCoord >=0:
if (face == 0):
face = 1
elif (face == 1):
face = 11
elif (face == 2):
face = 2
elif (face == 3):
face = 12
elif (face == 4):
face = 3
elif (face == 5):
face = 13
elif (face == 6):
face = 4
elif (face == 7):
face = 14
elif (face == 8):
face = 5
elif (face == 9):
face = 15
else:
if (face == 0):
face = 6
elif (face == 1):
face = 16
elif (face == 2):
face = 7
elif (face == 3):
face = 17
elif (face == 4):
face = 8
elif (face == 5):
face = 18
elif (face == 6):
face = 9
elif (face == 7):
face = 19
elif (face == 8):
face = 10
elif (face == 9):
face = 20
# Translate coordinates to center (origin) of icosahedron triangle,
# taking into account triangle orientation
xOrigin = 0
yOrigin = 0
if ((face >= 1 and face <=5) or (face>=11 and face <=15)):
xOrigin = ((-NEWORIGX) - xCoord) * (-1)
yOrigin = ((-NEWORIGY) - yCoord) * (-1)
else:
xOrigin = (-NEWORIGX) - xCoord
yOrigin = ((-NEWORIGY) + yCoord) * (-1)
# Equation 17
Azprime = atan2(xOrigin, yOrigin)
# Equation 18
rho = sqrt((pow(xOrigin,2) + pow(yOrigin,2)))
# Adjust Azprime to fall within 0 to 120 degrees
Azprime_adjust_multiples = 0;
while Azprime < 0.0:
Azprime += DEG120
Azprime_adjust_multiples-=1
while (Azprime > DEG120):
Azprime -= DEG120
Azprime_adjust_multiples+=1
AzprimeCopy = Azprime
# Equation 19
AG = (pow(RPRIME,2) * pow(TANLOWERG,2)) / (2 * ((1/(tan(Azprime))) + COTTHETA))
# Iteration, Azprime (plane) converges to Az (sphere)
for i in range(4):
H = acos((sin(Azprime) * SINUPPERG * COSLOWERG) - (cos(Azprime) * COSUPPERG))
FAZ = (AG - G - H - Azprime + DEG180)
FPRIMEAZ = (((cos(Azprime) * SINUPPERG * COSLOWERG) + (sin(Azprime) * COSUPPERG)) / (sin(H))) - 1
DeltaAzprime = -FAZ/(FPRIMEAZ)
Azprime = Azprime + DeltaAzprime
Az = Azprime
# Equations 9-11, 23 to obtain z
q = atan((TANLOWERG)/(cos(Az) + (sin(Az)*COTTHETA)))
# eq 10
dprime = ((RPRIME * TANLOWERG) / (cos(AzprimeCopy) + (sin(AzprimeCopy) * COTTHETA)))
# eq 11
f = dprime / (2.0 * RPRIME * sin(q / 2.0))
# eq 23, obtain z
z = 2 * asin((rho)/(2*RPRIME*f))
# Add back 120 degree adjustments to Az
Az += DEG120 * Azprime_adjust_multiples
# Adjust Az to be clockwise from north (needed for final calculation)
if (face >=1 and face<=5) or (face>=11 and face<=15):
if (Az <0):
Az = (M_PI - (Az * (-1))) + M_PI
else:
if (Az <0):
Az = M_PI - (Az * (-1))
else:
Az = Az + M_PI
z = z * R
# triangle center
center = icostriangles[face]
lat2, lon2 = vincentyDirect(flattening, R, center.lat * RAD2DEG, center.lon * RAD2DEG, Az * RAD2DEG, z)
return lat2, lon2
def MortonToLatLong3D(x,y,h,face, res):
# Convert h/Z to height above/below ellipsoid
height = ((-1) * hrange) + ((h / totRange) * (2 * hrange))
# Scale coordinates to scale of Cartesian system
scaledX = (x/totRange) * (-NEWORIGX *2)
scaledY = (y/totRange)*(-NEWORIGX*2)
# Convert coordinates from skewed system to Cartesian system (origin at left)
a = np.array([[1,(-1/sqrt(3))], [1,(1/sqrt(3))]])
b = np.array([scaledX,scaledY])
x = np.linalg.solve(a, b)
xCoord = x[0]
yCoord = x[1]
# Get triangle face from rhombus face based on values of y.
# If y is negative, triangles will be downward oriented
if yCoord >=0:
if (face == 0):
face = 1
elif (face == 1):
face = 11
elif (face == 2):
face = 2
elif (face == 3):
face = 12
elif (face == 4):
face = 3
elif (face == 5):
face = 13
elif (face == 6):
face = 4
elif (face == 7):
face = 14
elif (face == 8):
face = 5
elif (face == 9):
face = 15
else:
if (face == 0):
face = 6
elif (face == 1):
face = 16
elif (face == 2):
face = 7
elif (face == 3):
face = 17
elif (face == 4):
face = 8
elif (face == 5):
face = 18
elif (face == 6):
face = 9
elif (face == 7):
face = 19
elif (face == 8):
face = 10
elif (face == 9):
face = 20
# Translate coordinates to center (origin) of icosahedron triangle,
# taking into account triangle orientation
xOrigin = 0
yOrigin = 0
if ((face >= 1 and face <=5) or (face>=11 and face <=15)):
xOrigin = ((-NEWORIGX) - xCoord) * (-1)
yOrigin = ((-NEWORIGY) - yCoord) * (-1)
else:
xOrigin = (-NEWORIGX) - xCoord
yOrigin = ((-NEWORIGY) + yCoord) * (-1)
# Equation 17
Azprime = atan2(xOrigin, yOrigin)
# Equation 18
rho = sqrt((pow(xOrigin,2) + pow(yOrigin,2)))
# Adjust Azprime to fall within 0 to 120 degrees
Azprime_adjust_multiples = 0;
while Azprime < 0.0:
Azprime += DEG120
Azprime_adjust_multiples-=1
while (Azprime > DEG120):
Azprime -= DEG120
Azprime_adjust_multiples+=1
AzprimeCopy = Azprime
#Equation 19
AG = (pow(RPRIME,2) * pow(TANLOWERG,2)) / (2 * ((1/(tan(Azprime))) + COTTHETA))
# Iteration, Azprime (plane) converges to Az (ellipsoid)
for i in range(4):
H = acos((sin(Azprime) * SINUPPERG * COSLOWERG) - (cos(Azprime) * COSUPPERG))
FAZ = (AG - G - H - Azprime + DEG180)
FPRIMEAZ = (((cos(Azprime) * SINUPPERG * COSLOWERG) + (sin(Azprime) * COSUPPERG)) / (sin(H))) - 1
DeltaAzprime = -FAZ/(FPRIMEAZ)
Azprime = Azprime + DeltaAzprime
Az = Azprime
# Equations 9-11, 23 to obtain z
q = atan((TANLOWERG)/(cos(Az) + (sin(Az)*COTTHETA)))
# eq 10
dprime = ((RPRIME * TANLOWERG) / (cos(AzprimeCopy) + (sin(AzprimeCopy) * COTTHETA)))
# eq 11
f = dprime / (2.0 * RPRIME * sin(q / 2.0))
#eq 23, obtain z
z = 2 * asin((rho)/(2*RPRIME*f))
# Add back 120 degree adjustments to Az
Az += DEG120 * Azprime_adjust_multiples
# Adjust Az to be clockwise from north (needed for final calculation)
if (face >=1 and face<=5) or (face>=11 and face<=15):
if (Az <0):
Az = (M_PI - (Az * (-1))) + M_PI
else:
if (Az <0):
Az = M_PI - (Az * (-1))
else:
Az = Az + M_PI
z = z * R
# triangle center
center = icostriangles[face]
lat2, lon2 = vincentyDirect(flattening, R, center.lat * RAD2DEG, center.lon * RAD2DEG, Az * RAD2DEG, z)
return lat2, lon2, height
def MortonToLatLong4D(x,y,h,t, face, res):
# Convert h to height above sphere/ellipsoid
height = ((-1) * hrange) + ((h / totRange) * (2 * hrange))