forked from tru-hy/gtfs_shape_mapfit
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_geojson.py
executable file
·61 lines (56 loc) · 3.07 KB
/
generate_geojson.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys, ast
reload(sys)
sys.setdefaultencoding('utf8')
def generate_geojson(stop_file, shapefit_file, shape_file_original, shape_file_fitted):
stop_features = []
shape_features = []
for row in open(stop_file):
row = row.split(';')
error_type = row[0]
if (error_type.startswith("Huge error") or error_type.startswith("No OSM stop")):
stop_id = row[1].split(':')[1].lstrip()
coordinates_hsl = ast.literal_eval(row[2])
feature_hsl = '{"type": "Feature", "geometry": {"type": "Point","coordinates": %s}, "properties": {"ref": "%s", "errorType": "%s", "source": "HSL" }}'%(coordinates_hsl, stop_id, error_type)
stop_features.append(feature_hsl)
if (error_type.startswith("Huge error")):
coordinates_osm = ast.literal_eval(row[3])
feature_osm = '{"type": "Feature", "geometry": {"type": "Point","coordinates": %s}, "properties": {"ref": "%s", "errorType": "%s", "source": "OSM" }}'%(coordinates_osm, stop_id, error_type)
stop_features.append(feature_osm)
for row in open(shapefit_file):
row = row.split(';')
error_type = row[0]
if (error_type.startswith("Probably bad fit") or error_type.startswith("Outliers found")):
route_id = row[1].split(':')[1].lstrip()
coordinates_hsl = find_coordinates(shape_file_original, route_id)
coordinates_osm = find_coordinates(shape_file_fitted, route_id)
if (error_type.startswith("Probably bad fit")):
score = row[2].split(':')[1].lstrip()
score_limit = row[3].split(': ')[1].lstrip()
properties_hsl = '{"ref": "%s", "errorType": "%s", "source": "HSL", "score" : "%s", "scoreLimit" : "%s"}'%(route_id, error_type, score, score_limit)
properties_osm = '{"ref": "%s", "errorType": "%s", "source": "HSL Fitted", "score" : "%s", "scoreLimit" : "%s"}'%(route_id, error_type, score, score_limit)
else:
outliers = row[2].split(':')[1]
properties_hsl = '{"ref": "%s", "errorType": "%s", "source": "HSL", "outliers" : "%s"}'%(route_id, error_type, outliers)
properties_osm = '{"ref": "%s", "errorType": "%s", "source": "HSL Fitted", "outliers" : "%s"}'%(route_id, error_type, outliers)
feature_hsl = '{"type": "Feature", "geometry": {"type": "LineString", "coordinates": %s}, "properties": %s}'%(coordinates_hsl, properties_hsl)
feature_osm = '{"type": "Feature", "geometry": {"type": "LineString", "coordinates": %s}, "properties": %s}'%(coordinates_osm, properties_osm)
shape_features.append(feature_hsl)
shape_features.append(feature_osm)
all_features = stop_features + shape_features
feature_collection = '{"type": "FeatureCollection", "features": [%s]}'%(", ".join([feature.decode('utf8') for feature in all_features]))
sys.stdout.write(feature_collection)
def find_coordinates(file, route_id):
coordinates = []
for row in open(file):
row = row.split(',')
list_route_id = row[0]
if route_id == list_route_id:
coordinate_lat = ast.literal_eval(row[1])
coordinate_lon = ast.literal_eval(row[2])
coordinates.append([coordinate_lon, coordinate_lat])
return coordinates
if __name__ == '__main__':
import argh
argh.dispatch_command(generate_geojson)