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
|
#!/usr/bin/env python
#
# gk2kml
#
# converts Gauss-Krueger coordinates to WGS84
# and also output a .kml so we can easily
# load it up in Google Earth without copy-pasting
#
# 2022-04-27 Oliver Meckmann
import argparse
from textwrap import dedent
from sys import exit
from pyproj import CRS # Coordinate Reference System
from pyproj import Transformer
# https://spatialreference.org/ref/epsg/wgs-84/
# https://en.wikipedia.org/wiki/World_Geodetic_System
# https://epsg.io/?q=Gauss-Kruger%20kind%3APROJCRS
def run_argparser():
argparser = argparse.ArgumentParser(\
formatter_class=argparse.RawTextHelpFormatter,
description=dedent('''
Convert Gauss-Kru(e)ger Coordinates to WGS84
'''))
argparser.add_argument(
'-z',
'--zone',
default='3',
help=dedent('''
Input Gauss-Kruger Zone (Default: 3)
2 = 3deg Zone 2: Germany
3 = 3deg Zone 3: Germany onshore between 7°30'E and 10°30'E
4 = 3deg Zone 4: Germany onshore between 10°30'E and 13°30'E
5 = 3deg Zone 5: Germany onshore east of 13°30'E
'''),
nargs='?',
)
argparser.add_argument(
'-w',
'--write',
help='Write to file [filename] (.kml gets attached)',
type=str,
nargs='?'
)
argparser.add_argument(
'coord',
metavar='coord',
type=str,
help=dedent('''
[easting northing] ([Rechtswert Hochwert])
'''),
nargs=2)
args = argparser.parse_args()
return args
def main():
args = run_argparser()
if len(args.coord[0]) < 6 or len(args.coord[1]) < 6:
print('Error: coordinates too short/inaccurate!')
exit()
easting = args.coord[0]
northing = args.coord[1]
# sometimes we don't have THAT accurate coords, we then fill up with a 0
if len(easting) == 6:
easting += '0'
if len(northing) == 6:
northing += '0'
# set zone
if args.zone == '2':
src_crs = 'EPSG:5682'
elif args.zone == '3':
src_crs = 'EPSG:5683'
elif args.zone == '4':
src_crs = 'EPSG:5684'
elif args.zone == '5':
src_crs = 'EPSG:5685'
else:
print('Error: Wrong zone!')
exit()
transformer = Transformer.from_crs(src_crs, 'EPSG:4326')
result = transformer.transform(easting, northing)
if(str(result[0]) != 'inf'):
print(str(result[0]) + ', ' + str(result[1]))
if args.write:
print('Writing to kml file: ' + args.write + '.kml')
template = open('template.kml', 'r', encoding='utf-8')
outf = open(args.write + '.kml', 'w', encoding='utf-8',\
errors='ignore')
line = template.readline()
while line:
line = line.replace('$NAME', args.write)
line = line.replace('$LON', str(result[1]))
line = line.replace('$LAT', str(result[0]))
#print(line, end='')
outf.write(line)
line = template.readline()
outf.close()
else:
print('Error: no valid result! Check input coords!')
if __name__ == '__main__':
main()
|