-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate.py
501 lines (422 loc) · 17.5 KB
/
generate.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import argparse
import json
from collections import defaultdict, Counter, OrderedDict
import re
import csv
import os
import sys
from sqlalchemy import create_engine
from shapely.geometry import shape, MultiPoint
from shapely import wkt
NON_LOWER_RE = re.compile('[^a-z]|aße$|asse$')
MAX_YEAR = 2018
def make_name(name):
if '(' in name:
name = name[:name.index('(')].strip()
return NON_LOWER_RE.sub('', name.lower().replace('ß', 'ss'))
def clean_street(street):
parts = street.split(' / ')
return list(OrderedDict([(p.strip(), None) for p in parts]).keys())
class GeoIndex(object):
def __init__(self, filename, district_filename, engine_config,
mapping=None, district_history=None):
with open(filename) as f:
streets = json.load(f)
with open(district_filename) as f:
districts = json.load(f)
self.engine = create_engine(engine_config)
self.features = streets['features']
district_history = district_history or {}
self.district_history = {}
for k in district_history:
self.district_history[k] = shape({
"type": "Point",
"coordinates": district_history[k]
})
mapping = mapping or {}
self.mapping = {}
for k in mapping:
if isinstance(mapping[k], list):
self.features.append({
"type": "Feature",
'properties': {'name': k, 'osmid': -1},
'geometry': {'type': 'Point', 'coordinates': mapping[k]}
})
else:
self.mapping[make_name(k)] = make_name(mapping[k])
self.lost_streets = Counter()
self.districts = {}
for feature in districts['features']:
self.districts[feature['properties']['spatial_name']] = shape(feature['geometry'])
self.shapes = [shape(feature['geometry']) for feature in self.features]
print('Calculating lengths...', file=sys.stderr)
self.shape_lengths = [self.get_shape_length(s) for s in self.shapes]
print('Done Calculating lengths...', file=sys.stderr)
self.names = defaultdict(list)
for i, feature in enumerate(self.features):
self.names[make_name(feature['properties']['name'])].append(i)
def get_weighted_streets(self, year):
for feat, count in self.street_counter.items():
yield {
"type": "Feature",
"properties": {
"name": self.features[feat]['properties']['name'],
"year": int(year),
"count": count,
"weighted_count": count / self.shapes[feat].length
},
"geometry": self.features[feat]['geometry']
}
def find_by_name(self, original_name, district=None, year=None):
if district:
if district not in self.districts and district not in self.district_history:
raise Exception('Missing district %s' % district)
if district in self.districts:
district = self.districts[district]
else:
district = self.district_history[district]
else:
district = None
name = make_name(original_name)
if name in self.mapping:
name = self.mapping[name]
if name in self.names:
candidates = [i for i in self.names[name]]
if len(candidates) > 1:
return self.get_best_for_district(candidates, district)
if candidates:
return candidates[0]
self.lost_streets[original_name] += 1
return None
def get_best_for_district(self, candidates, district=None):
if district is None and len(candidates) > 1:
raise Exception('More than one candidate, but no district: %s' %
[self.features[x]['properties']['name'] for x in candidates])
new_candidates = []
for candidate in candidates:
candidate_shape = self.shapes[candidate]
if district is None:
new_candidates.append((float('infinity'), candidate))
else:
new_candidates.append((district.distance(candidate_shape), candidate))
new_candidates.sort(key=lambda x: x[0])
return new_candidates[0][1]
def get_st_closest_point(self, a, b):
result = self.engine.execute('''SELECT
ST_AsText(ST_ClosestPoint(foo.a, foo.b)) AS a_b,
ST_AsText(ST_ClosestPoint(foo.b, foo.a)) As b_a
FROM (
SELECT '%s'::geometry As a, '%s'::geometry As b
) AS foo;''' % (
a.wkt, b.wkt
)).fetchall()
return wkt.loads(result[0][0]), wkt.loads(result[0][1])
def get_shape_length(self, shape):
result = self.engine.execute('''SELECT ST_Length(the_geog) As length_spheroid,
ST_Length(the_geog, false) As length_sphere
FROM (
SELECT ST_GeographyFromText(
'SRID=4326;%s')
As the_geog)
As foo;''' % (shape.wkt)).fetchall()
return result[0][0]
def get_georeference(self, streets, district=None, year=None):
len_streets = len(streets)
features = [self.find_by_name(street, district, year) for street in streets]
features = [feature for feature in features if feature is not None]
center = self.get_center(features, len_streets)
return {
'streets': streets,
'center': center,
'features': [self.features[f] for f in features],
'feature_idx': features
}
def get_center(self, features, len_streets):
if len(features) > 1:
mid_points = []
# FIXME: make this more robust
for a, b in zip(features[:-1], features[1:]):
closest_a, closest_b = self.get_st_closest_point(self.shapes[a], self.shapes[b])
mid_points.append(((closest_a.x + closest_b.x) / 2, (closest_a.y + closest_b.y) / 2))
center = MultiPoint(mid_points).centroid
return center
if not features:
return None
feat = features[0]
if len_streets == 1:
center = self.shapes[feat].centroid
a, _ = self.get_st_closest_point(self.shapes[feat], center)
return a
if len_streets > 1:
mid_point = self.shapes[feat].centroid
return mid_point
def get_accidents_for_year(self, year):
reader = csv.DictReader(open('csvs/%d.csv' % year))
for lineno, line in enumerate(reader, start=1):
if not line['directorate']:
print(year, lineno, line, file=sys.stderr)
streets = clean_street(line['street'])
geo_data = self.get_georeference(streets,
district=line['directorate'],
year=year)
geo_data['year'] = year
geo_data.update(line)
yield geo_data
def get_accidents(self, years):
for year in years:
yield from self.get_accidents_for_year(year)
def write_geojson(fh, generator):
fh.write('{"type":"FeatureCollection","features":[')
first = True
for feat in generator:
if first:
first = False
else:
fh.write(',')
json.dump(feat, fh)
fh.write(']}')
def write_csv(fh, generator):
writer = None
for x in generator:
if writer is None:
writer = csv.DictWriter(fh, list(x.keys()))
writer.writeheader()
writer.writerow(x)
def get_accident_feature(accident):
return {
"type": "Feature",
"properties": {
"name": accident['street'],
"year": int(accident['year']),
"count": int(accident['count']),
"directorate": accident['directorate'],
"street_count": len(accident['streets'])
}
}
def get_accidents_as_points(idx, accidents):
for accident in accidents:
center = accident['center']
if center is None:
continue
acc_feat = get_accident_feature(accident)
acc_feat['geometry'] = {
"type": "Point",
"coordinates": [center.x, center.y]
}
yield acc_feat
def get_accidents_as_lines(idx, accidents):
accidents_by_feature = defaultdict(int)
for accident in accidents:
for feat_id in accident['feature_idx']:
accidents_by_feature[feat_id] += int(accident['count'])
for feat_id, count in accidents_by_feature.items():
feat = idx.features[feat_id]
length = idx.shape_lengths[feat_id]
if not length:
count_by_length = None
else:
count_by_length = count / length
yield {
"type": "Feature",
"properties": {
"name": feat['properties']['name'],
"length": length,
"count": count,
"count_by_length": count_by_length
},
"geometry": feat['geometry']
}
#
# for accident in accidents:
# for feat_id, feat in zip(accident['feature_idx'], accident['features']):
# acc_feat = get_accident_feature(accident)
# acc_feat['geometry'] = feat['geometry']
# yield acc_feat
def get_accidents_as_features(idx, accidents):
for accident in accidents:
if len(accident['features']) > 1:
yield from get_accidents_as_points(idx, [accident])
else:
yield from get_accidents_as_lines(idx, [accident])
def get_accident_list(idx, accidents):
for accident in accidents:
center = accident['center']
if center is None:
continue
oneway_ratio = None
ride_length = None
shape_length = None
if len(accident['features']) == 1:
props = idx.features[accident['feature_idx'][0]]['properties']
oneway_ratio = props.get('oneway_length', 0) / props.get('total_length', 1)
shape_length = idx.shape_lengths[accident['feature_idx'][0]]
# Count full non-oneway street as double (both directions)
ride_length = (2 - oneway_ratio) * shape_length
yield {
'street': accident['street'],
'count': accident['count'],
'year': accident['year'],
'directorate': accident['directorate'],
'lat': center.x,
'lng': center.y,
'oneway_ratio': oneway_ratio,
'feature_count': len(accident['features']),
'feature_length': shape_length,
'ride_length': ride_length,
'features': '-'.join(str(x) for x in sorted(int(x['properties']['osmid']) for x in accident['features']))
}
def get_accident_list_split(idx, accidents):
for accident in accidents:
center = accident['center']
if center is None:
continue
count = len(accident['features'])
for feat in accident['feature_idx']:
props = idx.features[feat]['properties']
oneway_ratio = props.get('oneway_length', 0) / props.get('total_length', 1)
shape_length = idx.shape_lengths[feat]
# Count full non-oneway street as double (both directions)
ride_length = (2 - oneway_ratio) * shape_length
yield {
'street': accident['street'],
'single_street': idx.features[feat]['properties']['name'],
'count': int(accident['count']) / count,
'year': accident['year'],
'directorate': accident['directorate'],
'lat': center.x,
'lng': center.y,
'oneway_ratio': oneway_ratio,
'feature_count': count,
'feature_length': shape_length,
'ride_length': ride_length,
'features': '-'.join(str(x) for x in sorted(int(x['properties']['osmid']) for x in accident['features'])),
'single_feature': idx.features[feat]['properties']['osmid']
}
def get_accident_street_list(idx, accidents):
for accident in accidents:
if not accident['features']:
yield {
'osmid': None,
'name': accident['street'],
'count': accident['count'],
'year': accident['year'],
'directorate': accident['directorate'],
'length': None
}
continue
for feat_id, feat in zip(accident['feature_idx'], accident['features']):
yield {
'osmid': feat['properties']['osmid'],
'name': feat['properties']['name'],
'count': accident['count'],
'year': accident['year'],
'directorate': accident['directorate'],
'length': idx.shape_lengths[feat_id]
}
break
def time_compare(idx, accidents):
YEAR_COUNT = 3.0
year_stats = defaultdict(lambda: defaultdict(int))
for accident in accidents:
for feat_id in accident['feature_idx']:
year = int(accident['year'])
year_stats[feat_id][year] += int(accident['count'])
last_year = MAX_YEAR + 1
new_years = (last_year - YEAR_COUNT, last_year)
old_years = (new_years[0] - YEAR_COUNT, new_years[0])
for feat_id, year_stat in year_stats.items():
feat = idx.features[feat_id]
length = idx.shape_lengths[feat_id]
old_years_count = sum(year_stat[y] for y in range(*old_years))
new_years_count = sum(year_stat[y] for y in range(*new_years))
difference = new_years_count - old_years_count
old_mean = old_years_count / YEAR_COUNT
new_mean = new_years_count / YEAR_COUNT
mean_difference = new_mean - old_mean
mean_difference_percent = 100
if old_mean > 0:
mean_difference_percent = mean_difference / float(old_mean) * 100
percent_change = 100
if old_years_count > 0:
percent_change = difference / float(old_years_count) * 100
old_relative = old_years_count / length
new_relative = new_years_count / length
relative_difference = new_relative - old_relative
relative_difference_percent = 100
if old_relative > 0:
relative_difference_percent = relative_difference / float(old_relative) * 100
yield {
"type": "Feature",
"properties": {
"name": feat['properties']['name'],
"length": length,
"relative_difference": relative_difference,
"relative_difference_percent": relative_difference_percent,
"old_accident_relative": old_relative,
"new_accident_relative": new_relative,
"old_count": old_years_count,
"new_count": new_years_count,
"old_mean": old_mean,
"new_mean": new_mean,
"mean_difference": mean_difference,
"mean_difference_percent": mean_difference_percent,
"difference": difference,
"difference_percent": percent_change
},
"geometry": feat['geometry']
}
def get_missing(idx, accidents):
for a in accidents:
pass
for missing in idx.lost_streets.most_common():
yield {
'name': missing[0],
'original_name': missing[0],
'count': missing[1],
'type': 'accidents',
'osmid': None
}
# for feat in idx.features:
# yield {
# 'name': feat['properties']['name'],
# 'original_name': feat['properties']['name'],
# 'count': 0,
# 'type': 'osm',
# 'osmid': feat['properties']['osmid'],
# }
GENERATORS = {
'accident_points': (get_accidents_as_points, 'geojson'),
'accident_streets': (get_accidents_as_lines, 'geojson'),
'accidents': (get_accidents_as_features, 'geojson'),
'accident_list': (get_accident_list, 'csv'),
'accident_list_split': (get_accident_list_split, 'csv'),
'street_list': (get_accident_street_list, 'csv'),
'missing': (get_missing, 'csv'),
'time_compare': (time_compare, 'geojson'),
}
OUTPUT_WRITER = {
'csv': write_csv,
'geojson': write_geojson
}
def main(name, years, engine=None):
engine = engine or os.environ.get('DATABASE_URL')
idx = GeoIndex('geo/berlin_streets.geojson',
'geo/polizeidirektionen.geojson',
engine_config=engine,
mapping=json.load(open('geo/missing_mapping.json')),
district_history=json.load(open('geo/policedistrict_historic.json')))
if not years:
years = list(range(2008, MAX_YEAR + 1))
else:
years = [int(y) for y in years.split(',')]
accident_generator = idx.get_accidents(years)
processor, format = GENERATORS[name]
processed = processor(idx, accident_generator)
OUTPUT_WRITER[format](sys.stdout, processed)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate different output files from bike accident data.')
parser.add_argument('name', choices=list(GENERATORS.keys()))
parser.add_argument('--engine', help='PostGIS engine URL')
parser.add_argument('--years', help='years')
args = parser.parse_args()
main(args.name, args.years, engine=args.engine)