3

I would like to download tile layers in this form:

{atlas_name}/{z}/{x}/{y}.png

Because I want to use them offline with my leaflet map.

However, I can't find what are the approaches to download such tiles for offline use? Are there some apps for this?

11
  • 1
    How many tiles are we talking about? What's the geographic extent, the zoom levels? Techniques differ depending on that. Commented Apr 23, 2019 at 7:32
  • forgot to sign up, so posting from this account. geographic area is country: georgia. zoom levels, let's say: 16,17,18
    – user140887
    Commented Apr 23, 2019 at 7:39
  • Does the tile provider allow this kind of usage? Commented Apr 23, 2019 at 8:36
  • 1
    Just for your info: zoom level 18 means 68.719.476.736 tiles (if map is square), and each tile should be about 5KB to 25KB.
    – TomazicM
    Commented Apr 23, 2019 at 16:21
  • 1
    You can try TileLayer.PouchDBCached plugin: github.com/MazeMap/Leaflet.TileLayer.PouchDBCached
    – TomazicM
    Commented Apr 24, 2019 at 15:00

3 Answers 3

2

This golang script works like a charm for downloading tiles from WMS servers: https://github.com/Luqqk/wms-tiles-downloader.

To do it in the browser: https://github.com/MazeMap/Leaflet.TileLayer.PouchDBCached as mentioned by https://gis.stackexchange.com/users/104146/tomazicm (@TomazicM)

1

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.

0

sas.planet is my favorite tool. You will use it forever.

for more info : SAS.Team SAS SAS.Planet

https://php-full-stack.blogspot.com/2019/06/sasteam-sas-sasplanetbin.html

3
  • 1
    can you add some more information to your answer as links may get offline.
    – MrXsquared
    Commented Jun 5, 2019 at 22:31
  • You need to install it on an online computer to get the tiles downloaded in the installation sub directory.Then copy that directory in an offline computer to use them offline with leaflet map. Commented Jun 6, 2019 at 11:28
  • Please let me know if you succeded to do it :) Commented Jun 18, 2019 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.