This is a Python script generated by the Gemini large language model, designed to download map tiles from CartoDB services for specified geographic regions and zoom levels. It supports downloading by either geographic latitude/longitude bounds or direct tile XY coordinates, and offers advanced features like multi-threading, resume functionality, retries, custom User-Agents, and proxy support.
- Flexible Tile Sources: Supports various preset CartoDB map styles (e.g.,
dark_all,light_all,voyager, etc.) and allows for custom tile URL templates. - Two Download Modes:
- Geographic Bounds Download: Download tiles for a specific area by specifying minimum/maximum latitude and longitude. The program automatically calculates and downloads all tiles covering that region.
- Tile Coordinate Download: Directly specify the X and Y tile coordinate ranges for download, useful when you already know the tile indices.
- Multi-Zoom Level Support: Capable of downloading all tiles within a specified
min_zoomtomax_zoomrange in one go. - Resume Capability: Skips downloading tile files if they already exist, allowing you to resume interrupted downloads easily.
- Efficient Concurrency: Utilizes multi-threading (
ThreadPoolExecutor) to speed up tile downloads. - Error Handling & Retries: Supports a retry mechanism for failed downloads and specifically handles common HTTP errors like
400 Bad Request,404 Not Found, and403 Forbiddento prevent futile retries. - Custom Request Headers: Allows setting a custom
User-Agentto mimic browser access. - Proxy Support: Configurable HTTP/HTTPS proxy settings for downloads.
- Progress Display: Uses the
tqdmlibrary to show download progress for each zoom level and overall. - Global Map Download Optimization: Intelligently handles latitude inputs like
-90.0to90.0to correctly identify and download all tiles for a full global map. - Logging: Detailed log output for easy tracking of the download process and troubleshooting.
Ensure you have Python 3 installed on your system. Then, install the required dependencies:
pip install requests tqdmIt's recommended to install these dependencies within a Python virtual environment to avoid cluttering your global environment.
The script is configured via command-line arguments. Here are some common usage examples:
python enhanced_tile_downloader.py ^
--url dark_all ^
--min_zoom 0 ^
--max_zoom 5 ^
--min_lat -90.0 ^
--min_lon -180.0 ^
--max_lat 90.0 ^
--max_lon 180.0 ^
--output "Global_Tiles_Dark_Z0-5" ^
--workers 10 ^
--retries 3Parameter Explanation:
--url: Specifies the tile type (e.g.,dark_all) or a complete custom tile URL template.--min_zoom,--max_zoom: Sets the starting and ending zoom levels for download.--min_lat,--min_lon,--max_lat,--max_lon: Defines the geographic bounds of the download area. Note that when downloading global maps, inputs like-90.0and90.0for latitude are correctly handled by the program.--output: The root directory where tiles will be saved. The program will create it if it doesn't exist.--workers: The number of concurrent download threads (default is10).--retries: The number of times to retry a failed tile download (default is3).
python enhanced_tile_downloader.py ^
--url voyager ^
--min_zoom 10 ^
--max_zoom 14 ^
--min_lat 31.20 ^
--min_lon 121.45 ^
--max_lat 31.25 ^
--max_lon 121.50 ^
--output "Shanghai_Voyager_Z10-14" ^
--workers 15python enhanced_tile_downloader.py ^
--url light_all ^
--min_zoom 12 ^
--max_zoom 12 ^
--min_x 3500 ^
--max_x 3510 ^
--min_y 1600 ^
--max_y 1610 ^
--output "Custom_Tiles_Z12"python enhanced_tile_downloader.py ^
--url dark_all ^
--min_zoom 0 ^
--max_zoom 1 ^
--min_lat 0 ^
--min_lon 0 ^
--max_lat 10 ^
--max_lon 10 ^
--output "Proxy_Test" ^
--http_proxy "http://user:pass@your_proxy_host:port" ^
--https_proxy "https://user:pass@your_proxy_host:port" ^
--user_agent "MyCustomAgent/1.0"After the download is complete, all tiles will be organized in the structure: {output_directory}/{z}/{x}/{y}.png.
For example, for the Global_Tiles_Dark_Z0-5 directory:
Global_Tiles_Dark_Z0-5/
├── 0/
│ └── 0/
│ └── 0.png
├── 1/
│ ├── 0/
│ │ ├── 0.png
│ │ └── 1.png
│ └── 1/
│ ├── 0.png
│ └── 1.png
└── ...
- Legal Use: Please ensure your download activities comply with the terms of service of the tile provider (e.g., CartoDB). Excessive or unauthorized downloading may lead to IP blocking.
- Network Connection: Tile downloading requires a stable internet connection and sufficient bandwidth.
- Storage Space: High-zoom level tiles can be very numerous. Ensure you have ample disk space. For instance, global tiles from Z=0 to Z=18 can consume terabytes of storage.
- Floating-Point Precision: Latitude and longitude parameters are floating-point numbers. While the program has optimizations, very minor precision issues might still occur in extreme edge cases.
-
Create
requirements.txt: First, create a file namedrequirements.txtin the same directory as yourenhanced_tile_downloader.pyandDockerfile. This file should list the Python dependencies your script needs:requests tqdm -
Build the Docker Image: Open your terminal or command prompt, navigate to the directory where you saved the
Dockerfile,enhanced_tile_downloader.py, andrequirements.txt, and run the following command:docker build -t tile-downloader .This command builds a Docker image named
tile-downloaderbased on yourDockerfile. The.at the end indicates that the build context is the current directory. -
Run the Docker Container: Once the image is built, you can run your tile downloader inside a Docker container. You'll need to specify the download arguments and also mount a volume so that the downloaded tiles are saved to your host machine (otherwise, they'd just be inside the container and disappear when the container stops).
Here's an example command to download global tiles from Z0-Z5, saving them to a local folder named
my_downloaded_tiles:docker run --rm -v "$(pwd)/my_downloaded_tiles:/app/Global_Tiles_Dark_Z0-5" tile-downloader ^ --url dark_all ^ --min_zoom 0 ^ --max_zoom 5 ^ --min_lat -90.0 ^ --min_lon -180.0 ^ --max_lat 90.0 ^ --max_lon 180.0 ^ --output "Global_Tiles_Dark_Z0-5" ^ --workers 10 ^ --retries 3
--rm: This option automatically removes the container once it exits.-v "$(pwd)/my_downloaded_tiles:/app/Global_Tiles_Dark_Z0-5": This is the volume mount."$(pwd)/my_downloaded_tiles": This specifies a directory on your host machine (the current directorypwdfollowed bymy_downloaded_tiles).:/app/Global_Tiles_Dark_Z0-5: This specifies the corresponding directory inside the container where the script will try to save the tiles (based on your--outputargument). Make sure the path after/app/matches your--outputargument's value. If you change your--outputfolder, you'll need to change this part of the volume mount too.
tile-downloader: This is the name of the Docker image you built.- The remaining arguments (
--url,--min_zoom, etc.) are passed directly to your Python script running inside the container.
Note for Windows users: If you're using Command Prompt, you might need to change
$(pwd)to%cd%and adjust path separators if necessary, or use Git Bash/WSL for a more Unix-like experience. For PowerShell,$(pwd)should work.
This Dockerfile provides a solid foundation for packaging and running your tile downloader.
Thank you for using this tool! Feel free to provide feedback or suggestions.