Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mbtiles/cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def process_tiles(
resampling=None,
img_ext=None,
image_dump=None,
tile_scheme=None,
progress_bar=None,
open_options=None,
warp_options=None,
Expand Down Expand Up @@ -60,7 +61,7 @@ def process_tiles(

for future in done:
tile, contents = future.result()
insert_results(tile, contents, img_ext=img_ext, image_dump=image_dump)
insert_results(tile, contents, img_ext=img_ext, image_dump=image_dump, tile_scheme=tile_scheme)

count += len(done)
if count > BATCH_SIZE:
Expand Down
3 changes: 2 additions & 1 deletion mbtiles/mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def process_tiles(
resampling=None,
img_ext=None,
image_dump=None,
tile_scheme=None,
progress_bar=None,
open_options=None,
warp_options=None,
Expand Down Expand Up @@ -62,7 +63,7 @@ def grouper(iterable, n, fillvalue=None):
if item is None:
break
tile, contents = item
insert_results(tile, contents, img_ext=img_ext, image_dump=image_dump)
insert_results(tile, contents, img_ext=img_ext, image_dump=image_dump, tile_scheme=tile_scheme)

commit_mbtiles()

Expand Down
20 changes: 18 additions & 2 deletions mbtiles/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ def extract_features(ctx, param, value):
metavar="PATH",
help="A directory into which image tiles will be optionally " "dumped.",
)
@click.option(
"--scheme",
"tile_scheme",
type=click.Choice(["xyz", "tms"]),
default="tms",
help="Influences the y direction of the tile coordinates in file names, when `--image-dump` is used.",
)
@click.option(
"-j",
"num_workers",
Expand Down Expand Up @@ -235,6 +242,7 @@ def mbtiles(
tile_size,
zoom_levels,
image_dump,
tile_scheme,
num_workers,
src_nodata,
dst_nodata,
Expand Down Expand Up @@ -531,19 +539,26 @@ def init_mbtiles():
)
conn.commit()

def insert_results(tile, contents, img_ext=None, image_dump=None):
def insert_results(tile, contents, img_ext=None, image_dump=None, tile_scheme="tms"):
"""Also a closure."""
cursor = conn.cursor()
if contents is None:
log.info("Tile %r is empty and will be skipped", tile)
return

# MBTiles have a different origin than Mercantile/tilebelt.
# See https://gist.github.com/tmcw/4954720
# and https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/#raster-scheme.
tiley = int(math.pow(2, tile.z)) - tile.y - 1

# Optional image dump.
if image_dump:
img_name = "{}-{}-{}.{}".format(tile.x, tiley, tile.z, img_ext)
img_name = "{}-{}-{}.{}".format(
tile.x,
tile.y if tile_scheme == "xyz" else tiley,
tile.z,
img_ext
)
img_path = os.path.join(image_dump, img_name)
with open(img_path, "wb") as img:
img.write(contents)
Expand Down Expand Up @@ -589,6 +604,7 @@ def gen_tiles():
resampling=resampling,
img_ext=img_ext,
image_dump=image_dump,
tile_scheme=tile_scheme,
progress_bar=pbar,
open_options=open_options,
creation_options=creation_options,
Expand Down