feat: mvt decode + download tiles

This commit is contained in:
Matthieu Bessat 2024-06-15 08:51:00 +02:00
parent 94898aca13
commit 4ae433d404
7 changed files with 79300 additions and 2 deletions

View file

@ -1 +1,2 @@
tiles
results/

View file

@ -0,0 +1,58 @@
import requests
import os
import math
def deg2num(lat_deg, lon_deg, zoom):
lat_rad = math.radians(lat_deg)
n = 1 << zoom
xtile = int((lon_deg + 180.0) / 360.0 * n)
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return xtile, ytile
# around chatillon
BOUNDS = ((48.8, 2.26), (48.82, 2.32))
BOUNDS = ((48.5, 2.1), (49.9, 2.6))
BASE_URL = os.getenv("SOURCE_BASE_URL")
BASE_DIR = os.getenv("OUTPUT_BASE_DIR")
LAYERS = os.getenv("LAYERS_NAMES")
iz = 0
for z in range(10, 15):
TILES_BOUNDS = (
deg2num(BOUNDS[0][0], BOUNDS[0][1], z),
deg2num(BOUNDS[1][0], BOUNDS[1][1], z)
)
maxx = 0
maxy = 0
xrange = range(TILES_BOUNDS[0][0], TILES_BOUNDS[1][0])
yrange = range(TILES_BOUNDS[1][1], TILES_BOUNDS[0][1])
print(TILES_BOUNDS)
print(xrange, yrange)
iy = 0
for y in reversed(yrange):
ix = 0
for x in xrange:
url = f"{BASE_URL}/{LAYERS}/{z}/{x}/{y}"
dir_path = f'{BASE_DIR}/tiles/{LAYERS}/{z}/{x}'
tile_path = dir_path + f'/{y}'
img_data = 0
print(tile_path)
if not os.path.exists(tile_path):
print(url)
res = requests.get(url)
if res.status_code >= 300:
print(res.content)
exit()
img_data = res.content
os.makedirs(dir_path, exist_ok=True)
with open(tile_path, 'wb') as handler:
handler.write(img_data)
ix += 1
iy += 1
iz += 1
print("DONE")

View file

@ -12,12 +12,15 @@ def deg2num(lat_deg, lon_deg, zoom):
ytile = int((1.0 - math.asinh(math.tan(lat_rad)) / math.pi) / 2.0 * n)
return xtile, ytile
BOUNDS = ((49.17, 1.31), (49.18, 1.35))
#BOUNDS = ((49.17, 1.31), (49.18, 1.35))
BOUNDS = ((48.7, 2.2), (48.8, 2.3))
BOUNDS = ((48.8, 2.26), (48.82, 2.32))
DEFAULT_HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:127.0) Gecko/20100101 Firefox/127.0"}
iz = 0
for z in range(16, 17):
for z in range(14, 15):
# https://tile.openstreetmap.org/11/1029/700.png
# 1029, 1030 going to the est, increasing the longitude angle (x)
# 700, 701, going to the south, deacreasing the flatitude latitude angle (y)

1
mvt_decode/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
venv/

10
mvt_decode/decode.py Normal file
View file

@ -0,0 +1,10 @@
import mapbox_vector_tile
import json
import sys
with open(sys.argv[1], 'rb') as f:
data = f.read()
decoded_data = mapbox_vector_tile.decode(data)
print(json.dumps(decoded_data))
with open('out.txt', 'w') as f:
f.write(repr(decoded_data))

79224
mvt_decode/out.json Normal file

File diff suppressed because it is too large Load diff

1
mvt_decode/out.txt Normal file

File diff suppressed because one or more lines are too long