commit 89ade8168f594d40b62e8e3ef76073febe20c7bd Author: Matthieu Bessat Date: Wed May 29 12:48:36 2024 +0200 feat: geo tiles assembler with python and matplotlib diff --git a/geo_tiles_assembler/.gitignore b/geo_tiles_assembler/.gitignore new file mode 100644 index 0000000..70b8406 --- /dev/null +++ b/geo_tiles_assembler/.gitignore @@ -0,0 +1 @@ +tiles diff --git a/geo_tiles_assembler/plot_osm_map.py b/geo_tiles_assembler/plot_osm_map.py new file mode 100644 index 0000000..f46b95f --- /dev/null +++ b/geo_tiles_assembler/plot_osm_map.py @@ -0,0 +1,54 @@ +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()