Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Here are the concrete steps for making and adding routes to the repo:
* track `desc`

5. Run `./_bin/gpx-inplace-fixup.sh routes/_gpx/recently-added-route.gpx` to add elevation data to the route. Make sure you have python installed since this script invokes other python scripts.
* Running the script with `--wait SECONDS` may be helpful if the API frequently times out.

6. Follow the instructions for `Building and Developing Locally`. Then run `make serve` to check that it works locally and the site looks right.

Expand Down
33 changes: 24 additions & 9 deletions _bin/gpx-inplace-fixup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,44 @@ while [ -L "$src" ]; do
done
SDIR="$(cd -P "$(dirname "$src")" && pwd)"

# parse optional --wait argument
WAIT=""
while [[ $# -gt 0 ]]; do
case $1 in
--wait)
WAIT="$2"
shift 2
;;
*)
GPX_FILE="$1"
shift
;;
esac
done

# ensure we get a gpx file as input
if [ $# -ne 1 ]; then
echo "Usage: $0 ROUTE.gpx"
if [ -z "${GPX_FILE:-}" ]; then
echo "Usage: $0 [--wait SECONDS] ROUTE.gpx"
exit 1
fi

# ensure input is a readable file
if [ ! -r "$1" ]; then
echo "Error: File '$1' does not exist or is not readable."
if [ ! -r "$GPX_FILE" ]; then
echo "Error: File '$GPX_FILE' does not exist or is not readable."
exit 1
fi

# ensure input is a gpx file
if [[ "$1" != *.gpx ]]; then
echo "Error: File '$1' may not be a GPX file (extension is not \".gpx\")."
if [[ "$GPX_FILE" != *.gpx ]]; then
echo "Error: File '$GPX_FILE' may not be a GPX file (extension is not \".gpx\")."
exit 1
fi

# inject the elevation data into the GPX file
uv run python3 ${SDIR}/replace_route_elevations.py --input "$1" --output "$1"
uv run python3 ${SDIR}/replace_route_elevations.py ${WAIT:+--wait "$WAIT"} --normalize-after --input "$GPX_FILE" --output "$GPX_FILE"

# add surface metadata to the GPX file
uv run python3 ${SDIR}/add_surface_to_gpx.py --input "$1" --output "$1"
uv run python3 ${SDIR}/add_surface_to_gpx.py --input "$GPX_FILE" --output "$GPX_FILE"

# pretty print the GPX file back to itself
uv run python3 ${SDIR}/normalize_gpx.py --input "$1" --output "$1"
uv run python3 ${SDIR}/normalize_gpx.py --input "$GPX_FILE" --output "$GPX_FILE"
10 changes: 9 additions & 1 deletion _bin/replace_route_elevations.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ def main():
parser.add_argument("--output", required=True, nargs="+", help="Output GPX file(s).")
parser.add_argument("--overwrite", action="store_true", help="Replace any existing elevation data")
parser.add_argument("--wait", type=float, default=0.25, help="Wait time between elevation queries (seconds)")
parser.add_argument("--normalize-after", action="store_true", help="Suppress the normalization reminder (caller will run normalize_gpx.py)")
args = parser.parse_args()
print("Resulting GPX files will be denormalized. Use `normalize_gpx.py` to fix them before committing.")

if len(args.input) != len(args.output):
raise ValueError("The number of inputs must match the number of outputs.")

is_gpx_modified = False

for inpath, outpath in zip(args.input, args.output):
# Load GPX
route = gpxpy.parse(open(inpath, 'r'))
Expand All @@ -110,14 +112,20 @@ def main():
for point in tqdm.tqdm(segment.points):
if not point.elevation or args.overwrite:
point.elevation = query_usgs_elevation(point.latitude, point.longitude, wait_time=args.wait)
is_gpx_modified = True

# Iterate over all waypoints (pois)
for waypoint in route.waypoints:
if not waypoint.elevation or args.overwrite:
waypoint.elevation = query_usgs_elevation(waypoint.latitude, waypoint.longitude, wait_time=args.wait)
is_gpx_modified = True

# Save GPX
with open(outpath, 'w') as f:
f.write(route.to_xml())

if is_gpx_modified and not args.normalize_after:
print("Resulting GPX files will be denormalized. Use `normalize_gpx.py` to fix them before committing.")

if __name__ == '__main__':
main()
Loading