mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 22:12:15 +00:00
59 lines
1.8 KiB
Python
Executable File
59 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import click
|
|
|
|
|
|
@click.command()
|
|
@click.argument(
|
|
'metadata_path', type=click.Path(exists=True, dir_okay=False, file_okay=True, path_type=Path)
|
|
)
|
|
@click.argument('tilejson_path', type=click.Path(path_type=Path))
|
|
@click.argument('url_prefix')
|
|
@click.option('--minify', is_flag=True, help='Minify the generated JSON')
|
|
def cli(metadata_path: Path, tilejson_path: Path, url_prefix: str, minify: bool):
|
|
"""
|
|
Takes a MBTiles metadata.json and generates a TileJSON 3.0.0 file
|
|
|
|
URL_PREFIX: Base URL to use as a prefix for tiles in the generated TileJSON.
|
|
|
|
Reference: https://github.com/mapbox/tilejson-spec/tree/master/3.0.0
|
|
"""
|
|
|
|
tilejson = dict(tilejson='3.0.0')
|
|
|
|
with open(metadata_path) as fp:
|
|
metadata = json.load(fp)
|
|
|
|
metadata_json_key = json.loads(metadata.pop('json'))
|
|
|
|
tilejson['tiles'] = [url_prefix.rstrip('/') + '/{z}/{x}/{y}.pbf']
|
|
|
|
''
|
|
tilejson['vector_layers'] = metadata_json_key.pop('vector_layers')
|
|
assert not metadata_json_key # check that no more keys left
|
|
|
|
tilejson['attribution'] = metadata.pop('attribution')
|
|
tilejson['bounds'] = [float(n) for n in metadata.pop('bounds').split(',')]
|
|
tilejson['center'] = [float(n) for n in metadata.pop('center').split(',')]
|
|
tilejson['center'][2] = 1
|
|
|
|
tilejson['description'] = metadata.pop('description')
|
|
|
|
tilejson['maxzoom'] = int(metadata.pop('maxzoom'))
|
|
tilejson['minzoom'] = int(metadata.pop('minzoom'))
|
|
|
|
tilejson['name'] = metadata.pop('name')
|
|
tilejson['version'] = metadata.pop('version')
|
|
|
|
with open(tilejson_path, 'w') as fp:
|
|
if minify:
|
|
json.dump(tilejson, fp, ensure_ascii=False, separators=(',', ':'))
|
|
else:
|
|
json.dump(tilejson, fp, ensure_ascii=False, indent=2)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
cli()
|