-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathykj2tm35.py
112 lines (88 loc) · 4.08 KB
/
ykj2tm35.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
import math
import pyqtree
import time
class YKJ2TM35:
def __init__(self):
self.triangle_points = {}
self.triangles = {}
self.triagindexes = {
'tm35':pyqtree.Index(bbox=(50199.4814, 6582464.0358, 761274.6247, 7799839.8902)),
'ykj':pyqtree.Index(bbox=(3049997.0487, 6585250.7768, 3761392.2103, 7803169.4895))
}
self.loadTriangles()
self.loadTriangleParams('KKJ_TO_ETRS_TM35FIN.txt','fwd')
self.loadTriangleParams('ETRS_TM35FIN_TO_KKJ.txt','inv')
def loadTriangles(self):
f = open('kkjEUREFFINtriangulationVertices.txt','rb')
for l in f:
l = l.strip().split('\t')
pid = int(l[0])
self.triangle_points[pid] = {
'ykj':tuple(reversed(map(float,l[1:3]))),
'tm35':tuple(reversed(map(float,l[3:5])))
}
f.close()
def loadTriangleParams(self,fn,convtype):
f = open(fn,'rb')
for l in f:
l = l.strip().split('\t')
l[:4] = map(int,l[:4])
l[4:] = map(float,l[4:])
tid = l[3]
if not tid in self.triangles:
self.triangles[tid] = {
'id':tid,
'fwd':None,
'inv':None,
'tids':tuple(l[:3]),
'triag': tuple((self.triangle_points[tpid] for tpid in l[:3])),
'bbox_ykj':(
min((self.triangle_points[tpid]['ykj'][0] for tpid in (l[:3]))),
min((self.triangle_points[tpid]['ykj'][1] for tpid in (l[:3]))),
max((self.triangle_points[tpid]['ykj'][0] for tpid in (l[:3]))),
max((self.triangle_points[tpid]['ykj'][1] for tpid in (l[:3]))),
),
'bbox_tm35':(
min((self.triangle_points[tpid]['tm35'][0] for tpid in (l[:3]))),
min((self.triangle_points[tpid]['tm35'][1] for tpid in (l[:3]))),
max((self.triangle_points[tpid]['tm35'][0] for tpid in (l[:3]))),
max((self.triangle_points[tpid]['tm35'][1] for tpid in (l[:3]))),
)
}
self.triagindexes['tm35'].insert(item=tid,bbox=self.triangles[tid]['bbox_tm35'])
self.triagindexes['ykj'].insert(item=tid,bbox=self.triangles[tid]['bbox_ykj'])
self.triangles[tid][convtype] = {}
for i,k in enumerate(('a1','a2','dE','b1','b2','dN')):
self.triangles[tid][convtype][k] = l[4+i]
f.close()
def __doAffineTransformation(self,p,params):
return (
params['a1'] * p[1] + params['a2'] * p[0] + params['dE'],
params['b1'] * p[1] + params['b2'] * p[0] + params['dN']
)
def __getTriangleForPoint(self,p,type):
idxtriags = self.triagindexes[type].intersect((p[0],p[1],p[0],p[1]))
triag_match = None
for tid in idxtriags:
t = self.triangles[tid]
bbox = t['bbox_%s' % type]
if p[0] < bbox[0] or p[1] < bbox[1]:
continue
if p[0] > bbox[2] or p[1] > bbox[3]:
continue
if self.__ptInTriangle(p,(tp[type] for tp in t['triag'])):
triag_match = t
return triag_match
def __ptInTriangle(self,p,triag):
p0,p1,p2 = triag
A = 1.0 / 2.0 * (-p1[1] * p2[0] + p0[1] * (-p1[0] + p2[0]) + p0[0] * (p1[1] - p2[1]) + p1[0] * p2[1])
s = math.copysign((p0[1] * p2[0] - p0[0] * p2[1] + (p2[1] - p0[1]) * p[0] + (p0[0] - p2[0]) * p[1]),A)
t = math.copysign((p0[0] * p1[1] - p0[1] * p1[0] + (p0[1] - p1[1]) * p[0] + (p1[0] - p0[0]) * p[1]),A)
return s > 0 and t > 0 and (s + t) < (2.0 * A*math.copysign(1,A))
def fwd(self,p):
triag = self.__getTriangleForPoint(p, 'ykj');
assert triag
assert triag['fwd']
return self.__doAffineTransformation(p, triag['fwd']);
if __name__ == '__main__':
y2e = YKJ2TM35()