mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 02:41:09 +00:00
reformat with black
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
#pmtiles to files
|
||||
# pmtiles to files
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from pmtiles.convert import mbtiles_to_pmtiles, pmtiles_to_mbtiles, pmtiles_to_dir
|
||||
|
||||
parser = argparse.ArgumentParser(description='Convert between PMTiles and other archive formats.')
|
||||
parser.add_argument('input',help='Input .mbtiles or .pmtiles')
|
||||
parser.add_argument('output',help='Output .mbtiles, .pmtiles, or directory')
|
||||
parser.add_argument('--maxzoom', help='the maximum zoom level to include in the output.')
|
||||
parser.add_argument('--gzip', help='The output should be gzip-compressed.',action='store_true')
|
||||
parser.add_argument('--overwrite', help='Overwrite the existing output.',action='store_true')
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert between PMTiles and other archive formats."
|
||||
)
|
||||
parser.add_argument("input", help="Input .mbtiles or .pmtiles")
|
||||
parser.add_argument("output", help="Output .mbtiles, .pmtiles, or directory")
|
||||
parser.add_argument(
|
||||
"--maxzoom", help="the maximum zoom level to include in the output."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--gzip", help="The output should be gzip-compressed.", action="store_true"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--overwrite", help="Overwrite the existing output.", action="store_true"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if os.path.exists(args.output) and not args.overwrite:
|
||||
@@ -26,10 +34,10 @@ if args.overwrite:
|
||||
|
||||
print("compression:", "gzip" if args.gzip else "disabled")
|
||||
|
||||
if args.input.endswith('.mbtiles') and args.output.endswith('.pmtiles'):
|
||||
if args.input.endswith(".mbtiles") and args.output.endswith(".pmtiles"):
|
||||
mbtiles_to_pmtiles(args.input, args.output, args.maxzoom, args.gzip)
|
||||
|
||||
elif args.input.endswith('.pmtiles') and args.output.endswith('.mbtiles'):
|
||||
elif args.input.endswith(".pmtiles") and args.output.endswith(".mbtiles"):
|
||||
pmtiles_to_mbtiles(args.input, args.output, args.gzip)
|
||||
|
||||
elif args.input.endswith(".pmtiles"):
|
||||
|
||||
@@ -13,51 +13,58 @@ from pmtiles.reader import read
|
||||
class ThreadingSimpleServer(ThreadingMixIn, http.server.HTTPServer):
|
||||
pass
|
||||
|
||||
parser = argparse.ArgumentParser(description='Convert between PMTiles and other archive formats.')
|
||||
parser.add_argument('pmtiles_file',help='PMTiles archive to serve')
|
||||
parser.add_argument('port',help='Port to bind to')
|
||||
parser.add_argument('--bind', help='Address to bind server to: default localhost')
|
||||
parser.add_argument('--cors-allow-all', help='Return Access-Control-Allow-Origin:* header',action='store_true')
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert between PMTiles and other archive formats."
|
||||
)
|
||||
parser.add_argument("pmtiles_file", help="PMTiles archive to serve")
|
||||
parser.add_argument("port", help="Port to bind to")
|
||||
parser.add_argument("--bind", help="Address to bind server to: default localhost")
|
||||
parser.add_argument(
|
||||
"--cors-allow-all",
|
||||
help="Return Access-Control-Allow-Origin:* header",
|
||||
action="store_true",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
with read(args.pmtiles_file) as reader:
|
||||
fmt = reader.metadata['format']
|
||||
fmt = reader.metadata["format"]
|
||||
|
||||
class Handler(http.server.SimpleHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
if self.path == "/metadata":
|
||||
self.send_response(200)
|
||||
if args.cors_allow_all:
|
||||
self.send_header('Access-Control-Allow-Origin','*')
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(reader.metadata).encode('utf-8'))
|
||||
self.wfile.write(json.dumps(reader.metadata).encode("utf-8"))
|
||||
return
|
||||
match = re.match("/(\d+)/(\d+)/(\d+)." + fmt, self.path)
|
||||
if not match:
|
||||
self.send_response(400)
|
||||
self.end_headers()
|
||||
self.wfile.write("bad request".encode('utf-8'))
|
||||
self.wfile.write("bad request".encode("utf-8"))
|
||||
return
|
||||
z = int(match.group(1))
|
||||
x = int(match.group(2))
|
||||
y = int(match.group(3))
|
||||
data = reader.get(z,x,y)
|
||||
data = reader.get(z, x, y)
|
||||
if not data:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write("tile not found".encode('utf-8'))
|
||||
self.wfile.write("tile not found".encode("utf-8"))
|
||||
return
|
||||
self.send_response(200)
|
||||
if args.cors_allow_all:
|
||||
self.send_header('Access-Control-Allow-Origin','*')
|
||||
if fmt == 'pbf':
|
||||
self.send_header('Content-Type','application/x-protobuf')
|
||||
self.send_header("Access-Control-Allow-Origin", "*")
|
||||
if fmt == "pbf":
|
||||
self.send_header("Content-Type", "application/x-protobuf")
|
||||
else:
|
||||
self.send_header('Content-Type','image/' + fmt)
|
||||
self.send_header("Content-Type", "image/" + fmt)
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
bind = args.bind or 'localhost'
|
||||
print(f'serving {bind}:{args.port}/{{z}}/{{x}}/{{y}}.{fmt}, for development only')
|
||||
httpd = ThreadingSimpleServer((args.bind or '', int(args.port)), Handler)
|
||||
bind = args.bind or "localhost"
|
||||
print(f"serving {bind}:{args.port}/{{z}}/{{x}}/{{y}}.{fmt}, for development only")
|
||||
httpd = ThreadingSimpleServer((args.bind or "", int(args.port)), Handler)
|
||||
httpd.serve_forever()
|
||||
|
||||
@@ -11,21 +11,21 @@ if len(sys.argv) <= 1:
|
||||
|
||||
with read(sys.argv[1]) as reader:
|
||||
if len(sys.argv) == 2:
|
||||
print('spec version: ',reader.version)
|
||||
print('metadata:')
|
||||
print("spec version: ", reader.version)
|
||||
print("metadata:")
|
||||
for k, v in reader.metadata.items():
|
||||
print(k,'=',v)
|
||||
print('root entries:', reader.root_entries)
|
||||
print('leaf directories:', len(set(reader.leaves.values())))
|
||||
print(k, "=", v)
|
||||
print("root entries:", reader.root_entries)
|
||||
print("leaf directories:", len(set(reader.leaves.values())))
|
||||
elif len(sys.argv) == 3:
|
||||
for k,v in reader.root_dir.items():
|
||||
for k, v in reader.root_dir.items():
|
||||
print(f"{k[0]} {k[1]} {k[2]} {v[0]} {v[1]}")
|
||||
for val in set(reader.leaves.values()):
|
||||
leaf_dir, _ = reader.load_directory(val[0],val[1]//17)
|
||||
for k,v in leaf_dir.items():
|
||||
leaf_dir, _ = reader.load_directory(val[0], val[1] // 17)
|
||||
for k, v in leaf_dir.items():
|
||||
print(f"{k[0]} {k[1]} {k[2]} {v[0]} {v[1]}")
|
||||
else:
|
||||
z = int(sys.argv[2])
|
||||
x = int(sys.argv[3])
|
||||
y = int(sys.argv[4])
|
||||
print(reader.get(z,x,y))
|
||||
print(reader.get(z, x, y))
|
||||
|
||||
Reference in New Issue
Block a user