#!/usr/bin/env python3> """ دانلود فایل‌های tile برای شهر ساری """ import os import sys import time import math import urllib.request from pathlib import Path # مختصات جغرافیایی شهر ساری SARI_BOUNDS = { 'north': 36.62, # عرض جغرافیایی شمال 'south': 36.52, # عرض جغرافیایی جنوب 'east': 53.15, # طول جغرافیایی شرق 'west': 53.02 # طول جغرافیایی غرب } # سطوح زوم مورد نیاز ZOOM_LEVELS = [12, 13, 14, 15, 16] # آدرس سرور tiles (باید تغییر کنه) TILE_SERVER = "https://app.e-saricity.ir/gnaf/tiles" def lat_lon_to_tile(lat, lon, zoom): """تبدیل مختصات جغرافیایی به شماره tile""" n = 2 ** zoom x = int((lon + 180) / 360 * n) y = int((1 - math.log(math.tan(math.radians(lat)) + 1 / math.cos(math.radians(lat))) / math.pi) / 2 * n) return x, y def get_tile_range(bounds, zoom): """محاسبه محدوده tiles برای یک سطح زوم""" min_x, min_y = lat_lon_to_tile(bounds['north'], bounds['west'], zoom) max_x, max_y = lat_lon_to_tile(bounds['south'], bounds['east'], zoom) # معکوس نکردن y if min_y > max_y: min_y, max_y = max_y, min_y return min_x, max_x, min_y, max_y def download_tile(x, y, z, output_dir, server_url): """دانلود یک tile""" url = f"{server_url}/{z}/{x}/{y}.pbf" output_path = Path(output_dir) / str(z) / str(x) / f"{y}.pbf" # اگر فایل قبلاً دانلود شده، رد شو if output_path.exists(): return True, "exists" try: # ایجاد پوشه output_path.parent.mkdir(parents=True, exist_ok=True) # دانلود req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}) with urllib.request.urlopen(req, timeout=10) as response: data = response.read() # ذخیره with open(output_path, 'wb') as f: f.write(data) return True, "downloaded" except Exception as e: return False, str(e) def main(): print(""" ╔══════════════════════════════════════════════════════════════╗ ║ دانلود Tiles نقشه شهر ساری ║ ╚══════════════════════════════════════════════════════════════╝ """) # مسیر خروجی script_dir = Path(__file__).parent.parent output_dir = script_dir / "public" / "tiles" print(f"输出 مسیر: {output_dir}") print(f"سرور.tiles: {TILE_SERVER}") print(f"محدوده جغرافیایی:") print(f" شمال: {SARI_BOUNDS['north']}") print(f" جنوب: {SARI_BOUNDS['south']}") print(f" شرق: {SARI_BOUNDS['east']}") print(f" غرب: {SARI_BOUNDS['west']}") print(f"سطوح زوم: {ZOOM_LEVELS}") print() # محاسبه تعداد کل tiles total_tiles = 0 tile_counts = {} for z in ZOOM_LEVELS: min_x, max_x, min_y, max_y = get_tile_range(SARI_BOUNDS, z) count = (max_x - min_x + 1) * (max_y - min_y + 1) tile_counts[z] = count total_tiles += count print(f"Zoom {z}: {count} tiles ({max_x - min_x + 1} x {max_y - min_y + 1})") print(f"\n总计: {total_tiles} tiles") print() # تایید response = input("آیا دانلود شروع بشه؟ (y/n): ") if response.lower() != 'y': print("لغو شد.") sys.exit(0) print() # دانلود tiles downloaded = 0 skipped = 0 errors = 0 for z in ZOOM_LEVELS: min_x, max_x, min_y, max_y = get_tile_range(SARI_BOUNDS, z) print(f"\nدانلود zoom level {z}...") for x in range(min_x, max_x + 1): for y in range(min_y, max_y + 1): success, status = download_tile(x, y, z, output_dir, TILE_SERVER) if success: if status == "downloaded": downloaded += 1 # نمایش پیشرفت if downloaded % 10 == 0: print(f" دانلود شده: {downloaded}", end='\r') else: skipped += 1 else: errors += 1 print(f"\n خطا: {z}/{x}/{y}: {status}") # تاخیر کوچک برای جلوگیری از فشار به سرور time.sleep(0.1) print(f""" ╔══════════════════════════════════════════════════════════════╗ ║ گزارش نهایی ║ ╠══════════════════════════════════════════════════════════════╣ ║ دانلود شده: {downloaded:>6} ║ ║ رد شده (موجود): {skipped:>6} ║ ║ خطا: {errors:>6} ║ ╚══════════════════════════════════════════════════════════════╝ """) if errors > 0: print("نکته: بعضی tiles ممکنه در دسترس نباشن. این طبیعیه.") if __name__ == "__main__": main()