mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 14:02:15 +00:00
metadata_to_tilejson
This commit is contained in:
1
scripts/http_host/.gitignore
vendored
Normal file
1
scripts/http_host/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.json
|
||||
59
scripts/http_host/metadata_to_tilejson.py
Executable file
59
scripts/http_host/metadata_to_tilejson.py
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
|
||||
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()
|
||||
@@ -7,6 +7,9 @@ from pathlib import Path
|
||||
import click
|
||||
|
||||
|
||||
RUNS_DIR = Path('/data/ofm/http_host/runs')
|
||||
|
||||
|
||||
@click.command()
|
||||
def cli():
|
||||
if not Path('/etc/fstab').exists():
|
||||
@@ -30,9 +33,42 @@ def cli():
|
||||
|
||||
area, version = subdir.name.split('-')
|
||||
|
||||
run_dir = RUNS_DIR / area / version
|
||||
if not run_dir.is_dir():
|
||||
print(f"{run_dir} doesn't exists, skipping")
|
||||
continue
|
||||
|
||||
tilejson_path = run_dir / 'tilejson-tiles-org.json'
|
||||
|
||||
metadata_path = subdir / 'metadata.json'
|
||||
if not metadata_path.is_file():
|
||||
print(f"{metadata_path} doesn't exists, skipping")
|
||||
continue
|
||||
|
||||
url_prefix = f'https://tiles.openfreemap.org/{area}/{version}/tiles//'
|
||||
|
||||
subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
Path(__file__).parent.parent / 'metadata_to_tilejson.py',
|
||||
'--minify',
|
||||
metadata_path,
|
||||
tilejson_path,
|
||||
url_prefix,
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
|
||||
version_str = rf"""
|
||||
location /{area}/{version}/ {{ # trailing hash important
|
||||
alias {subdir}/; # trailing hash important
|
||||
location /{area}/{version} {{ # no trailing hash
|
||||
alias {tilejson_path}; # no trailing hash
|
||||
|
||||
add_header Cache-Control public;
|
||||
expires 10y;
|
||||
}}
|
||||
|
||||
location /{area}/{version}/ {{ # trailing hash
|
||||
alias {subdir}/tiles/; # trailing hash
|
||||
try_files $uri @empty;
|
||||
|
||||
add_header Cache-Control public;
|
||||
@@ -45,7 +81,7 @@ def cli():
|
||||
if not help_text:
|
||||
help_text = (
|
||||
'\ntest with:\n'
|
||||
f'curl -H "Host: ofm" -I http://localhost/{area}/{version}/tiles/14/8529/5975.pbf'
|
||||
f'curl -H "Host: ofm" -I http://localhost/{area}/{version}/14/8529/5975.pbf'
|
||||
)
|
||||
|
||||
nginx_template = nginx_template.replace('___LOCATION_BLOCKS___', location_block_str)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
server {
|
||||
server_name ofm tiles.openfreemaps.org;
|
||||
server_name ofm tiles.openfreemap.org;
|
||||
|
||||
# disabling access log by default
|
||||
# access_log /data/ofm/http_host/logs_nginx/nginx-access.log access_json buffer=32k;
|
||||
|
||||
Reference in New Issue
Block a user