Created
August 1, 2023 00:25
-
-
Save quietvoid/5713950bf978c6bc955097c7aa6d6724 to your computer and use it in GitHub Desktop.
Download Wahoo routing tiles from bounding box
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/python | |
from collections import defaultdict | |
from pathlib import Path | |
import requests | |
valhalla_tiles = [{'level': 2, 'size': 0.25}] | |
def tiles_for_bounding_box(left, bottom, right, top): | |
# if this is crossing the anti meridian split it up and combine | |
if left > right: | |
east = tiles_for_bounding_box(left, bottom, 180.0, top) | |
west = tiles_for_bounding_box(-180.0, bottom, right, top) | |
return east + west | |
# move these so we can compute percentages | |
left += 180 | |
right += 180 | |
bottom += 90 | |
top += 90 | |
tiles = [] | |
# for each size of tile | |
for tile_set in valhalla_tiles: | |
# for each column | |
for x in range(int(left / tile_set['size']), int(right / tile_set['size']) + 1): | |
# for each row | |
for y in range(int(bottom / tile_set['size']), int(top / tile_set['size']) + 1): | |
# give back the level and the tile index | |
tiles.append((tile_set['level'], int(y * (360.0 / tile_set['size']) + x))) | |
return tiles | |
# Fill this with lat/lng of the bounding box | |
left = 0 | |
bottom = 0 | |
right = 0 | |
top = 0 | |
l2_ids = [str(t[1]) for t in tiles_for_bounding_box(left, bottom, right, top)] | |
grouped_y = defaultdict(list) | |
for e in l2_ids: | |
grouped_y[int(e[:3])].append(int(e[3:])) | |
if l2_ids is not None: | |
root_path = Path('routing/2/000') | |
root_path.mkdir(parents=True, exist_ok=True) | |
# replace tiles with tiles_elevation for routing files with elevation | |
url_format = 'https://cdn.wahooligan.com/wahoo-maps/12/routing/tiles_elevation/2/000/{x:03d}/{y:03d}.gph.lzma' | |
for x, y_list in grouped_y.items(): | |
x_path = root_path.joinpath(str(x)) | |
x_path.mkdir() | |
for y in y_list: | |
file_url = url_format.format(x=x, y=y) | |
y_path = x_path.joinpath(f'{y:03d}.gph.lzma') | |
print(f'Downloading {file_url} to {y_path}') | |
res = requests.get(file_url, timeout=30) | |
if res.ok: | |
with open(y_path, 'wb') as f: | |
f.write(res.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment