Created
September 19, 2016 01:08
-
-
Save kingds/b4bc4ef0a409f4bdb46d410c18637fcb to your computer and use it in GitHub Desktop.
Python script for converting sets of GPS coordinates from protrails.com into a GPX file which can be loaded onto a GPS device.
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# This is a script for converting coordinates from a protrails.com trail description into a | |
# gpx file that can be loaded onto a GPS device. | |
# In order to use it, copy and paste the coordinates from the protrails map page (not the | |
# main page for the hike, but the page with the link "Interactive GPS Topo Map") into an input | |
# file. Run the script with # python protrails_to_gpx.py <input file> <output file> <route name> | |
from lxml import etree as tree | |
import sys | |
# Set up input file, output file, and route name | |
INPUT_FILE = sys.argv[1] | |
OUTPUT_FILE = sys.argv[2] | |
ROUTE_NAME = sys.argv[3] | |
# Start building the xml file | |
gpx = tree.Element("gpx") | |
# Create the route | |
rte = tree.SubElement(gpx, "rte") | |
name = tree.SubElement(rte, "name") | |
name.text = ROUTE_NAME | |
number = tree.SubElement(rte, "number") | |
number.text = "1" | |
# Read the input file | |
with open(INPUT_FILE, "r") as read_file: | |
# Create an entry for each coordinate | |
coords = [] | |
rtept_number = 1 | |
for line in read_file.readlines(): | |
split = line.split(" ") | |
coord = split[1 : split.index("—")] | |
coord = [coord[0], coord[1].split("\t")[0], coord[1].split("\t")[1], coord[2]] | |
coords.append(coord) | |
multiplier = -1 if coord[0][0] == "S" else 1 | |
lat = (multiplier * int(coord[0][1:])) + (multiplier * (0.0166667 * float(coord[1]))) | |
multiplier = -1 if coord[2][0] == "W" else 1 | |
lon = (multiplier * int(coord[2][1:])) + (multiplier * (0.0166667 * float(coord[3]))) | |
rtept_name_string = " ".join(split[split.index("—")+1 :]).replace("\n", "") | |
rtept = tree.SubElement(rte, "rtept", lat=str(lat), lon=str(lon)) | |
rtept_name = tree.SubElement(rtept, "name") | |
rtept_name.text = rtept_name_string | |
# Write the xml to the output file | |
tree.ElementTree(gpx).write(OUTPUT_FILE, pretty_print=True, xml_declaration=True, encoding="UTF-8") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please provide link or sample data to run protrails_to_gpx.py?