Skip to content

Commit

Permalink
Merge pull request #10 from openmaptiles/generate-metadata
Browse files Browse the repository at this point in the history
generate-metadata  tool :   ( add metadata to mbtiles )
  • Loading branch information
lukasmartinelli authored Jan 9, 2017
2 parents ef24d52 + fa8630d commit 8000f18
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM python:3.6

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
graphviz \
graphviz \
sqlite3 \
&& rm -rf /var/lib/apt/lists/

RUN mkdir -p /usr/src/app
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.py27
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM python:2.7

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
graphviz \
graphviz \
sqlite3 \
&& rm -rf /var/lib/apt/lists/

RUN mkdir -p /usr/src/app
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ buildtest:
release:
@echo "Release: $(VERSION)"
docker build -f Dockerfile -t openmaptiles/openmaptiles-tools:$(VERSION) .
docker images | grep $(DOCKER_IMAGE) | grep -v $(DOCKER_IMAGE_PY27) | grep $(VERSION)
docker build -f Dockerfile -t openmaptiles/openmaptiles-tools:latest .
docker images | grep $(DOCKER_IMAGE) | grep -v $(DOCKER_IMAGE_PY27)
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ Check out the [OpenMapTiles project](https://github.com/openmaptiles/openmaptile
## Install

You need Python 2 or Python 3 installed on your system.
External dependency: graphviz, sqlite3

```
pip install openmaptiles-tools
# As long as we are not published to PyPI you can install directly from git
pip install git+https://github.com/openmaptiles/openmaptiles-tools
#Some tool call external command, you can install them on debian / ubuntu :
apt-get install graphviz sqlite3
```

## Concepts
Expand Down Expand Up @@ -105,6 +110,8 @@ generate-doc <tileset>
### Generate ETL (Extract-Transform-Load ) graph
dependency: graphviz
Takes a source code from the imposm3 mapping file and the SQL postprocessing code ,
nad parsing for the `etldoc:` graphviz based comments, and generate an svg file.
The `.dot` and the `.svg` filename prefix is `etl_`
Expand All @@ -131,5 +138,11 @@ example:
generate-sqlquery layers/landcover/landcover.yaml 14
```
### Add simple metadata to mbtiles file
dependency: sqlite3
example:
```
generate_metadata ./data/tiles.mbtiles
```
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2
0.3
110 changes: 110 additions & 0 deletions bin/generate-metadata
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">&copy; OpenMapTiles</a> <a href="http://www.openstreetmap.org/about/" target="_blank">&copy; 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))
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
license='MIT',
long_description=open('README.md').read(),
scripts=[
'bin/generate-tm2source',
'bin/generate-imposm3',
'bin/generate-sql',
'bin/generate-sqlquery',
'bin/generate-doc',
'bin/generate-etlgraph',
'bin/generate-imposm3',
'bin/generate-mapping-graph',
'bin/generate-metadata',
'bin/generate-sql',
'bin/generate-sqlquery',
'bin/generate-tm2source',
],
install_requires=[
'docopt',
Expand Down

0 comments on commit 8000f18

Please sign in to comment.