Last active
August 25, 2020 13:44
-
-
Save quantenschaum/42c78631b5062c18fc6e29846e618811 to your computer and use it in GitHub Desktop.
simple tile proxy for osmand allowing to use quadkey named tiles
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 python3 | |
from flask import Flask, abort, send_file, request | |
from mercantile import quadkey | |
from requests import get | |
from os import makedirs | |
from os.path import isfile, join, dirname | |
cache_dir = 'cache' | |
# https://www.genesismaps.com/SocialMap/Index | |
cmap_base = 'https://s3.amazonaws.com/socialmap.digitalmarine.com/20200807/' # zoom z = 5-18 | |
# zoom z = 6-18 | |
nav_token = '****' | |
navionics_basic = 'https://backend.navionics.com/tile/{z}/{x}/{y}?LAYERS=config_1_5.00_{s}&TRANSPARENT=TRUE&UGC=FALSE&theme=0&navtoken=' + nav_token | |
app = Flask(__name__) | |
@app.route('/<p>/<int:z>/<int:x>/<int:y>.png') | |
def tile(p, x, y, z): | |
cached_file = cache_dir+request.path | |
if isfile(cached_file): | |
return send_file(cached_file) | |
if p.startswith('n'): | |
url = navionics_basic.format(x=x, y=y, z=z, s=1 if 's' in p else 0) | |
tile = get(url, headers={'Referer': 'https://webapp.navionics.com/'}) | |
elif p.startswith('c'): | |
url = f'{cmap_base}{p[1]}_{int(quadkey(x, y, z))}.png' # b=base, t=lines | |
tile = get(url) | |
print(url, tile) | |
if tile.status_code == 200: | |
write(cached_file, tile.content) | |
return tile.content | |
return '' # not 404, confuses osmand | |
def write(filename, content=b''): | |
makedirs(dirname(filename), exist_ok=True) | |
with open(filename, 'wb') as f: | |
f.write(content) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment