sp_replace_typ/sp_replace_typ.py

146 lines
4.1 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.")
# fid = subprocess.run(f'{GMT} -i {imgfile_path} | grep ", FID " | cut -d\',\' -f 2 | sed -e \'s/ FID //\'',
# shell=True, capture_output=True, text=True).stdout.strip()
# name = subprocess.run(f'{GMT} -i {imgfile_path} | grep ", FID " | cut -d\',\' -f 3 | sed -e \'s/ //\'',
# shell=True, capture_output=True, text=True).stdout.strip()
output = subprocess.run(
[f'{GMT} -i {imgfile_path}'], shell=True, capture_output=True, text=True).stdout
pattern = ".* FID (\d+), (.*)"
match = re.search(pattern, output)
if not match:
sys.exit(
f"Keine gültige gmapsupp-Datei: {imgfile_path}\nBitte geben Sie eine gültige Datei an.")
return (match.group(1), match.group(2))
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':
srtm = '_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 Garmin-"Speichenkarte" an. ')
parser.add_argument('imgfile', type=str, help='Garmin gmapsupp-Datei')
parser.add_argument()
args = parser.parse_args()
imgfile = args.imgfile
(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}\n')
if not os.path.isfile(typ_file):
sys.exit('FEHLER: Die Typdatei ist nicht vorhanden.')
print('Wende die Typ-Datei an…')
subprocess.run([GMT, '-wx', imgfile, typ_file])
print('\nFertig.')
if __name__ == "__main__":
if platform.system() == 'Linux':
GMT = './gmt'
else: # Windows…
GMT = './gmt.exe'
main()