33# dependencies = [
44# "mercantile",
55# "pmtiles",
6+ # "shapely",
67# ]
78# ///
89
1819
1920
2021import mercantile
21- from shapely .geometry import mapping
22+ from shapely .geometry import shape
2223
2324from osgeo_utils .gdal2tiles import main as gdal2tiles_main
2425from osgeo_utils .gdal2tiles import create_overview_tile , TileJobInfo , GDAL2Tiles
2526
26-
2727from tile_sources import PartitionedPMTilesSource , MissingTileError
2828
2929WEBP_QUALITY = 75
3030
31-
32-
3331class AttrDict (dict ):
3432 def __init__ (self , * args , ** kwargs ):
3533 super (AttrDict , self ).__init__ (* args , ** kwargs )
@@ -110,7 +108,7 @@ def get_tile_file(tile, tiles_dir):
110108
111109def check_sheets (sheets_to_pull , tiffs_dir ):
112110 for sheet_no in sheets_to_pull :
113- to = tiffs_dir .joinpath (f'{ sheet_no } .tif ' )
111+ to = tiffs_dir .joinpath (f'{ sheet_no } ' )
114112 if not to .exists ():
115113 raise Exception (f'missing file { to } ' )
116114
@@ -122,7 +120,7 @@ def copy_tiles_over(tiles_to_pull, tiles_dir, from_pmtiles_prefix):
122120 pull_from_pmtiles (to , from_pmtiles_prefix )
123121
124122
125- def create_upper_tiles (tiles_to_create , tiles_dir ):
123+ def create_upper_tiles (z , tiles_to_create , tiles_dir ):
126124 options = AttrDict ({
127125 'resume' : True ,
128126 'verbose' : False ,
@@ -162,7 +160,7 @@ def get_sheet_data(bounds_fname):
162160 sheets_to_box = {}
163161 for f in index_data ['features' ]:
164162 sheet_no = f ['properties' ]['id' ]
165- geom = mapping (f ['geometry' ])
163+ geom = shape (f ['geometry' ])
166164 xmin , ymin , xmax , ymax = geom .bounds
167165 box = mercantile .LngLatBbox (xmin , ymin , xmax , ymax )
168166 sheets_to_box [sheet_no ] = box
@@ -203,7 +201,7 @@ def create_vrt_file(sheets, tiffs_dir):
203201 vrt_file = Path (f'{ tiffs_dir } /combined.vrt' )
204202 if vrt_file .exists ():
205203 return vrt_file
206- tiff_list = [ tiffs_dir .joinpath (f'{ p_sheet } .tif ' ).resolve () for p_sheet in sheets ]
204+ tiff_list = [ tiffs_dir .joinpath (f'{ p_sheet } ' ).resolve () for p_sheet in sheets ]
207205 tiff_list = [ str (f ) for f in tiff_list if f .exists () ]
208206
209207 tiff_list_str = ' ' .join (tiff_list )
@@ -239,7 +237,7 @@ def cli():
239237 parser .error (f"The following arguments are required when --sheets-to-pull-list-outfile is not provided: { ', ' .join (missing )} " )
240238
241239 retile_sheets = Path (args .retile_list_file ).read_text ().split ('\n ' )
242- retile_sheets = set ([ r .strip () for r in retile_sheets if r .strip () != '' ])
240+ retile_sheets = set ([ r .strip (). replace ( '.tif' , '' ) for r in retile_sheets if r .strip () != '' ])
243241
244242 if args .from_pmtiles_prefix is None :
245243 if not args .max_zoom :
@@ -271,7 +269,7 @@ def cli():
271269 for tile in affected_base_tiles :
272270 to_add = base_tiles_to_sheets [tile ]
273271 for sheet in to_add :
274- sheets_to_pull .add (sheet )
272+ sheets_to_pull .add (sheet + '.tif' )
275273
276274 print (f'{ sheets_to_pull = } ' )
277275
@@ -282,20 +280,22 @@ def cli():
282280 exit (0 )
283281
284282
283+ tiles_dir = Path (args .tiles_dir )
284+ tiffs_dir = Path (args .tiffs_dir )
285285
286286 print ('check the sheets availability' )
287- check_sheets (sheets_to_pull , args . tiffs_dir )
287+ check_sheets (sheets_to_pull , tiffs_dir )
288288
289289 print ('creating vrt file from sheets involved' )
290- vrt_file = create_vrt_file (sheets_to_pull , args . tiffs_dir )
290+ vrt_file = create_vrt_file (sheets_to_pull , tiffs_dir )
291291
292292 Path (args .tiles_dir ).mkdir (exist_ok = True , parents = True )
293293
294294 print ('creating tiles for base zoom with a vrt' )
295- create_base_tiles (f' { vrt_file } ' , str ( args . tiles_dir ) , f'{ max_zoom } ' )
295+ create_base_tiles (str ( vrt_file ), tiles_dir , f'{ max_zoom } ' )
296296
297297 print ('deleting unwanted base tiles' )
298- delete_unwanted_tiles (affected_base_tiles , max_zoom , args . tiles_dir )
298+ delete_unwanted_tiles (affected_base_tiles , max_zoom , tiles_dir )
299299
300300 prev_affected_tiles = affected_base_tiles
301301 for z in range (max_zoom - 1 , min_zoom - 1 , - 1 ):
@@ -313,13 +313,13 @@ def cli():
313313 child_tiles_to_pull .add (ctile )
314314
315315 print ('copying additional child tiles required for curr level' )
316- copy_tiles_over (child_tiles_to_pull , args . tiles_dir , args .from_pmtiles_prefix )
316+ copy_tiles_over (child_tiles_to_pull , tiles_dir , args .from_pmtiles_prefix )
317317
318318 print ('creating tiles for current level' )
319- create_upper_tiles (curr_affected_tiles , args . tiles_dir )
319+ create_upper_tiles (z , curr_affected_tiles , tiles_dir )
320320
321321 print ('removing unwanted child tiles' )
322- delete_unwanted_tiles (prev_affected_tiles , z + 1 , args . tiles_dir )
322+ delete_unwanted_tiles (prev_affected_tiles , z + 1 , tiles_dir )
323323
324324 prev_affected_tiles = curr_affected_tiles
325325
0 commit comments