-
Notifications
You must be signed in to change notification settings - Fork 0
/
bifidus_cli.py
189 lines (160 loc) · 5.08 KB
/
bifidus_cli.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
#!/usr/bin/env python
# coding: utf-8
import requests
import csv
import sys
import argparse
parser = argparse.ArgumentParser(
prog="Bifidus, improve your transit",
epilog="Mobility open data, proudly crafted by the OpenStreetMap community - An open source tool by Jungle Bus",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Check quality of OpenStreetMap transport data",
)
parser.add_argument(
"--useful-links",
"-u",
metavar="FILE",
type=argparse.FileType("r"),
help="a csv file with useful links",
)
parser.add_argument(
"--route-masters",
"-l",
metavar="FILE",
type=argparse.FileType("r"),
help="a csv file with OSM route_master relations",
)
parser.add_argument(
"--routes",
"-r",
metavar="FILE",
type=argparse.FileType("r"),
help="a csv file with OSM route relations",
)
parser.add_argument(
"--name",
"-n",
default="My Awesome OpenStreetMap transport project",
help="the name of your project",
)
args = parser.parse_args()
tt = csv.DictReader(args.route_masters)
osm_lines = list(tt)
tt = csv.DictReader(args.routes)
osm_routes = list(tt)
conf = None
if args.useful_links:
conf = csv.DictReader(args.useful_links)
project_name = args.name
osmose_url = "http://osmose.openstreetmap.fr/fr/api/0.3/issues?osm_type=relation&osm_id={}&full=true"
josm_url = "http://localhost:8111/load_object?relation_members=true&objects="
# Guess format of osm id
sample_osm_id = osm_lines[0]["line_id"]
try:
tmp = int(sample_osm_id) # 10512380
osm_relations = [line["line_id"] for line in osm_lines] + [
line["route_id"] for line in osm_routes
]
except:
try:
tmp = int(sample_osm_id.split("r")[-1]) # r10512380
osm_relations = [line["line_id"].split("r")[-1] for line in osm_lines] + [
line["route_id"].split("r")[-1] for line in osm_routes
]
except:
try:
tmp = int(sample_osm_id.split("/")[-1]) # relation/10512380
osm_relations = [line["line_id"].split("/")[-1] for line in osm_lines] + [
line["route_id"].split("/")[-1] for line in osm_routes
]
except:
try:
tmp = int(sample_osm_id.split(":")[-1]) # relation:10512380
osm_relations = [
line["line_id"].split(":")[-1] for line in osm_lines
] + [line["route_id"].split(":")[-1] for line in osm_routes]
except:
print(
"Error: Could not guess format of osm_id: need an line_id/route_id column with valid osm_id (for instance r10512380)"
)
sys.exit()
seems_ok = []
to_check = []
errors = {}
for a_relation in osm_relations:
osmose_ = osmose_url.format(a_relation)
osmose_call = requests.get(
osmose_,
headers={
"Origin": "https://jungle-bus.github.io/bifidus/",
"From": "[email protected]",
},
)
osmose_results = osmose_call.json()["issues"]
if not osmose_results:
seems_ok.append(a_relation)
continue
for error in osmose_results:
error_type = "{}_{}".format(error["item"], error["class"])
if error_type not in errors:
output_error = {
"title": error["title"],
"subtitle": error["subtitle"],
"objects": [],
}
errors[error_type] = output_error
errors[error_type]["objects"].append(a_relation)
to_check.append(a_relation)
seems_ok_objects = ["r{}".format(relation) for relation in seems_ok]
josm_ok = "{}{}".format(josm_url, ",".join(seems_ok_objects))
to_check_objects = ["r{}".format(relation) for relation in to_check]
josm_to_check = "{}{}".format(josm_url, ",".join(to_check_objects))
print("# Analyse qualité pour {}".format(project_name))
if conf:
for link in conf:
print("- [{}]({})".format(link["display"], link["url"]))
print("")
print("")
print("## Erreurs")
print("- {} objets".format(len(osm_relations)))
print(
"- {} objets en erreur : [Charger dans JOSM]({})".format(
len(to_check_objects), josm_to_check
)
)
print(
"- {} objets a priori ok : [Charger dans JOSM]({})".format(
len(seems_ok_objects), josm_ok
)
)
print("")
print("## Liste des erreurs")
for error in errors.values():
title = error["title"]["auto"]
subtitle = ""
if error["subtitle"]:
subtitle = error["subtitle"]["auto"]
if len(error["objects"]) > 4:
object_list = [
"- [{}]({}r{})\n".format(
error["objects"][0], josm_url, error["objects"][0]
),
"- [{}]({}r{})\n".format(
error["objects"][1], josm_url, error["objects"][1]
),
"- et {} autres erreurs de ce type\n".format(len(error["objects"]) - 2),
]
else:
object_list = [
"- [{}]({}r{})\n".format(elem, josm_url, elem) for elem in error["objects"]
]
print(
"""
#### {}
{}
Objet(s):
{}
""".format(
title, subtitle, "".join(object_list)
)
)