-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclip-bounds.py
162 lines (114 loc) · 4.88 KB
/
clip-bounds.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
from urllib import urlopen
from httplib import HTTPConnection
from urlparse import urlparse, urljoin
from StringIO import StringIO
from tarfile import TarFile
from gzip import GzipFile
from csv import DictReader
from shapely.geometry import MultiPolygon, Polygon
from psycopg2 import connect
def size_and_date(href):
""" Get Content-Length and Last-Modified for a URL.
"""
for attempt in range(10):
s, host, path, p, q, f = urlparse(href)
conn = HTTPConnection(host, 80)
conn.request('HEAD', path)
resp = conn.getresponse()
if resp.status in range(300, 399):
href = urljoin(href, resp.getheader('location'))
continue
if resp.status != 200:
raise IOError('not found')
content_length = resp.getheader('content-length')
last_modified = resp.getheader('last-modified')
return content_length, last_modified
def parse_poly(lines):
""" Parse an Osmosis polygon filter file.
http://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format
"""
in_ring = False
coords = []
for (index, line) in enumerate(lines):
if index == 0:
# first line is junk.
continue
elif index == 1:
# second line is the first polygon ring.
coords.append([[], []])
ring = coords[-1][0]
in_ring = True
elif in_ring and line.strip() == 'END':
# we are at the end of a ring, perhaps with more to come.
in_ring = False
elif in_ring:
# we are in a ring and picking up new coordinates.
ring.append(map(float, line.split()))
elif not in_ring and line.strip() == 'END':
# we are at the end of the whole polygon.
break
elif not in_ring and line.startswith('!'):
# we are at the start of a polygon part hole.
coords[-1][1].append([])
ring = coords[-1][1][-1]
in_ring = True
elif not in_ring:
# we are at the start of a polygon part.
coords.append([[], []])
ring = coords[-1][0]
in_ring = True
return MultiPolygon(coords)
metro_url = 'http://metro.teczno.com/cities.txt'
metro_pattern = 'http://osm-metro-extracts.s3.amazonaws.com/%s.osm.pbf'
gf_url = 'http://download.geofabrik.de/clipbounds/clipbounds.tgz'
gf_base_href = 'http://download.geofabrik.de/osm/'
if __name__ == '__main__':
extracts = []
extract_href = 'http://planet.openstreetmap.org/pbf/planet-latest.osm.pbf'
try:
content_length, last_modified = size_and_date(extract_href)
print extract_href
except IOError:
print 'Failed', extract_href
else:
x1, y1, x2, y2 = -180, -90, 180, 90
shape = Polygon([(x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)])
extracts.append((extract_href, content_length, last_modified, shape))
metro_list = StringIO(urlopen(metro_url).read())
metro_list = DictReader(metro_list, dialect='excel-tab')
for city in metro_list:
extract_href = metro_pattern % city['slug']
try:
content_length, last_modified = size_and_date(extract_href)
print extract_href
except IOError:
print 'Failed', extract_href
continue
y1, x1, y2, x2 = [float(city[key]) for key in ('top', 'left', 'bottom', 'right')]
shape = Polygon([(x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)])
extracts.append((extract_href, content_length, last_modified, shape))
gf_archive = StringIO(urlopen(gf_url).read())
gf_archive = GzipFile(fileobj=gf_archive)
gf_archive = TarFile(fileobj=gf_archive)
for member in gf_archive.getmembers():
if not member.name.endswith('.poly'):
continue
extract_path = member.name[:-5] + '.osm.pbf'
extract_href = urljoin(gf_base_href, extract_path)
try:
content_length, last_modified = size_and_date(extract_href)
print extract_href
except IOError:
print 'Failed', extract_href
continue
lines = list(gf_archive.extractfile(member))
shape = parse_poly(lines)
extracts.append((extract_href, content_length, last_modified, shape))
db = connect(database='tiledrawer', user='tiledrawer').cursor()
db.execute('BEGIN')
db.execute('DELETE FROM extracts')
for (href, size, date, shape) in extracts:
db.execute("""INSERT INTO extracts (href, size, date, geom)
VALUES (%s, %s, %s, SetSRID(Multi(GeomFromText(%s)), 4326))""",
(href, size, date, str(shape)))
db.execute('COMMIT')