-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
176 lines (133 loc) · 4.63 KB
/
cli.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import yaml
import click
from shutil import rmtree
from sh import wget, bzip2, osm2pgsql, npm, carto, createdb, psql, dropuser,\
dropdb, python, ln
from os import environ, makedirs, path
from os.path import abspath, join as pjoin
from path import Path
from distutils.core import run_setup
DB_HOST = '127.0.0.1'
DB_USER = 'gis'
DB_PASS = 'gis'
DB_NAME = 'gis'
DATA_DIR = 'data'
OSM_CARTO_STYLE_FILE = 'style.xml'
OSM_DUMP_FILE_UNCOMPRESSED = 'berlin-latest.osm'
OSM_DUMP_PATH_UNCOMPRESSED = abspath(pjoin(DATA_DIR, OSM_DUMP_FILE_UNCOMPRESSED))
OSM_DUMP_FILE = OSM_DUMP_FILE_UNCOMPRESSED + '.bzip2'
OSM_DUMP_PATH = pjoin(DATA_DIR, OSM_DUMP_FILE)
OSM_DUMP_URL = 'http://download.geofabrik.de/europe/germany/'+OSM_DUMP_FILE
def mapnik_is_installed():
try:
import mapnik
return mapnik.mapnik_version() > 0
except ImportError:
return False
def mapnik_install():
if mapnik_is_installed():
return
environ['PYCAIRO'] = "true"
with Path("libs/python-mapnik"):
dist = run_setup('setup.py')
dist.run_command('clean')
dist.run_command('install')
del environ['PYCAIRO']
@click.group(help="mapnik related commands")
def mapnik():
pass
@mapnik.command()
def is_installed():
if mapnik_is_installed():
click.secho("Mapnik is installed and works!", fg='green')
else:
click.secho("Mapnik is missing!", fg='red')
click.echo("Run 'mapnik install' do fix this")
@mapnik.command()
def install():
mapnik_install()
click.secho("Mapnik installed successfully!", fg='green')
@click.group(help="osm related commands")
def osm():
pass
@osm.command()
def generate_style():
with Path("libs/openstreetmap-carto"):
click.echo("Installing carto")
npm('install', 'carto')
click.echo("Updating credentials in project.mml")
credentials = {
'host': DB_HOST,
'dbname': DB_NAME,
'user': DB_USER,
'password': DB_PASS
}
with open("project.mml", "r+") as f:
project = yaml.safe_load(f)
for layer in project['Layer']:
layer['Datasource'].update(credentials)
f.seek(0)
f.write(yaml.dump(project))
click.echo("Downloading shapefiles")
python("scripts/get-shapefiles.py", "-s")
click.echo("Generating style")
carto("project.mml", "-f", OSM_CARTO_STYLE_FILE)
#with Path("data"):
# ln("-s", pjoin("../libs/openstreetmap-carto/", OSM_CARTO_STYLE_FILE))
@osm.command()
def download_dump():
makedirs(DATA_DIR, exist_ok=True)
if not path.isfile(OSM_DUMP_PATH_UNCOMPRESSED):
click.echo("Downloading OSM dump")
wget('-nc', '-P'+DATA_DIR, OSM_DUMP_URL, _fg=True)
click.echo("Decompressing downloaded OSM dump")
click.echo(bzip2('-d', OSM_DUMP_PATH))
click.secho("OSM dump: "+ OSM_DUMP_PATH_UNCOMPRESSED, fg="green")
@osm.command()
def import_dump():
with Path("libs/openstreetmap-carto"):
environ['PGPASSWORD'] = "gis"
osm2pgsql('-H'+DB_HOST, '-U'+DB_USER, '-d'+DB_NAME, '-G', '--hstore', OSM_DUMP_PATH_UNCOMPRESSED,
style='openstreetmap-carto.style',
tag_transform_script='openstreetmap-carto.lua',
_fg=True)
click.secho("Successfully imported : "+ OSM_DUMP_FILE_UNCOMPRESSED, fg="green")
@click.group()
def cli():
pass
cli.add_command(mapnik)
cli.add_command(osm)
@cli.command(help="Creates postgres user, database and extensions")
def initdb():
environ['PGUSER'] = "postgres"
click.echo("Creating user")
psql("-c CREATE USER "+DB_USER+" WITH PASSWORD '"+DB_PASS+"';")
click.echo("Creating databases")
createdb("-Ogis", DB_NAME, encoding='utf-8')
createdb("-Ogis", "maps", encoding='utf-8')
click.echo("Creating extensions")
psql("-d"+DB_NAME,"-c CREATE EXTENSION postgis; CREATE EXTENSION hstore;")
psql("-dmaps", "-c CREATE EXTENSION postgis")
@cli.command(help="Deletes postgres user and corresponding databases")
def deletedb():
environ['PGUSER'] = "postgres"
click.echo("Dropping databases")
dropdb(DB_NAME)
dropdb("maps")
click.echo("Dropping user")
dropuser(DB_USER)
@cli.command(help="clear map directory")
def clearmaps():
from app import app
rmtree(path.join(app.static_folder, 'maps'), ignore_errors=True)
@cli.command(help="Install and configures everything")
@click.pass_context
def init(ctx):
ctx.invoke(install)
ctx.invoke(deletedb)
ctx.invoke(initdb)
ctx.invoke(download_dump)
ctx.invoke(import_dump)
ctx.invoke(generate_style)
if __name__ == '__main__':
cli()