11import asyncio
2+ import gzip
23import os
34import random
45import signal
@@ -45,6 +46,20 @@ async def acquire(self) -> bool:
4546 return True
4647
4748
49+ def normalize_format (ext : str , default : str | None = None ) -> str :
50+ """拡張子をMBTiles仕様のformat値に正規化"""
51+ ext = ext .lower ().lstrip ("." )
52+ if not ext :
53+ if default is None :
54+ raise ValueError ("format must be specified when url has no extension" )
55+ return normalize_format (default )
56+ if ext in ("jpeg" , "jpg" ):
57+ return "jpg"
58+ if ext in ("mvt" , "pbf" ):
59+ return "pbf"
60+ return ext
61+
62+
4863def is_retryable_error (e : Exception ) -> bool :
4964 if isinstance (e , httpx .TimeoutException ):
5065 return True
@@ -165,6 +180,11 @@ async def download_mbtiles(
165180 if data is None :
166181 return
167182
183+ # MVT(pbf)はgzip圧縮して保存する必要がある
184+ ext = os .path .splitext (tileurl .split ("?" )[0 ])[- 1 ].lower ().lstrip ("." )
185+ if ext in ("mvt" , "pbf" ) and data [:2 ] != b"\x1f \x8b " :
186+ data = gzip .compress (data )
187+
168188 if overwrite :
169189 c .execute (
170190 "DELETE FROM tiles WHERE zoom_level = ? AND tile_column = ? AND tile_row = ?" ,
@@ -184,7 +204,7 @@ def create_mbtiles(output_file: str):
184204 c .execute (
185205 """
186206 CREATE TABLE metadata (
187- name TEXT,
207+ name TEXT PRIMARY KEY ,
188208 value TEXT
189209 )
190210 """
@@ -232,32 +252,32 @@ def handle_sigint():
232252
233253 conn = None
234254 if params .mode == "mbtiles" :
235- if not os .path .exists (params .output_path ):
255+ is_new = not os .path .exists (params .output_path )
256+ if is_new :
236257 create_mbtiles (params .output_path )
237258
238259 conn = sqlite3 .connect (params .output_path , check_same_thread = False )
239260
240- c = conn .cursor ()
241- c .execute (
242- "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
243- ("name" , os .path .basename (params .output_path )),
244- )
245- c .execute (
246- "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
247- (
248- "format" ,
249- os .path .splitext (params .tileurl .split ("?" )[0 ])[- 1 ].replace ("." , "" ),
250- ),
251- )
252- c .execute (
253- "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
254- ("minzoom" , params .minzoom ),
255- )
256- c .execute (
257- "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
258- ("maxzoom" , params .maxzoom ),
259- )
260- conn .commit ()
261+ if is_new :
262+ ext = os .path .splitext (params .tileurl .split ("?" )[0 ])[- 1 ]
263+ c = conn .cursor ()
264+ c .execute (
265+ "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
266+ ("name" , os .path .basename (params .output_path )),
267+ )
268+ c .execute (
269+ "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
270+ ("format" , normalize_format (ext , params .format )),
271+ )
272+ c .execute (
273+ "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
274+ ("minzoom" , params .minzoom ),
275+ )
276+ c .execute (
277+ "INSERT INTO metadata (name, value) VALUES (?, ?)" ,
278+ ("maxzoom" , params .maxzoom ),
279+ )
280+ conn .commit ()
261281
262282 tilescheme = (
263283 tiletanic .tileschemes .WebMercatorBL ()
0 commit comments