-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsr2osm.py
1163 lines (845 loc) · 34.9 KB
/
nsr2osm.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf8
# nsr2osm
# Converts public transportation stops from Entur NeTex files and matches with OSM for update in JOSM
# Reads NSR data from Entur NeTEx file (XML)
# Usage: stop2osm.py [-manual | -upload]
# Creates OSM file with name "nsr_update.osm" and log file "nsr_update_log.txt"
# Uploads to OSM if -upload is selected
import sys
import json
import zipfile
import csv
import math
import copy
import time
import datetime
import base64
import os.path
import urllib.request, urllib.error, urllib.parse
from io import BytesIO, TextIOWrapper
from xml.etree import ElementTree as ET
version = "2.0.0"
debug = True
request_header = {"User-Agent": "nsr2osm"}
username = "nsr2osm" # Upload to OSM from this user
osm_api = "https://api.openstreetmap.org/api/0.6/" # Production database
overpass_api = "https://overpass-api.de/api/interpreter"
#overpass_api = "https://overpass.kumi.systems/api/interpreter"
history_filename = "~/Google Drive/Stoppested/nsr_history.json"
exclude_counties = [] # Omit counties (two digit ref's)
#exclude_counties = [03", "11", "15", "18", "30", "34", "38", "42", "46", "50", "54"]
user_whitelist = ["nsr2osm", "ENTUR Johan Wiklund", "Wulfmorn"] # Only modify stops in OSM if last edit is from these users
quays_abroad = ["101150", "15343"] # Quays just outside of Norway border (to avoid duplicates)
out_filename = "nsr_update"
max_distance = 1.0 # Nodes relocated more than or equal to this distance will get new coordinates (meters)
ptv1 = False # True to maintain PTv1 tagging, unless stop is part of PTv2 relation
ptv1_modify = True # True to maintain PTv1 tagging when stop/station is updated for other reasons
# Keys used for manual inspection
manual_keys = ["EDIT", "DISTANCE", "NSR", "NSR_NAME", "NSR_REFERENCE", "USER", "OTHER", "MUNICIPALITY", "VERSION", "STOPTYPE", "SUBMODE", "NSRNOTE", "DELETE", "LAST_USED"]
# Open file/api, try up to 5 times, each time with double sleep time
def open_url (url):
delay = 60 # seconds
tries = 0
while tries < 5:
try:
return urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
if e.code in [429, 503, 504]: # Too many requests, Service unavailable or Gateway timed out
if tries == 0:
message ("\n")
message ("\rRetry %i in %ss... " % (tries + 1, delay * (2**tries)))
time.sleep(delay * (2**tries))
tries += 1
error = e
elif e.code in [401, 403]:
message ("\nHTTP error %i: %s\n" % (e.code, e.reason)) # Unauthorized or Blocked
sys.exit()
elif e.code in [400, 409, 412]:
message ("\nHTTP error %i: %s\n" % (e.code, e.reason)) # Bad request, Conflict or Failed precondition
message ("%s\n" % str(e.read()))
sys.exit()
else:
raise
except urllib.error.URLError as e: # Mostly "Connection timed out"
if tries == 0:
message ("\n")
message ("\r\tRetry %i in %ss... " % (tries + 1, delay * (2**tries)))
time.sleep(delay * (2**tries))
tries += 1
message ("\nHTTP error %i: %s\n" % (error.code, error.reason))
sys.exit()
# Output message
def message (output_text):
sys.stdout.write (output_text)
sys.stdout.flush()
# Log query results
def log(log_text):
if debug:
log_file.write(log_text)
# Compute approximation of distance between two coordinates, in meters
# Works for short distances
# Format: (lon, lat)
def compute_distance (p1, p2):
lon1, lat1, lon2, lat2 = map(math.radians, [p1[0], p1[1], p2[0], p2[1]])
x = (lon2 - lon1) * math.cos( 0.5*(lat2+lat1) )
y = lat2 - lat1
return round(6371000 * math.sqrt( x*x + y*y ), 1) # One decimal
# Generate OSM/XML for one OSM element, including for changeset
# Parameter:
# - element: Dict of OSM element in same format as returned by Overpass API
# 'action' contains 'create', 'modify' or 'delete' (or is not present)
def generate_osm_element (element):
if element['type'] == "node":
osm_element = ET.Element("node", lat=str(element['lat']), lon=str(element['lon']))
elif element['type'] == "way":
osm_element = ET.Element("way")
if "nodes" in element:
for node_ref in element['nodes']:
osm_element.append(ET.Element("nd", ref=str(node_ref)))
elif element['type'] == "relation":
osm_element = ET.Element("relation")
if "members" in element:
for member in element['members']:
osm_element.append(ET.Element("member", type=member['type'], ref=str(member['ref']), role=member['role']))
if "tags" in element:
for key, value in iter(element['tags'].items()):
osm_element.append(ET.Element("tag", k=key, v=value))
osm_element.set('id', str(element['id']))
# osm_element.set('visible', 'true')
if "user" in element: # Existing element
osm_element.set('version', str(element['version']))
osm_element.set('user', element['user'])
osm_element.set('uid', str(element['uid']))
osm_element.set('timestamp', element['timestamp'])
osm_element.set('changeset', str(element['changeset']))
if "action" in element and element['action'] in ["create", "modify"]:
osm_element.set('action', 'modify')
osm_root.append(osm_element)
if upload and "action" in element:
action_element = ET.Element(element['action'])
action_element.append(osm_element)
upload_root.append(action_element)
# Output stops
# Parameters:
# - action: new/modify/delete/user edit/other stop/nsr reference
# - stop_type: station/quay
# - nsr_ref: Unique station or quay reference in NSR, or None
# - osm_stop: Dict of OSM element from Overpass, or None
# - nsr_stop: Dict of NSR station or quay generated from Entur input file, or None
# - distance: Computed distance between coordinates for stop in NSR vs OSM, or None
def produce_stop (action, stop_type, nsr_ref, osm_stop, nsr_stop, distance):
global node_id, osm_data # osm_way_nodes, osm_relation_members
log ("\n\n%s: %s #%s\n" % (action.upper(), stop_type, nsr_ref))
if distance > 0:
log (" Moved %.1f meters\n" % distance)
# Create new stop or reference stops
# Reference stops are not for uploading to OSM; included to show NSR coordinate and content if user has edited stop in OSM
if action in ["new", "nsr reference"]:
node_id -= 1
entry = {
'id': node_id,
'lat': nsr_stop['lat'],
'lon': nsr_stop['lon'],
'type': "node",
'tags': {}
}
if stop_type == "station":
entry['tags']['ref:nsrs'] = nsr_ref
elif stop_type == "quay":
entry['tags']['ref:nsrq'] = nsr_ref
for key in ["name", "official_name", "ref", "unsigned_ref"]:
if key in nsr_stop:
entry['tags'][key] = nsr_stop[key]
for key in ["municipality", "submode", "nsrnote", "stoptype", "version"]:
if key in nsr_stop:
entry['tags'][key.upper()] = nsr_stop[key]
if action == "new": # Do not include main tags for reference elements
entry['action'] = "create"
if stop_type == "station":
entry['tags']['amenity'] = "bus_station"
elif stop_type == "quay":
entry['tags']['highway'] = "bus_stop"
else:
entry['tags']['NSR_REFERENCE'] = "yes" # Mark element as reference only (not for update)
osm_data['elements'].append(entry)
log (json.dumps(nsr_stop, indent=2, ensure_ascii=False))
log ("\n")
# Modify stop
elif action in ["modify", "relocate"]:
# Detach stop if it is a node in a way (the extra node is not counted)
osm_stop['action'] = "modify"
if osm_stop['id'] in osm_way_nodes:
entry = copy.deepcopy(osm_stop)
del entry['tags']
osm_data['elements'].append(entry)
# stops_new += 1
node_id -= 1
osm_stop['id'] = node_id
osm_stop['action'] = "create"
log (" Detach stop node from way\n")
# Modify tags
if osm_stop['type'] == "node" and action == "relocate" or osm_stop['action'] == "create":
osm_stop['lat'] = nsr_stop['lat']
osm_stop['lon'] = nsr_stop['lon']
if distance > 0:
osm_stop['tags']['DISTANCE'] = "%.1f" % distance
if stop_type == "station":
osm_stop['tags']['amenity'] = "bus_station"
osm_stop['tags']['ref:nsrs'] = nsr_ref
if "highway" in osm_stop['tags'] and osm_stop['tags']['highway'] == "bus_stop":
del osm_stop['tags']['highway']
log (" Change tagging from 'highway = bus_stop' to 'amenity = bus_station'\n")
check_keys = ["name", "route_ref"]
if ptv1_modify and osm_stop['id'] not in osm_ptv2_members:
check_keys += ['public_transport', 'bus']
elif stop_type == "quay":
osm_stop['tags']['highway'] = "bus_stop"
osm_stop['tags']['ref:nsrq'] = nsr_ref
if "amenity" in osm_stop['tags'] and osm_stop['tags']['amenity'] == "bus_station":
del osm_stop['tags']['amenity']
log (" Change tagging from 'amenity = bus_station' to 'highway = bus_stop'\n")
check_keys = ["name", "official_name", "ref", "unsigned_ref", "route_ref"]
if ptv1_modify and osm_stop['id'] not in osm_ptv2_members:
check_keys += ['public_transport', 'bus']
for key in check_keys:
if key in nsr_stop:
if key in osm_stop['tags']:
if osm_stop['tags'][key] != nsr_stop[key]:
log (" Change tag '%s' from '%s' to '%s'\n" % (key, osm_stop['tags'][key], nsr_stop[key]))
else:
log (" New tag '%s = %s'\n" % (key, nsr_stop[key]))
osm_stop['tags'][key] = nsr_stop[key]
elif key in osm_stop['tags']:
log (" Delete tag '%s = %s'\n" % (key, osm_stop['tags'][key]))
del osm_stop['tags'][key]
# Mark stop as deleted (for manual deletion in JOSM)
elif action == "delete":
osm_stop['tags']['DELETE'] = "yes"
log (json.dumps(osm_stop, indent=2, ensure_ascii=False))
log ("\n")
# Keep element if element belongs to or is itself a way or relation
if (upload and ((osm_stop['id'] in osm_way_nodes)
or (osm_stop['id'] in osm_relation_members)
or ("nodes" in osm_stop) or ("members" in osm_stop))):
osm_stop['tags'] = {}
if osm_stop['id'] in osm_ptv2_members:
osm_stop['tags']['fixme'] = "Relocate bus stop? (bus stop not used in NSR routes)"
log (" Check %s #%s (deleted stop/station had way or relation dependencies)\n" % (osm_stop['type'], osm_stop['id']))
message (" *** Check %s #%s (deleted stop/station had way or relation dependencies)\n" % (osm_stop['type'], osm_stop['id']))
osm_stop['action'] = "modify"
else:
osm_stop['action'] = "delete"
# Mark stop as edited by user (for information only)
elif action == "user edit":
osm_stop['tags']['EDIT'] = osm_stop['timestamp'][0:10]
osm_stop['tags']['USER'] = osm_stop['user']
if distance > 0:
osm_stop['tags']['DISTANCE'] = "%.1f" % distance # Include distance from NSR coodinate if different
osm_name = ""
nsr_name = ""
if "name" in nsr_stop:
nsr_name = nsr_stop['name']
if "name" in osm_stop['tags']:
osm_name = osm_stop['tags']['name']
if osm_name != nsr_name:
log (" User tagged 'name' as '%s'; in NSR '%s'\n" % (osm_name, nsr_name))
if nsr_name:
osm_stop['tags']['NSR_NAME'] = nsr_name # Include NSR name if different
log (json.dumps(osm_stop, indent=2, ensure_ascii=False))
log ("\n")
# Include bus_stop or bus_station which is not present in NSR (without ref:nsrs/nsrq tags)
elif action == "other stop":
osm_stop['tags']['OTHER'] = osm_stop['timestamp'][0:10]
osm_stop['tags']['USER'] = osm_stop['user']
log (json.dumps(osm_stop, indent=2, ensure_ascii=False))
log ("\n")
# Extra information about when stops were last used by any route
if (stop_type == "quay" and nsr_ref is not None and osm_stop is not None
and nsr_ref not in route_quays and nsr_ref in history['quays'] and "date" in history['quays'][ nsr_ref ]):
osm_stop['tags']['LAST_USED'] = history['quays'][ nsr_ref ]['date']
# Read stops from OSM for county, match with NSR and output result
# Paramters:
# - county_id: Two digit county reference
# - county_name: Full name of county
def process_county (county_id, county_name):
# Check if tags in NSR and OSM are differnt
def different_tags(nsr_stop, osm_tags, check_tags):
for tag in check_tags:
osm_tag = ""
nsr_tag = ""
if tag in nsr_stop:
nsr_tag = nsr_stop[ tag ]
if tag in osm_tags:
osm_tag = osm_tags[ tag ]
if osm_tag != nsr_tag:
return True
return False
global stops_total_modify, stops_total_delete, stops_total_edits, stops_total_others, stops_new
global osm_data
message ("\nLoading #%s %s county... " % (county_id, county_name))
log ("\n\n*** COUNTY: %s %s\n" % (county_id, county_name))
# Load stops from Overpass, plus any parent ways/relations and children
query = ('[out:json][timeout:90];'
'(area["name"="%s"][admin_level=4];)->.a;'
'('
'nwr["amenity"="bus_station"](area.a);'
'nwr["highway"="bus_stop"](area.a);'
')->.b;'
'(.b; .b >; .b <;);'
'out center meta;' % county_name)
osm_data = {'elements': []}
while not osm_data['elements']: # May deliver empty result
request = urllib.request.Request(overpass_api + "?data=" + urllib.parse.quote(query), headers=request_header)
file = open_url(request)
osm_data = json.load(file)
file.close()
# Make lists of all stop nodes witch are part of ways and relations
osm_way_nodes.clear()
osm_relation_members.clear()
osm_ptv2_members.clear()
count_relations = 0
for element in osm_data['elements']:
if "nodes" in element:
for node in element['nodes']:
osm_way_nodes.append(node)
if "members" in element:
count_relations += 1
for member in element['members']:
osm_relation_members.append(member['ref'])
if "tags" in element:
if "public_transport" in element['tags'] or "type" in element['tags'] and element['tags']['type'] == "route":
osm_ptv2_members.append(member['ref'])
message ("Connected to %i nodes, %i relations\n" % (len(osm_way_nodes), count_relations))
# Iterate stops from OSM and discover differences between NSR and OSM
# The dict osm_data will be modified to include all stops to be output
# When done, only NSR stops which did not get a match remain in NSR stations/quays dicts
stops_nsr = 0
stops_osm = 0
stops_modify = 0
stops_delete = 0
stops_new = 0
stops_edit = 0
stops_other = 0
stops_history = 0
index = 0
the_end = False
end_element = osm_data['elements'][-1] # Iteration will end at this stop
osm_stop = None
while not(the_end):
osm_stop = osm_data['elements'][index]
index += 1
if osm_stop == end_element:
the_end = True
if "tags" in osm_stop:
tags = osm_stop['tags']
# Stations
if "ref:nsrs" in tags:
stops_osm += 1
nsr_ref = tags['ref:nsrs']
if nsr_ref in stations:
stops_nsr += 1
station = stations[nsr_ref]
relocate = False
nsr_relocate = False # Will become True if location in NSR changed since last check
tag_modify = False
extra_distance = 0
# Check location
if "center" in osm_stop:
distance = compute_distance((osm_stop['center']['lon'], osm_stop['center']['lat']), (station['lon'], station['lat']))
extra_distance = 100
else:
distance = compute_distance((osm_stop['lon'], osm_stop['lat']), (station['lon'], station['lat']))
if distance >= max_distance + extra_distance:
relocate = True
if (nsr_ref in history['stations'] and "point" in history['stations'][ nsr_ref ]
and history['stations'][ nsr_ref ]['point'] != [station['lon'], station['lat']]):
nsr_relocate = True
stops_history += 1
# Check tags
check_tags = ["name"]
if ptv1 and osm_stop['id'] not in osm_ptv2_members:
check_tags += ["public_transport", "bus"]
if different_tags(station, tags, check_tags):
tag_modify = True
# Modify if name difference or if relocated in NSR or if last edit is by user in whitelist, else include for information
if relocate or tag_modify:
if nsr_relocate or relocate and (osm_stop['user'] in user_whitelist or tag_modify):
produce_stop ("relocate", "station", nsr_ref, osm_stop, station, distance)
stops_modify += 1
elif tag_modify:
produce_stop ("modify", "station", nsr_ref, osm_stop, station, distance)
stops_modify += 1
else:
produce_stop ("user edit", "station", nsr_ref, osm_stop, station, distance)
if distance > 0:
produce_stop ("nsr reference", "station", nsr_ref, None, station, 0) # Output NSR stop for reference only
stops_edit += 1
del stations[ nsr_ref ]
else:
produce_stop ("delete", "station", nsr_ref, osm_stop, None, 0)
stops_delete += 1
# Quays
elif "ref:nsrq" in tags:
stops_osm += 1
nsr_ref = tags['ref:nsrq']
if nsr_ref in quays:
stops_nsr += 1
quay = quays[nsr_ref]
relocate = False
nsr_relocate = False
tag_modify = False
extra_distance = 0
# Check location
if "center" in osm_stop:
distance = compute_distance((osm_stop['center']['lon'], osm_stop['center']['lat']), (station['lon'], station['lat']))
extra_distance = 10
else:
distance = compute_distance((osm_stop['lon'], osm_stop['lat']), (quay['lon'], quay['lat']))
if distance >= max_distance + extra_distance:
relocate = True
if (nsr_ref in history['quays'] and "point" in history['quays'][ nsr_ref ]
and history['quays'][ nsr_ref ]['point'] != [quay['lon'], quay['lat']]):
nsr_relocate = True
stops_history += 1
# Check tags
check_tags = ["name", "official_name", "ref", "unsigned_ref", "route_ref"]
if ptv1 and osm_stop['id'] not in osm_ptv2_members:
check_tags += ["public_transport", "bus"]
if different_tags(quay, tags, check_tags):
tag_modify = True
# Modify if tag difference or if relocated in NSR or if last edit is by user in whitelist, else include for information
if relocate or tag_modify:
if nsr_relocate or relocate and (osm_stop['user'] in user_whitelist or tag_modify):
produce_stop ("relocate", "quay", nsr_ref, osm_stop, quay, distance)
stops_modify += 1
elif tag_modify:
produce_stop ("modify", "quay", nsr_ref, osm_stop, quay, distance)
stops_modify += 1
else:
produce_stop ("user edit", "quay", nsr_ref, osm_stop, quay, distance)
if distance > 0:
produce_stop ("nsr reference", "quay", nsr_ref, None, quay, 0) # Output NSR stop for reference only
stops_edit += 1
del quays[ nsr_ref ]
else:
produce_stop ("delete", "quay", nsr_ref, osm_stop, None, 0)
stops_delete += 1
# Include other stops witch do not have NSR ref tags
else:
if "highway" in tags and tags['highway'] == "bus_stop" or "amenity" in tags and tags['amenity'] == "bus_station":
produce_stop ("other stop", None, None, osm_stop, None, 0)
stops_other += 1
# Count NSR stations and quays which were not found in OSM. Output later
for nsr_ref, station in iter(stations.items()):
if station['municipality'][0:2] == county_id:
# produce_stop ("new", "station", nsr_ref, None, station, 0)
stops_new += 1
stops_nsr += 1
for nsr_ref, quay in iter(quays.items()):
if quay['municipality'][0:2] == county_id and nsr_ref not in quays_abroad: # Omit quays outside of Norway border
# produce_stop ("new", "quay", nsr_ref, None, quay, 0)
stops_new += 1
stops_nsr += 1
# Display summary information
message ("\n")
message (" Stops in OSM : %i\n" % stops_osm)
message (" Stops in NSR : %i\n" % stops_nsr)
message (" User edited stops : %i\n" % stops_edit)
message (" Other non-NSR stops : %i\n" % stops_other)
message (" Modified stops : %i\n" % stops_modify)
message (" Deleted stops : %i\n" % stops_delete)
message (" New stops (preliminary): %i\n" % stops_new) # Preliminary count; conclusion later
message (" Stops relocated in NSR : %i\n" % stops_history)
stops_total_modify += stops_modify
stops_total_delete += stops_delete
stops_total_edits += stops_edit
stops_total_others += stops_other
# Produce output to file, including parent and child elements
for element in osm_data['elements']:
generate_osm_element (element)
# Output remaining NSR stations and quays which were not found in OSM
def process_new_stops():
global stops_total_new, osm_data
log ("\n\n*** NEW STOPS: Norway\n")
osm_data = { 'elements': [] }
for nsr_ref, station in iter(stations.items()):
if station['municipality'][0:2] not in exclude_counties:
produce_stop ("new", "station", nsr_ref, None, station, 0)
stops_total_new += 1
for nsr_ref, quay in iter(quays.items()):
if quay['municipality'][0:2] not in exclude_counties and nsr_ref not in quays_abroad: # Omit quays outside of Norway border
produce_stop ("new", "quay", nsr_ref, None, quay, 0)
stops_total_new += 1
message ("\n\nNew stops in Norway: %i\n" % stops_total_new)
for element in osm_data['elements']:
generate_osm_element (element)
# Load NSR routes to discover which bus stops are being used.
# The set route_quays will contain all quays which are used by one or more routes.
def load_nsr_routes():
url = "https://storage.googleapis.com/marduk-production/outbound/gtfs/rb_norway-aggregated-gtfs-basic.zip"
in_file = urllib.request.urlopen(url)
zip_file = zipfile.ZipFile(BytesIO(in_file.read()))
# Load routes to discover quays in use from time table data
file = zip_file.open("stop_times.txt")
file_csv = csv.DictReader(TextIOWrapper(file, "utf-8"), fieldnames=['trip_id','stop_id'], delimiter=",")
next(file_csv)
for row in file_csv:
quay_id = row['stop_id'][9:]
route_quays.add(quay_id)
file.close()
in_file.close()
# Load date of last route assignment for all quays
def load_history():
global history
file_path = os.path.expanduser(history_filename)
if os.path.isfile(file_path):
file = open(file_path)
history = json.load(file)
file.close()
message ("Loaded quay history\n")
else:
message ("Quay history '%s' not found\n" % file_path)
# Save date of last route assignment for all quays + all locations.
# Part 1 of this function needs to be executed before merging with OSM (to get all stations/quays).
# When merging, the old history dict will be used.
def save_history(save_file):
global new_history
if not save_file:
# Part 1: Update all stations and quays with the current NSR location + today's date for quays with an active route
new_history = copy.deepcopy(history)
for ref, station in iter(stations.items()):
if ref not in history['stations']:
new_history['stations'][ ref ] = {}
new_history['stations'][ ref ]['point'] = ( station['lon'], station['lat'] )
for ref, quay in iter(quays.items()):
if ref not in new_history['quays']:
new_history['quays'][ ref ] = {}
new_history['quays'][ ref ]['point'] = ( quay['lon'], quay['lat'] )
for ref in route_quays:
if ref not in new_history['quays']:
new_history['quays'][ ref ] = {}
new_history['quays'][ ref ]['date'] = today
else:
# Part 2: Save history file
file_path = os.path.expanduser(history_filename)
file = open(file_path, "w")
json.dump(new_history, file, indent=1)
file.close()
message ("Saved quay history to '%s'\n" % history_filename)
# Read all NSR data into memory from Entur NeTEx file and convert to OSM tags
# The stations and quay dicts will contain all bus stations and bus stops, respectively
def load_nsr_data():
url = "https://storage.googleapis.com/marduk-production/tiamat/Current_latest.zip"
in_file = urllib.request.urlopen(url)
zip_file = zipfile.ZipFile(BytesIO(in_file.read()))
filename = zip_file.namelist()[0]
file = zip_file.open(filename)
tree = ET.parse(file)
file.close()
root = tree.getroot()
ns_url = 'http://www.netex.org.uk/netex'
ns = {'ns0': ns_url} # Namespace
stop_data = root.find("ns0:dataObjects/ns0:SiteFrame/ns0:stopPlaces", ns)
station_count = 0
quay_count = 0
keep_one_year_count = 0
exclude_one_year_count = 0
# Iterate all stops
for stop_place in stop_data.iter('{%s}StopPlace' % ns_url):
stop_type = stop_place.find('ns0:StopPlaceType', ns)
if stop_type != None:
stop_type = stop_type.text
else:
stop_type = ""
municipality = stop_place.find('ns0:TopographicPlaceRef', ns)
if municipality != None:
municipality = municipality.get('ref')
if municipality[0:3] == "KVE":
municipality = municipality.replace("KVE:TopographicPlace:", "")
else:
municipality = ""
else:
municipality = ""
# Only keep bus stops in Norway
if stop_type in ["busStation", "onstreetBus"] and municipality:
name = stop_place.find('ns0:Name', ns)
if name != None:
name = name.text
else:
name = ""
name = name.replace(" ", " ").strip()
transport_mode = stop_place.find('ns0:TransportMode', ns)
if transport_mode != None:
transport_mode = transport_mode.text
else:
transport_mode = ""
if transport_mode:
transport_submode = stop_place.find('ns0:%sSubmode' % transport_mode.title(), ns)
if transport_submode != None:
transport_submode = transport_submode.text
else:
transport_submode = ""
else:
transport_submode = ""
# Only keep stops which are not temporary
if transport_submode != "railReplacementBus":
# Get comments in NSR if any (for information to mapper only)
note = ""
key_list = stop_place.find('ns0:keyList', ns)
if key_list != None:
for key in key_list.iter('{%s}KeyValue' % ns_url):
key_name = key.find('ns0:Key', ns).text
key_value = key.find('ns0:Value', ns).text
if key_name:
if key_name.find("name") > 0:
note += ";[" + key_value + "]"
elif key_name.find("comment") > 0:
if key_value:
note += " " + key_value.replace("<", "<")
note = note.lstrip(";")
# Get bus station
if stop_type == "busStation":
station_count += 1
location = stop_place.find('ns0:Centroid/ns0:Location', ns)
longitude = float(location.find('ns0:Longitude', ns).text)
latitude = float(location.find('ns0:Latitude', ns).text)
nsr_ref = stop_place.get('id').replace("NSR:StopPlace:", "")
entry = {
'name': name,
'lon': longitude,
'lat': latitude,
'municipality': municipality,
'version': stop_place.get('version'),
}
if transport_submode:
entry['submode'] = transport_submode
if note:
entry['nsrnote'] = note
stations[ nsr_ref ] = entry
# Avoid single quays for bus stations
quay_data = stop_place.find('ns0:quays', ns)
if quay_data != None and stop_type == "busStation":
count = 0
for quay in quay_data.iter('{%s}Quay' % ns_url):
count += 1
if count == 1:
quay_data = None
# Get quay nodes
if quay_data != None:
for quay in quay_data.iter('{%s}Quay' % ns_url):
quay_count -= 1
location = quay.find('ns0:Centroid/ns0:Location', ns)
longitude = float(location.find('ns0:Longitude', ns).text)
latitude = float(location.find('ns0:Latitude', ns).text)
public_code = quay.find('ns0:PublicCode', ns)
if public_code != None:
ref = public_code.text
else:
ref = ""
# Use quay reference for bus stations + add station name in official_name
# Else use stop name if not station
# Add public reference number/letter, if any, in parenteces in name (it is displayed on the quay)
entry = {
'lon': longitude,
'lat': latitude,
'municipality': municipality,
'stoptype': stop_type,
'version': quay.get('version')
}
if stop_type == "busStation":
if ref:
entry['name'] = ref
entry['official_name'] = name + " (" + ref + ")"
entry['ref'] = ref
else:
entry['official_name'] = name
private_code = quay.find('ns0:PrivateCode', ns)
if private_code != None:
ref = private_code.text
if ref:
entry['unsigned_ref'] = ref
else:
if ref:
entry['name'] = name + " (" + ref + ")"
entry['ref'] = ref
else:
entry['name'] = name
if transport_submode:
entry['submode'] = transport_submode
if note:
entry['nsrnote'] = note
nsr_ref = quay.get('id').replace("NSR:Quay:", "")
# Omit quays which have not had a route last year, unless they belong to a bus station
if (stop_type == "busStation"
or nsr_ref in route_quays
or nsr_ref in history['quays']
and "date" in history['quays'][ nsr_ref ]
and (datetime.date.today() - datetime.date.fromisoformat(history['quays'][ nsr_ref ]['date'])).days < 365):
quays[ nsr_ref ] = entry
if (stop_type != "busStation"
and nsr_ref not in route_quays
and nsr_ref in history['quays']
and "date" in history['quays'][ nsr_ref ]):
if (datetime.date.today() - datetime.date.fromisoformat(history['quays'][ nsr_ref ]['date'])).days >= 365:
# message ("\tExcluded quay %s\n" % nsr_ref)
exclude_one_year_count += 1
else:
keep_one_year_count += 1
message ("%i kept up to one year, %i excluded after one year\n" % (keep_one_year_count, exclude_one_year_count))
# Upload changeset to OSM
def upload_changeset():
if upload and stops_total_changes > 0:
if stops_total_changes < 9900: # Maximum upload is 10.000 elements
today_date = time.strftime("%Y-%m-%d", time.localtime())
changeset_root = ET.Element("osm")
changeset_element = ET.Element("changeset")
changeset_element.append(ET.Element("tag", k="comment", v="Bus stop import update for Norway"))
changeset_element.append(ET.Element("tag", k="source", v="Entur: Norsk Stoppestedsregister (NSR)"))
changeset_element.append(ET.Element("tag", k="source:date", v=today_date))
changeset_root.append(changeset_element)
changeset_xml = ET.tostring(changeset_root, encoding='utf-8', method='xml')
request = urllib.request.Request(osm_api + "changeset/create", data=changeset_xml, headers=osm_request_header, method="PUT")
file = open_url(request) # Create changeset
changeset_id = file.read().decode()
file.close()
message ("\nUploading %i elements to OSM in changeset #%s..." % (stops_total_changes, changeset_id))
for element in upload_root:
element[0].set("changeset", changeset_id)
for tag in element[0].findall("tag"):
if tag.attrib['k'] in manual_keys: # Remove import keys
element[0].remove(tag)
indent_tree(upload_root)
changeset_xml = ET.tostring(upload_root, encoding='utf-8', method='xml')
request = urllib.request.Request(osm_api + "changeset/%s/upload" % changeset_id, data=changeset_xml, headers=osm_request_header)
file = open_url(request) # Post changeset in one go
file.close()
request = urllib.request.Request(osm_api + "changeset/%s/close" % changeset_id, headers=osm_request_header, method="PUT")
file = open_url(request) # Close changeset
file.close()
if debug:
file_out = open("nsr_changeset.xml", "w")
file_out.write(changeset_xml.decode())
file_out.close()
message ("\nDone\n\n")
else:
message ("\n\nCHANGESET TOO LARGE (%i) - UPLOAD MANUALLY WITH JOSM\n\n" % stops_total_changes)
# Insert line feeds into XLM file.
def indent_tree(elem, level=0):