rio-pmtiles python package [#338] (#542)

Add rio-pmtiles command line tool. [#338]

This is derived from the original mapbox/rio-mbtiles implementation, with changes:

* output PMTiles only instead of MBTiles.
* Python 3.7+ only.
* remove --implementation, --image-dump, --append/--overwrite, --covers features.
* bump dependency versions.
* better progress reporting; add pyroaring. 
* update README and license texts.
* rio-pmtiles v0.0.6 on PyPI
This commit is contained in:
Brandon Liu
2025-03-24 20:50:53 +08:00
committed by GitHub
parent 1d897f4f7e
commit 63182e525d
36 changed files with 1296 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env python
# pmtiles to files
import argparse
import os
import shutil
from pmtiles.convert import mbtiles_to_pmtiles, pmtiles_to_mbtiles, pmtiles_to_dir, disk_to_pmtiles
parser = argparse.ArgumentParser(
description="Convert between PMTiles and other archive formats."
)
parser.add_argument("input", help="Input .mbtiles, .pmtiles, or directory")
parser.add_argument("output", help="Output .mbtiles, .pmtiles, or directory")
parser.add_argument(
"--maxzoom", help="The maximum zoom level to include in the output. Set to 'auto' when converting from directory to use the highest zoom."
)
parser.add_argument(
"--overwrite", help="Overwrite the existing output.", action="store_true"
)
parser.add_argument(
"--scheme", help="Tiling scheme of the input directory ('ags', 'gwc', 'tms', 'zyx', 'zxy' (default))."
)
parser.add_argument(
"--format", help="Raster image format of tiles in the input directory ('png', 'jpeg', 'webp', 'avif') if not provided in the metadata.", dest="tile_format"
)
parser.add_argument(
"--verbose", help="Print progress when converting a directory to .pmtiles.", action="store_true"
)
args = parser.parse_args()
if os.path.exists(args.output) and not args.overwrite:
print("Output exists, use --overwrite to overwrite the output.")
exit(1)
if args.overwrite:
if os.path.isfile(args.output):
os.remove(args.output)
elif os.path.isdir(args.output):
shutil.rmtree(args.output)
if args.input.endswith(".mbtiles") and args.output.endswith(".pmtiles"):
print("Notice: check out the new PMTiles converter at https://github.com/protomaps/go-pmtiles")
mbtiles_to_pmtiles(args.input, args.output, args.maxzoom)
elif args.input.endswith(".pmtiles") and args.output.endswith(".mbtiles"):
pmtiles_to_mbtiles(args.input, args.output)
elif args.input.endswith(".pmtiles"):
pmtiles_to_dir(args.input, args.output)
elif args.output.endswith(".pmtiles"):
disk_to_pmtiles(args.input, args.output, args.maxzoom, scheme=args.scheme, tile_format=args.tile_format, verbose=args.verbose)
else:
print("Conversion not implemented")

View File

@@ -0,0 +1,3 @@
#!/usr/bin/env python3
print('This command has been deprecated; see the "pmtiles serve" command at https://github.com/protomaps/go-pmtiles')

21
python/pmtiles/bin/pmtiles-show Executable file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env python
import sys
import pprint
from pmtiles.reader import Reader, MmapSource
if len(sys.argv) <= 1:
print("Usage: pmtiles-show PMTILES_FILE")
print("Usage: pmtiles-show PMTILES_FILE Z X Y")
exit(1)
with open(sys.argv[1], "r+b") as f:
reader = Reader(MmapSource(f))
if len(sys.argv) == 2:
pprint.pprint(reader.header())
pprint.pprint(reader.metadata())
else:
z = int(sys.argv[2])
x = int(sys.argv[3])
y = int(sys.argv[4])
sys.stdout.buffer.write(reader.get(z, x, y))