diff --git a/README.md b/README.md index 3eb78ff..cfd9530 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/_bin/gpx-inplace-fixup.sh b/_bin/gpx-inplace-fixup.sh index 1434614..5022a9c 100755 --- a/_bin/gpx-inplace-fixup.sh +++ b/_bin/gpx-inplace-fixup.sh @@ -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" diff --git a/_bin/replace_route_elevations.py b/_bin/replace_route_elevations.py index ab6f117..db7a4eb 100644 --- a/_bin/replace_route_elevations.py +++ b/_bin/replace_route_elevations.py @@ -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')) @@ -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()