-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyc3dcars_bootstrap.py
84 lines (63 loc) · 2.18 KB
/
nyc3dcars_bootstrap.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
#!/usr/bin/env python
import os
import urllib
import gzip
import tarfile
import subprocess
import argparse
db_manifest = [
'geodatabase.dump.gz',
'nyc3dcars.dump.gz',
]
location_manifest = [
'times-square',
'apple',
'macys',
]
image_manifest = ['%s-images.tar.gz'%l for l in location_manifest]
manifest = db_manifest + image_manifest
dbname = 'nyc3dcars'
def create_database (clobberdb):
if clobberdb:
print ('Clobbering database')
subprocess.check_call(['dropdb', dbname])
subprocess.check_call(['createdb', dbname, '--locale=en_US.utf8', '--encoding=utf8', '-T', 'template0'])
subprocess.check_call(['psql', dbname, '-c', 'CREATE EXTENSION postgis'])
srid102718_source = urllib.urlopen('http://spatialreference.org/ref/esri/102718/postgis/')
srid102718 = srid102718_source.read()
srid102718 = srid102718.replace('9102718', '102718')
subprocess.check_call(['psql', dbname, '-c', srid102718])
def download_if_missing (local):
remote = 'https://s3.amazonaws.com/nyc3dcars/%s'%local
if not os.path.exists (local):
print ('Fetching %s'%remote)
urllib.urlretrieve (remote, local)
def restore_psql_dump (filename):
print ('Restoring %s'%filename)
with gzip.open(filename, 'rb') as f:
proc = subprocess.Popen([
'pg_restore',
'--dbname=%s'%dbname,
], stdin=subprocess.PIPE)
proc.stdin.write(f.read())
retval = proc.wait()
if retval != 0:
raise Exception ('Failed to restore %s'%filename)
def extract_archive (filename):
with tarfile.TarFile (filename, 'r') as f:
f.extractall()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument ('--clobberdb', action='store_true')
args = parser.parse_args()
print ('Downloading resources')
for filename in manifest:
download_if_missing (filename)
print ('Creating database')
create_database (args.clobberdb)
print ('Building geodatabase')
for filename in db_manifest:
restore_psql_dump (filename)
print ('Extracting images')
for filename in image_manifest:
extract_archive (filename)