I created this script in Python, which helped me a lot. Fill in the parameters according to your constraints. I used Thunderforest as a provider. You have to register to get the api_key and then choose the style you want. There is a free version for several requests per month. Feel free to change the URL and use other providers.
import requests
import os
import time
# Define the URL pattern for the Thunderforest Transport Map tile layer
# FILL map_style and api_key
url_pattern = "https://tile.thunderforest.com/<map_style>/{z}/{x}/{y}.png?apikey=<your_api_key>"
# Define the maximum zoom level to retrieve tiles for
max_zoom = 7
# Define the output directory for the downloaded tiles
output_dir = "tiles"
# time in seconds between requests to don't overload the provider
sleep_time = 0.2
# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Loop over each zoom level up to the maximum zoom level
for zoom in range(max_zoom + 1):
# Calculate the number of tiles in the x and y directions at the current zoom level
num_tiles = 2**zoom
if not os.path.exists(os.path.join(output_dir, str(zoom))):
os.makedirs(os.path.join(output_dir, str(zoom)))
# Loop over each tile in the x and y directions
for x in range(num_tiles):
if not os.path.exists(os.path.join(output_dir, str(zoom), str(x))):
os.makedirs(os.path.join(output_dir, str(zoom), str(x)))
for y in range( num_tiles):
# Construct the URL for the current tile
url = url_pattern.format(z=zoom, x=x, y=y)
# Send a request to download the tile
response = requests.get(url)
# Save the tile to the output directory
filename = os.path.join(
os.path.join(output_dir, str(zoom), str(x)), f"{y}.png"
)
with open(filename, "wb") as f:
f.write(response.content)
# Print a message to indicate progress
print(f"Downloaded tile {zoom}/{x}/{y}")
time.sleep(sleep_time)
This is useful for downloading the entire map at a specific zoom level. To give you an estimate, you will download 100Mb of data till Zoom 7, and you will get double the size for the next Zoom level. I hope it helps.
TileLayer.PouchDBCached
plugin: github.com/MazeMap/Leaflet.TileLayer.PouchDBCached