Created
March 16, 2023 10:30
-
-
Save clemensv/24b11c4bcf24e698bd44866f226d39f2 to your computer and use it in GitHub Desktop.
Routenplaner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import geopy | |
from geopy.geocoders import Nominatim | |
import xml.etree.ElementTree as ET | |
def main(input_file, output_file): | |
geolocator = Nominatim(user_agent="myGeocoder") | |
ET.register_namespace('', "http://www.topografix.com/GPX/1/1") | |
tree = ET.parse(input_file) | |
root = tree.getroot() | |
for rtept in root.findall(".//{http://www.topografix.com/GPX/1/1}rtept"): | |
name = rtept.find("{http://www.topografix.com/GPX/1/1}name").text | |
desc = rtept.find("{http://www.topografix.com/GPX/1/1}desc").text | |
name = " ".join(name.split()) | |
desc = " ".join(desc.split()) | |
address = name.split(";") | |
if len(address) > 2: | |
location = geolocator.geocode(f"{address[1]},{address[2]}") | |
elif len(address) > 1: | |
location = geolocator.geocode(f"{address[1]}") | |
else: | |
location = geolocator.geocode(f"{address[0]}") | |
if location: | |
rtept.set("lat", str(location.latitude)) | |
rtept.set("lon", str(location.longitude)) | |
print(f"Updated coordinates for {name}: {location.latitude}, {location.longitude}") | |
else: | |
print(f"Could not find coordinates for {name}") | |
tree.write(output_file, xml_declaration=True) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Update GPX file coordinates") | |
parser.add_argument("-i", "--input", dest="input_file", required=True, help="Path to the input GPX file") | |
parser.add_argument("-o", "--output", dest="output_file", help="Path to the output GPX file") | |
args = parser.parse_args() | |
input_file = args.input_file | |
output_file = args.output_file if args.output_file else input_file | |
main(input_file, output_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Erstelle eine neue Auto-Rundtour von 200km mit Startpunkt in Viersen mit dem Thema Wasserschlösser. | |
Ermittle die Strassenadressen für jeden Wegpunkt. Gebe das Ergebnis als GPX XML Text aus, eingeschlossen von ```. Benutze Routenpunkte. | |
Alle Erklärungen sollten in den Beschreibungselementen der Datei enthalten sein: | |
In <name> benutze das Format {zielname}; {strasse} {hausnummer}; {postleitzahl} {ort}. | |
Benutze eine aussagekräftige Beschreibung in <desc>. | |
Gib die Zusammenfassung der Route in <metadata> aus. | |
Antworte NUR mit dem XML Text. Achte auf die korrekten XML Namespaces. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment