This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfabfile.py
174 lines (142 loc) · 5.7 KB
/
fabfile.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
from __future__ import with_statement
import os
import json
import urllib
from fabric.api import *
def get_version():
"""
Returns the current version of the MapBox project.
"""
f = open('./%s/project.mml' % env.name,'r')
return json.load(f)['version']
env.tilemill = '$TILEMILL/index.js'
env.name = 'silent-la'
env.version = get_version()
env.release_name = '%s-%s' % (env.name, env.version)
env.tile_bucket = 'tiles.latimes.com'
def rollout():
update_osm()
build_tiles()
build_samples()
deploy_tiles()
def download_osm(state='california'):
"""
Download a state slice of OpenStreetMap data from GeoFabrik's
daily snapshots.
Returns a path to the a bz2 file containing the data.
Keyword argument:
state: The name of the state slice you want to install.
Default: 'california'
"""
print('Updating OpenStreetMap slice for the state of %s' % state.title())
# Figure out what file we want
bz2 = '%s-latest.osm.bz2' % state
# Delete it from the local folder if it already exists
if os.path.exists('./%s' % bz2):
print('- Deleting existing OpenStreetMap bz2 from this directory')
os.remove('./%s' % bz2)
# Download a new file from our data source
url ='http://download.geofabrik.de/openstreetmap/north-america/us/%s'
print('- Downloading new OpenStreetMap slice from geofabrik.de')
urllib.urlretrieve(url % bz2, './%s' % bz2)
print('- Download successful')
return bz2
def load_osm(bz2, postgres_user='postgres', postgres_host='localhost'):
"""
Loads a bz2 slice of OSM data into the database.
Keyword arguments:
- postgres_user: The name of the postgres user who can access you
database. Default: 'postgres'
- postgres_host: The postgres hostname or socket location to use
with osm2psql. Default: 'localhost'
"""
# Unzip file
print ('- Unzipping OpenStreetMap bz2')
local('bunzip2 ./%s' % bz2)
print ('- Unzip successful')
# Drop the database if it already exists
state = bz2.replace(".osm.bz2", "")
db = 'osm_%s' % state
try:
local("sudo -u %s dropdb %s" % (postgres_user, db))
print('- Dropped existing database %s' % db)
except:
pass
# Create a new database
print('- Creating new database %s' % db)
local('sudo -u %s createdb -U %s -T template_postgis %s' % (
postgres_user,
postgres_user,
db
))
# Load the database with osm2pgsql
print('- Loading OpenStreetMap data')
osm = '%s.osm' % state
local('osm2pgsql -U %s -H %s -d %s %s' % (postgres_user, postgres_host, db, osm))
# Remove OSM file
print('Removing %s' % osm)
local('rm %s' % osm)
def update_osm(state='california', postgres_user='postgres', postgres_host='localhost'):
"""
Download and install the latest snapshot of the OpenStreetMap database.
What it does:
- Download a state slice of OpenStreetMap data from GeoFabrik's
daily snapshots
- Drop your existing postgre database for that state slice
- Load the latest slice in using osm2psql
Keyword arguments:
- state: The name of the state slice you want to install.
Default: 'california'
- postgres_user: The name of the postgres user who can access you
database. Default: 'postgres'
- postgres_host: The postgres hostname or socket location to use
with osm2psql. Default: 'localhost'
Example usage:
$ fab update_osm:state=iowa
"""
bz2 = download_osm(state)
load_osm(bz2, postgres_user=postgres_user, postgres_host=postgres_host)
def build_tiles():
"""
Create a set of tiles and zip it up so it can be deploy.
"""
print('Building map tiles')
# Have TileMill build the current project as an mbtiles file
local('nodejs %(tilemill)s export %(name)s ./%(release_name)s.mbtiles --format=mbtiles' % env)
# Crack open the mbtiles file as a directory
print('- Opening mbtiles file')
local('mb-util ./%(release_name)s.mbtiles %(release_name)s' % env)
print('- Deleting mbtiles file')
local('rm -rf ./%(release_name)s.mbtiles' % env)
print('- Deleting export log')
local('rm *export*')
def build_samples():
"""
Create a set of predefined sample tiles
"""
# Set the directory for our samples
samples_dir = os.path.join('./samples', env.version)
# Make the directory if it doesn't already exist
os.path.exists(samples_dir) or os.mkdir(samples_dir)
# Delete any existing files in the directory
file_paths = [os.path.join(samples_dir, n)
for n in ['socal.png', 'sfvalley.png', 'dtla.png']]
[os.remove(i) for i in file_paths if os.path.exists(i)]
# Create the new samples
local("nodejs %(tilemill)s export %(name)s samples/%(version)s/socal.png --format=png --bbox=-118.921,34.696,-117.421,33.564 --minzoom=9 --maxzoom=9 --width=1024" % env)
local("nodejs %(tilemill)s export %(name)s samples/%(version)s/sfvalley.png --format=png --bbox=-118.685,34.106,-118.259,34.308 --minzoom=12 --maxzoom=12 --width=1024" % env)
local("nodejs %(tilemill)s export %(name)s samples/%(version)s/dtla.png --format=png --bbox=-118.280,34.0233,-118.216,34.064 --minzoom=14 --maxzoom=14 --width=1024" % env)
def deploy_tiles():
"""
Upload a release folder to the live Amazon S3 bucket.
"""
print('Deploying tiles to Amazon S3')
print('- Uploading files to %s' % env.tile_bucket)
local("ivs3 -P %s %s/%s" % (
env.release_name,
env.tile_bucket,
env.release_name,
)
)
print('- Deleting file directory')
local('rm -rf ./%(release_name)s' % env)