-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from openmaptiles/generate-metadata
generate-metadata tool : ( add metadata to mbtiles )
- Loading branch information
Showing
7 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2 | ||
0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python | ||
"""generate metadata to tiles.mbtiles | ||
Usage: | ||
generate_metadata <mbtiles> | ||
generate_metadata (-h | --help) | ||
generate_metadata --version | ||
Options: | ||
-h --help Show this screen. | ||
--version Show version. | ||
""" | ||
|
||
# generate_metadata ./data/tiles.mbtiles | ||
|
||
# TODO: better BBOX parameters .. | ||
# TODO: test! | ||
|
||
from __future__ import print_function | ||
|
||
import shutil | ||
import sqlite3 | ||
import sys | ||
import os | ||
import os.path | ||
from docopt import docopt | ||
import openmaptiles | ||
|
||
ATTRIBUTION = os.environ.get('METADATA_ATTRIBUTION', '<a href="http://openmaptiles.org/" target="_blank">© OpenMapTiles</a> <a href="http://www.openstreetmap.org/about/" target="_blank">© OpenStreetMap contributors</a>') | ||
VERSION = os.environ.get('METADATA_VERSION', '3.3') | ||
|
||
|
||
class Extract(object): | ||
|
||
def __init__(self, extract, country, city, top, left, bottom, right, | ||
min_zoom=0, max_zoom=14, center_zoom=10): | ||
self.extract = extract | ||
self.country = country | ||
self.city = city | ||
|
||
self.min_lon = left | ||
self.min_lat = bottom | ||
self.max_lon = right | ||
self.max_lat = top | ||
|
||
self.min_zoom = min_zoom | ||
self.max_zoom = max_zoom | ||
self.center_zoom = center_zoom | ||
|
||
def bounds(self): | ||
return '{},{},{},{}'.format(self.min_lon, self.min_lat, | ||
self.max_lon, self.max_lat) | ||
|
||
def center(self): | ||
center_lon = (self.min_lon + self.max_lon) / 2.0 | ||
center_lat = (self.min_lat + self.max_lat) / 2.0 | ||
return '{},{},{}'.format(center_lon, center_lat, self.center_zoom) | ||
|
||
def metadata(self, extract_file): | ||
return { | ||
"type": os.environ.get('METADATA_TYPE', 'baselayer'), | ||
"attribution": ATTRIBUTION, | ||
"version": VERSION, | ||
"minzoom": self.min_zoom, | ||
"maxzoom": self.max_zoom, | ||
"name": os.environ.get('METADATA_NAME', 'OpenMapTiles'), | ||
"id": os.environ.get('METADATA_ID', 'openmaptiles'), | ||
"description": os.environ.get('METADATA_DESC', "Development extract, based on http://openmaptiles.org"), | ||
"bounds": self.bounds(), | ||
"center": self.center(), | ||
"basename": os.path.basename(extract_file), | ||
"filesize": os.path.getsize(extract_file) | ||
} | ||
|
||
def update_metadata(mbtiles_file, metadata): | ||
""" | ||
Update metadata key value pairs inside the MBTiles file | ||
with the provided metadata | ||
""" | ||
conn = sqlite3.connect(mbtiles_file) | ||
|
||
def upsert_entry(key, value): | ||
conn.execute("DELETE FROM metadata WHERE name='{}'".format(key)) | ||
conn.execute("INSERT INTO metadata VALUES('{}', '{}')".format(key, value)) | ||
|
||
for key, value in metadata.items(): | ||
upsert_entry(key, value) | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
if __name__ == '__main__': | ||
args = docopt(__doc__, version=openmaptiles.__version__) | ||
|
||
extract_file= args['<mbtiles>'] | ||
|
||
extract = Extract(extract_file, | ||
country=None, | ||
city=None, | ||
left=-180, | ||
right=180, | ||
top=85.0511, | ||
bottom=-85.0511, | ||
center_zoom=2, | ||
min_zoom= os.environ.get('QUICKSTART_MIN_ZOOM', 0), | ||
max_zoom= os.environ.get('QUICKSTART_MAX_ZOOM', 14), | ||
) | ||
|
||
print('Update metadata {}'.format(extract_file)) | ||
update_metadata(extract_file, extract.metadata(extract_file)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters