snippets/geo_tiles_assembler/plot_osm_map.py

54 lines
1.7 KiB
Python

import matplotlib.pyplot as plt
import matplotlib.image as img
import requests
import numpy as np
import os
DEFAULT_HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:127.0) Gecko/20100101 Firefox/127.0"}
fig, ax = plt.subplots()
maxx = 0
maxy = 0
iz = 0
yrange = range(700, 704)
xrange = range(1029, 1034)
for z in range(11, 12):
# 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)
iy = 0
for y in reversed(yrange):
ix = 0
for x in xrange:
# https://tile.openstreetmap.org/17/65956/44733.png
url = f"https://tile.openstreetmap.org/{z}/{x}/{y}.png"
dir_path = f'tiles/{z}/{x}'
tile_path = dir_path + f'/{y}.png'
img_data = 0
print(tile_path)
if not os.path.exists(tile_path):
res = requests.get(url, headers=DEFAULT_HEADERS)
print(res)
if res.status_code != 200:
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)
im = img.imread(tile_path)
print("read")
print(ix, iy)
ax.imshow(im, aspect='auto', extent=(ix, ix+1, iy, iy+1), zorder=-1)
print("imshow")
ix += 1
iy += 1
iz += 1
ax.set_xlim(0, len(xrange))
ax.set_ylim(0, len(yrange))
ax.set_aspect('equal', adjustable='box')
plt.show()