forked from openmaptiles/openmaptiles-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-metadata
executable file
·118 lines (102 loc) · 4.22 KB
/
generate-metadata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
"""Generate and clean metadata to tiles.mbtiles.
Usage:
generate_metadata [--force] <mbtiles>
generate_metadata [--force] <mbtiles> <west> <south> <east> <north>
generate_metadata (-h | --help)
generate_metadata --version
Options:
-h --help Show this screen.
--version Show version.
--force Overwrite all metadata, not only missing
This script reads environment variable: METADATA_* for overwriting metadata values.
This script parses BBOX, MIN_ZOOM, MAX_ZOOM and CENTER_ZOOM to prepare bounds and center values.
"""
from docopt import docopt
import sqlite3
import os.path
import json
import openmaptiles
from openmaptiles.utils import Bbox
from openmaptiles.consts import *
def update_metadata(mbtiles_file, metadata, force=False):
"""
Update metadata key value pairs inside the MBTiles file
with the provided metadata
"""
conn = sqlite3.connect(mbtiles_file)
cursor = conn.cursor()
def upsert_entry(entry_key, entry_value):
# ID should be overwritten always!
if not force and entry_key != 'id':
cursor.execute("SELECT value FROM metadata WHERE name = ?", (entry_key, ))
for entry_row in cursor:
# Skip if the value exists
# print(f"{entry_key}={entry_row}")
if entry_row[0]:
return
conn.execute("DELETE FROM metadata WHERE name=?", (entry_key, ))
conn.execute("INSERT INTO metadata VALUES(?, ?)", (entry_key, entry_value, ))
for key, value in metadata.items():
upsert_entry(key, value)
# Change json value in metadata
cursor.execute("SELECT name, value FROM metadata WHERE name = 'json'")
metadata_json = ''
for row in cursor:
metadata_json = row[1]
break
if metadata_json:
obj = json.loads(metadata_json)
# Remove Layer - contains connections and SQLs
if 'Layer' in obj:
del obj['Layer']
# Remove unnecessary field _prefs
if '_prefs' in obj:
del obj['_prefs']
# Update maxzoom in vector_layers:
if 'vector_layers' in obj:
# find out maxzoom from metadata
maxzoom = 0
cursor.execute("SELECT name, value FROM metadata WHERE name='maxzoom'")
for row in cursor:
try:
maxzoom = int(row[1])
except:
maxzoom = 0
break
if maxzoom > 0:
vector_layers = obj['vector_layers']
for layer in vector_layers:
layer['maxzoom'] = maxzoom
# Save json back into metadata minified
metadata_json = json.dumps(obj, separators=(',', ':'))
cursor.execute("REPLACE INTO metadata VALUES (?, ?)", ('json', metadata_json, ))
conn.commit()
conn.commit()
conn.close()
def main(args):
extract_file = args['<mbtiles>']
bbox = Bbox(bbox=os.environ.get('BBOX'),
top=args.get('<north>'),
left=args.get('<west>'),
bottom=args.get('<south>'),
right=args.get('<east>'),
center_zoom=os.environ.get('CENTER_ZOOM'))
metadata = {
"type": os.environ.get('METADATA_TYPE', METADATA_TYPE),
"attribution": os.environ.get('METADATA_ATTRIBUTION', METADATA_ATTRIBUTION),
"description": os.environ.get('METADATA_DESCRIPTION', METADATA_DESCRIPTION),
"version": os.environ.get('METADATA_VERSION', METADATA_VERSION),
"minzoom": os.environ.get('MIN_ZOOM', MIN_ZOOM),
"maxzoom": os.environ.get('MAX_ZOOM', MAX_ZOOM),
"name": os.environ.get('METADATA_NAME', METADATA_NAME),
"id": os.environ.get('METADATA_ID', METADATA_ID),
"bounds": bbox.bounds_str(),
"center": bbox.center_str(),
"basename": os.path.basename(extract_file) if extract_file else None,
"filesize": os.path.getsize(extract_file) if extract_file else None,
}
print(f'Update metadata {extract_file}')
update_metadata(extract_file, metadata, args.get('--force', False))
if __name__ == '__main__':
main(docopt(__doc__, version=openmaptiles.__version__, options_first=True))