sp_replace_typ/sp_replace_typ.py

157 lines
4.3 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import os.path
import platform
import re
import sys
import subprocess
GMT = ''
def get_famid_name_from_imgfile(imgfile_path):
if os.path.isfile(imgfile_path) == False:
sys.exit("Datei nicht gefunden. Bitte geben Sie eine gültige .img-Datei an.")
# This doesn't work on Windows. So we stick to Popen.
# output = subprocess.run(
# [f'{GMT} -i "{imgfile_path}"'], shell=True, capture_output=True, text=True).stdout
session = subprocess.Popen(
[GMT, '-i', imgfile_path], stdout=subprocess.PIPE)
output, _ = session.communicate()
pattern = b".* FID (\d+), (.*)"
match = re.search(pattern, output)
if not match:
sys.exit(
f"Keine gültige gmapsupp-Datei!\nBitte geben Sie eine valide Datei für Garmin/QMapShack an.")
fid = str(match.group(1), 'UTF-8').strip()
name = str(match.group(2), 'UTF-8').strip()
return (fid, name)
def ask_for_visualization():
print("---------------------------------------")
print("TYP Auswahl für Basisdarstellung")
print("(Mehr Infos siehe www.speichenkarte.de)")
print("---------------------------------------")
print("1 TYP schmale Wege")
print("2 TYP Kontrast")
print("3 TYP breite Wege")
print("4 TYP Rennrad")
print("5 TYP blass")
print("6 TYP blass schmal")
print("7 TYP mehr Kontrast")
print("x Beenden\n")
print("Ihre Auswahl: ", end="")
choice = input().strip()
basic = ''
if choice == '1':
basic = '_schmal'
elif choice == '2':
basic = '_Kontrast'
elif choice == '3':
basic = '_breit'
elif choice == '4':
basic = '_RR'
elif choice == '5':
basic = '_blass'
elif choice == '6':
basic = '_blass_schmal'
elif choice == '7':
basic = '_mehr_Kontrast'
elif choice == 'x':
print("Bye!")
sys.exit()
else:
print('Ungültige Auswahl.')
sys.exit()
print("---------------------------------------")
print("TYP Auswahl für Höhenlinien")
print("(Mehr Infos siehe www.speichenkarte.de)")
print("---------------------------------------")
print("1 mit Höhenlinien")
print("2 ohne Höhenlinien")
print("x Beenden\n")
print("Ihre Auswahl: ", end="")
choice = input().strip()
srtm = ''
if choice == '1':
srtm = ''
elif choice == '2':
srtm = '_ohneSRTM'
elif choice == 'x':
print("Bye!")
sys.exit()
else:
print('Ungültige Auswahl.')
sys.exit()
print("---------------------------------------")
print("TYP Auswahl für Linienbeschriftung")
print("(Mehr Infos siehe www.speichenkarte.de)")
print("---------------------------------------")
print("1 mit Linenbeschriftung")
print("2 ohne Linienbeschriftung")
print("x Beenden\n")
print("Ihre Auswahl: ", end="")
choice = input().strip()
text = ''
if choice == '1':
text = ''
elif choice == '2':
text = '_ohneText'
elif choice == 'x':
print("Bye!")
sys.exit()
else:
print('Ungültige Auswahl.')
sys.exit()
return (basic, srtm, text)
def main():
parser = argparse.ArgumentParser(
description='Das Programm passt die Darstellung der "Speichenkarte" '
'für Garmin-Geräte/QMapShack an. ')
parser.add_argument('imgfile', type=str, help='Garmin gmapsupp-Datei')
parser.add_argument('-t', '--typdir', type=str,
help='Verzeichnis mit den TYP-Dateien')
args = parser.parse_args()
imgfile = args.imgfile
typ_dir = os.getcwd()
if args.typdir:
typ_dir = args.typdir
(fid, name) = get_famid_name_from_imgfile(imgfile)
(basic, srtm, text) = ask_for_visualization()
typ_file = f'TYP_{name}_{fid}{basic}{srtm}{text}_gmapsupp.typ'
print(f'\nGewählte TYP Datei: {typ_file}')
print(f'Verzeichnis mit den TYP-Dateien: {typ_dir}\n')
if not os.path.isfile(typ_dir + os.sep + typ_file):
sys.exit('FEHLER: Die Typdatei ist nicht vorhanden.')
print('Wende die Typ-Datei an…')
subprocess.run([GMT, '-wx', imgfile, typ_dir + os.sep + typ_file])
print('\nFertig.')
if __name__ == "__main__":
if platform.system() == 'Linux':
GMT = './gmt'
else: # Windows…
GMT = 'gmt.exe'
main()