aboutsummaryrefslogblamecommitdiff
path: root/gk2kml.py
blob: 24d842c8380978a585d38c6541dd694401efcf1e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                 

                             





                                                     
 
                    
                                        



































                                                                          
 















                                                                            


                                                   





                                                            

                                 



                                                                
                                                                           

                                                                   





                                                           
                                     







                                                            
 

                          
#!/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

# Coordinate Reference System
# from pyproj import CRS
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 int(args.zone) >= 2 and int(args.zone) <= 5:
        # EPS:5682 .. EPS:5685
        src_crs = 'EPSG:568' + args.zone
    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/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()
in each repos: see "about"-tab (if existing) for more details / README.
mailto contact at omeckman dot net
all timestamps in UTC (German winter time: UTC+01:00, summer time: UTC+02:00)
dark theme is a modded version of: https://gist.github.com/Yoplitein/f4b671a2ec70c9e743fa