-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcycling_quality_index.py
1657 lines (1491 loc) · 104 KB
/
cycling_quality_index.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
#---------------------------------------------------------------------------#
# Cycling Quality Index #
# -------------------------------------------------- #
# Script for processing OSM data to analyse the cycling quality of ways. #
# Download OSM data input from https://overpass-turbo.eu/s/1IDp, #
# save it at data/way_import.geojson and run the script. #
# #
# > version/date: 2024-04-15 #
#---------------------------------------------------------------------------#
import os, sys, processing, math, time, importlib
from os.path import exists
#project directory
from console.console import _console
project_dir = os.path.dirname(_console.console.tabEditorWidget.currentWidget().path) + '/'
dir_input = project_dir + 'data/way_import'
dir_output = project_dir + 'data/cycling_quality_index'
file_format = '.geojson'
multi_input = False #if "True", it's possible to merge different import files stored in the input directory, marked with an ascending number starting with 1 at the end of the filename (e.g. way_import1.geojson, way_import2.geojson etc.) - can be used to process different areas at the same time or to process a larger area that can't be downloaded in one file
if project_dir not in sys.path:
sys.path.append(project_dir)
import parameter as p
importlib.reload(p)
import definitions as d
importlib.reload(d)
#--------------------------------
# S c r i p t S t a r t
#--------------------------------
print(time.strftime('%H:%M:%S', time.localtime()), 'Start processing:')
print(time.strftime('%H:%M:%S', time.localtime()), 'Read data...')
#multiple input files can be merged to one single input
if multi_input:
input_data = []
i = 1
while exists(dir_input + str(i) + file_format):
print(time.strftime('%H:%M:%S', time.localtime()), ' Read input file ' + str(i) + '...')
layer_way_input = QgsVectorLayer(dir_input + str(i) + file_format + '|geometrytype=LineString', 'way input', 'ogr')
layer_way_input = processing.run('native:retainfields', { 'INPUT' : layer_way_input, 'FIELDS' : p.attributes_list, 'OUTPUT': 'memory:'})['OUTPUT']
input_data.append(layer_way_input)
i += 1
if input_data:
print(time.strftime('%H:%M:%S', time.localtime()), ' Merge input files...')
layer_way_input = processing.run('native:mergevectorlayers', { 'LAYERS' : input_data, 'OUTPUT': 'memory:'})['OUTPUT']
layer_way_input = processing.run('native:deleteduplicategeometries', {'INPUT': layer_way_input, 'OUTPUT': dir_input + file_format })
else:
print(time.strftime('%H:%M:%S', time.localtime()), '[!] Warning: No valid input files at "' + dir_input + '*' + file_format + '". Use ascending numbers starting with 1 at the end of the file names.')
if exists(dir_input + file_format):
print(time.strftime('%H:%M:%S', time.localtime()), '[!] Warning: Continuing with input file "' + dir_input + file_format + '".')
if not exists(dir_input + file_format):
if multi_input:
print(time.strftime('%H:%M:%S', time.localtime()), '[!] Error: No valid input files at "' + dir_input + '*' + file_format + '".')
else:
print(time.strftime('%H:%M:%S', time.localtime()), '[!] Error: No valid input file at "' + dir_input + file_format + '".')
else:
layer_way_input = QgsVectorLayer(dir_input + file_format + '|geometrytype=LineString', 'way input', 'ogr')
print(time.strftime('%H:%M:%S', time.localtime()), 'Reproject data...')
layer = processing.run('native:reprojectlayer', { 'INPUT' : layer_way_input, 'TARGET_CRS' : QgsCoordinateReferenceSystem(p.crs_metric), 'OUTPUT': 'memory:'})['OUTPUT']
#prepare attributes
print(time.strftime('%H:%M:%S', time.localtime()), 'Prepare data...')
#delete unneeded attributes
layer = processing.run('native:retainfields', { 'INPUT' : layer, 'FIELDS' : p.attributes_list, 'OUTPUT': 'memory:'})['OUTPUT']
#list of new attributes, important for calculating cycling quality index
new_attributes_dict = {
'way_type': 'String',
'index': 'Int',
'index_10': 'Int',
'stress_level': 'Int',
'offset': 'Double',
'offset_cycleway_left': 'Double',
'offset_cycleway_right': 'Double',
'offset_sidewalk_left': 'Double',
'offset_sidewalk_right': 'Double',
'type': 'String',
'side': 'String',
'proc_width': 'Double',
'proc_surface': 'String',
'proc_smoothness': 'String',
'proc_oneway': 'String',
'proc_sidepath': 'String',
'proc_highway': 'String',
'proc_maxspeed': 'Int',
'proc_traffic_mode_left': 'String',
'proc_traffic_mode_right': 'String',
'proc_separation_left': 'String',
'proc_separation_right': 'String',
'proc_buffer_left': 'Double',
'proc_buffer_right': 'Double',
'proc_mandatory': 'String',
'proc_traffic_sign': 'String',
'fac_width': 'Double',
'fac_surface': 'Double',
'fac_highway': 'Double',
'fac_maxspeed': 'Double',
'fac_protection_level': 'Double',
'prot_level_separation_left': 'Double',
'prot_level_separation_right': 'Double',
'prot_level_buffer_left': 'Double',
'prot_level_buffer_right': 'Double',
'prot_level_left': 'Double',
'prot_level_right': 'Double',
'base_index': 'Int',
'fac_1': 'Double',
'fac_2': 'Double',
'fac_3': 'Double',
'fac_4': 'Double',
'data_bonus': 'String',
'data_malus': 'String',
'data_incompleteness': 'Double',
'data_missing': 'String',
'data_missing_width': 'Int',
'data_missing_surface': 'Int',
'data_missing_smoothness': 'Int',
'data_missing_maxspeed': 'Int',
'data_missing_parking': 'Int',
'data_missing_lit': 'Int',
'filter_usable': 'Int',
'filter_way_type': 'String'
}
for attr in list(new_attributes_dict.keys()):
p.attributes_list.append(attr)
#make sure all attributes are existing in the table to prevent errors when asking for a missing one
with edit(layer):
for attr in p.attributes_list:
if layer.fields().indexOf(attr) == -1:
if attr in new_attributes_dict:
if new_attributes_dict[attr] == 'Double':
layer.dataProvider().addAttributes([QgsField(attr, QVariant.Double)])
elif new_attributes_dict[attr] == 'Int':
layer.dataProvider().addAttributes([QgsField(attr, QVariant.Int)])
else:
layer.dataProvider().addAttributes([QgsField(attr, QVariant.String)])
else:
layer.dataProvider().addAttributes([QgsField(attr, QVariant.String)])
layer.updateFields()
id_way_type = layer.fields().indexOf('way_type')
id_index = layer.fields().indexOf('index')
id_index_10 = layer.fields().indexOf('index_10')
id_stress_level = layer.fields().indexOf('stress_level')
id_offset = layer.fields().indexOf('offset')
id_offset_cycleway_left = layer.fields().indexOf('offset_cycleway_left')
id_offset_cycleway_right = layer.fields().indexOf('offset_cycleway_right')
id_offset_sidewalk_left = layer.fields().indexOf('offset_sidewalk_left')
id_offset_sidewalk_right = layer.fields().indexOf('offset_sidewalk_right')
id_type = layer.fields().indexOf('type')
id_side = layer.fields().indexOf('side')
id_proc_width = layer.fields().indexOf('proc_width')
id_proc_surface = layer.fields().indexOf('proc_surface')
id_proc_smoothness = layer.fields().indexOf('proc_smoothness')
id_proc_oneway = layer.fields().indexOf('proc_oneway')
id_proc_sidepath = layer.fields().indexOf('proc_sidepath')
id_proc_highway = layer.fields().indexOf('proc_highway')
id_proc_maxspeed = layer.fields().indexOf('proc_maxspeed')
id_proc_traffic_mode_left = layer.fields().indexOf('proc_traffic_mode_left')
id_proc_traffic_mode_right = layer.fields().indexOf('proc_traffic_mode_right')
id_proc_separation_left = layer.fields().indexOf('proc_separation_left')
id_proc_separation_right = layer.fields().indexOf('proc_separation_right')
id_proc_buffer_left = layer.fields().indexOf('proc_buffer_left')
id_proc_buffer_right = layer.fields().indexOf('proc_buffer_right')
id_proc_mandatory = layer.fields().indexOf('proc_mandatory')
id_proc_traffic_sign = layer.fields().indexOf('proc_traffic_sign')
id_fac_width = layer.fields().indexOf('fac_width')
id_fac_surface = layer.fields().indexOf('fac_surface')
id_fac_highway = layer.fields().indexOf('fac_highway')
id_fac_maxspeed = layer.fields().indexOf('fac_maxspeed')
id_fac_protection_level = layer.fields().indexOf('fac_protection_level')
id_prot_level_separation_left = layer.fields().indexOf('prot_level_separation_left')
id_prot_level_separation_right = layer.fields().indexOf('prot_level_separation_right')
id_prot_level_buffer_left = layer.fields().indexOf('prot_level_buffer_left')
id_prot_level_buffer_right = layer.fields().indexOf('prot_level_buffer_right')
id_prot_level_left = layer.fields().indexOf('prot_level_left')
id_prot_level_right = layer.fields().indexOf('prot_level_right')
id_base_index = layer.fields().indexOf('base_index')
id_fac_1 = layer.fields().indexOf('fac_1')
id_fac_2 = layer.fields().indexOf('fac_2')
id_fac_3 = layer.fields().indexOf('fac_3')
id_fac_4 = layer.fields().indexOf('fac_4')
id_data_bonus = layer.fields().indexOf('data_bonus')
id_data_malus = layer.fields().indexOf('data_malus')
id_data_incompleteness = layer.fields().indexOf('data_incompleteness')
id_data_missing = layer.fields().indexOf('data_missing')
id_data_missing_width = layer.fields().indexOf('data_missing_width')
id_data_missing_surface = layer.fields().indexOf('data_missing_surface')
id_data_missing_smoothness = layer.fields().indexOf('data_missing_smoothness')
id_data_missing_maxspeed = layer.fields().indexOf('data_missing_maxspeed')
id_data_missing_parking = layer.fields().indexOf('data_missing_parking')
id_data_missing_lit = layer.fields().indexOf('data_missing_lit')
id_filter_usable = layer.fields().indexOf('filter_usable')
id_filter_way_type = layer.fields().indexOf('filter_way_type')
QgsProject.instance().addMapLayer(layer, False)
#---------------------------------------------------------------#
#1: Check paths whether they are sidepath (a path along a road) #
#---------------------------------------------------------------#
print(time.strftime('%H:%M:%S', time.localtime()), 'Sidepath check...')
print(time.strftime('%H:%M:%S', time.localtime()), ' Create way layers...')
#create path layer: check all path, footways or cycleways for their sidepath status
layer_path = processing.run('qgis:extractbyexpression', { 'INPUT' : layer, 'EXPRESSION' : '"highway" IS \'cycleway\' OR "highway" IS \'footway\' OR "highway" IS \'path\' OR "highway" IS \'bridleway\' OR "highway" IS \'steps\'', 'OUTPUT': 'memory:'})['OUTPUT']
#create road layer: extract all other highway types (except tracks)
layer_roads = processing.run('qgis:extractbyexpression', { 'INPUT' : layer, 'EXPRESSION' : '"highway" IS NOT \'cycleway\' AND "highway" IS NOT \'footway\' AND "highway" IS NOT \'path\' AND "highway" IS NOT \'bridleway\' AND "highway" IS NOT \'steps\' AND "highway" IS NOT \'track\'', 'OUTPUT': 'memory:'})['OUTPUT']
print(time.strftime('%H:%M:%S', time.localtime()), ' Create check points...')
#create "check points" along each segment (to check for near/parallel highways at every checkpoint)
layer_path_points = processing.run('native:pointsalonglines', {'INPUT' : layer_path, 'DISTANCE' : p.sidepath_buffer_distance, 'OUTPUT': 'memory:'})['OUTPUT']
layer_path_points_endpoints = processing.run('native:extractspecificvertices', { 'INPUT' : layer_path, 'VERTICES' : '-1', 'OUTPUT': 'memory:'})['OUTPUT']
layer_path_points = processing.run('native:mergevectorlayers', { 'LAYERS' : [layer_path_points, layer_path_points_endpoints], 'OUTPUT': 'memory:'})['OUTPUT']
#create "check buffers" (to check for near/parallel highways with in the given distance)
layer_path_points_buffers = processing.run('native:buffer', { 'INPUT' : layer_path_points, 'DISTANCE' : p.sidepath_buffer_size, 'OUTPUT': 'memory:'})['OUTPUT']
QgsProject.instance().addMapLayer(layer_path_points_buffers, False)
print(time.strftime('%H:%M:%S', time.localtime()), ' Check for adjacent roads...')
#for all check points: Save nearby road id's, names and highway classes in a dict
sidepath_dict = {}
for buffer in layer_path_points_buffers.getFeatures():
buffer_id = buffer.attribute('id')
buffer_layer = buffer.attribute('layer')
if not buffer_id in sidepath_dict:
sidepath_dict[buffer_id] = {}
sidepath_dict[buffer_id]['checks'] = 1
sidepath_dict[buffer_id]['id'] = {}
sidepath_dict[buffer_id]['highway'] = {}
sidepath_dict[buffer_id]['name'] = {}
sidepath_dict[buffer_id]['maxspeed'] = {}
else:
sidepath_dict[buffer_id]['checks'] += 1
layer_path_points_buffers.removeSelection()
layer_path_points_buffers.select(buffer.id())
processing.run('native:selectbylocation', {'INPUT' : layer_roads, 'INTERSECT' : QgsProcessingFeatureSourceDefinition(layer_path_points_buffers.id(), selectedFeaturesOnly=True), 'METHOD' : 0, 'PREDICATE' : [0,6]})
id_list = []
highway_list = []
name_list = []
maxspeed_dict = {}
for road in layer_roads.selectedFeatures():
road_layer = road.attribute('layer')
if buffer_layer != road_layer:
continue #only consider geometries in the same layer
road_id = road.attribute('id')
road_highway = road.attribute('highway')
road_name = road.attribute('name')
road_maxspeed = d.getNumber(road.attribute('maxspeed'))
if not road_id in id_list:
id_list.append(road_id)
if not road_highway in highway_list:
highway_list.append(road_highway)
if not road_highway in maxspeed_dict or maxspeed_dict[road_highway] < road_maxspeed:
maxspeed_dict[road_highway] = road_maxspeed
if not road_name in name_list:
name_list.append(road_name)
for road_id in id_list:
if road_id in sidepath_dict[buffer_id]['id']:
sidepath_dict[buffer_id]['id'][road_id] += 1
else:
sidepath_dict[buffer_id]['id'][road_id] = 1
for road_highway in highway_list:
if road_highway in sidepath_dict[buffer_id]['highway']:
sidepath_dict[buffer_id]['highway'][road_highway] += 1
else:
sidepath_dict[buffer_id]['highway'][road_highway] = 1
for road_name in name_list:
if road_name in sidepath_dict[buffer_id]['name']:
sidepath_dict[buffer_id]['name'][road_name] += 1
else:
sidepath_dict[buffer_id]['name'][road_name] = 1
for highway in maxspeed_dict.keys():
if not highway in sidepath_dict[buffer_id]['maxspeed'] or sidepath_dict[buffer_id]['maxspeed'][highway] < maxspeed_dict[highway]:
sidepath_dict[buffer_id]['maxspeed'][highway] = maxspeed_dict[highway]
highway_class_list = ['motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'unclassified', 'residential', 'road', 'living_street', 'service', 'pedestrian', NULL]
#a path is considered a sidepath if at least two thirds of its check points are found to be close to road segments with the same OSM ID, highway class or street name
with edit(layer):
for feature in layer.getFeatures():
hw = feature.attribute('highway')
maxspeed = feature.attribute('maxspeed')
if maxspeed == 'walk' or (not maxspeed and hw == 'living_street'):
maxspeed = 10
if maxspeed == 'none':
maxspeed = 299
if not maxspeed and hw == 'living_street':
maxspeed = 10
if not hw in ['cycleway', 'footway', 'path', 'bridleway', 'steps']:
layer.changeAttributeValue(feature.id(), id_proc_highway, hw)
layer.changeAttributeValue(feature.id(), id_proc_maxspeed, d.getNumber(maxspeed))
continue
id = feature.attribute('id')
is_sidepath = feature.attribute('is_sidepath')
if feature.attribute('footway') == 'sidewalk':
is_sidepath = 'yes'
is_sidepath_of = feature.attribute('is_sidepath:of')
checks = sidepath_dict[id]['checks']
if not is_sidepath:
is_sidepath = 'no'
for road_id in sidepath_dict[id]['id'].keys():
if checks <= 2:
if sidepath_dict[id]['id'][road_id] == checks:
is_sidepath = 'yes'
else:
if sidepath_dict[id]['id'][road_id] >= checks * 0.66:
is_sidepath = 'yes'
if is_sidepath != 'yes':
for highway in sidepath_dict[id]['highway'].keys():
if checks <= 2:
if sidepath_dict[id]['highway'][highway] == checks:
is_sidepath = 'yes'
else:
if sidepath_dict[id]['highway'][highway] >= checks * 0.66:
is_sidepath = 'yes'
if is_sidepath != 'yes':
for name in sidepath_dict[id]['name'].keys():
if checks <= 2:
if sidepath_dict[id]['name'][name] == checks:
is_sidepath = 'yes'
else:
if sidepath_dict[id]['name'][name] >= checks * 0.66:
is_sidepath = 'yes'
layer.changeAttributeValue(feature.id(), id_proc_sidepath, is_sidepath)
#derive the highway class of the associated road
if not is_sidepath_of and is_sidepath == 'yes':
if len(sidepath_dict[id]['highway']):
max_value = max(sidepath_dict[id]['highway'].values())
max_keys = [key for key, value in sidepath_dict[id]['highway'].items() if value == max_value]
min_index = len(highway_class_list) - 1
for key in max_keys:
if highway_class_list.index(key) < min_index:
min_index = highway_class_list.index(key)
is_sidepath_of = highway_class_list[min_index]
layer.changeAttributeValue(feature.id(), id_proc_highway, is_sidepath_of)
if is_sidepath == 'yes' and is_sidepath_of and is_sidepath_of in sidepath_dict[id]['maxspeed']:
maxspeed = sidepath_dict[id]['maxspeed'][is_sidepath_of]
if maxspeed:
layer.changeAttributeValue(feature.id(), id_proc_maxspeed, d.getNumber(maxspeed))
#transfer names to sidepath
if is_sidepath == 'yes' and len(sidepath_dict[id]['name']):
name = max(sidepath_dict[id]['name'], key=lambda k: sidepath_dict[id]['name'][k]) #the most frequent name in the surrounding
if name:
layer.changeAttributeValue(feature.id(), layer.fields().indexOf('name'), name)
#-------------------------------------------------------------------------------#
#2: Split and shift attributes/geometries for sidepath mapped on the centerline #
#-------------------------------------------------------------------------------#
print(time.strftime('%H:%M:%S', time.localtime()), 'Split line bundles...')
with edit(layer):
for feature in layer.getFeatures():
highway = feature.attribute('highway')
cycleway = feature.attribute('cycleway')
cycleway_both = feature.attribute('cycleway:both')
cycleway_left = feature.attribute('cycleway:left')
cycleway_right = feature.attribute('cycleway:right')
sidewalk_bicycle = feature.attribute('sidewalk:bicycle')
sidewalk_both_bicycle = feature.attribute('sidewalk:both:bicycle')
sidewalk_left_bicycle = feature.attribute('sidewalk:left:bicycle')
sidewalk_right_bicycle = feature.attribute('sidewalk:right:bicycle')
offset_cycleway_left = offset_cycleway_right = offset_sidewalk_left = offset_sidewalk_right = 0
side = NULL
#TODO: more precise offset calculation taking "parking:", "placement", "width:lanes" and other Tags into account
if p.offset_distance == 'realistic':
#use road width as offset for the new geometry
width = d.getNumber(feature.attribute('width'))
#use default road width if width isn't specified
if not width:
if highway in p.default_highway_width_dict:
width = p.default_highway_width_dict[highway]
else:
width = p.default_highway_width_fallback
#offset for cycleways
if highway != 'cycleway':
#offset for left cycleways
if cycleway in ['lane', 'track', 'share_busway'] or cycleway_both in ['lane', 'track', 'share_busway'] or cycleway_left in ['lane', 'track', 'share_busway']:
#option 1: offset of sidepath lines according to real distances on the ground
if p.offset_distance == 'realistic':
offset_cycleway_left = width / 2
#option 2: static offset as defined in the variable
else:
offset_cycleway_left = d.getNumber(p.offset_distance)
layer.changeAttributeValue(feature.id(), id_offset_cycleway_left, offset_cycleway_left)
#offset for right cycleways
if cycleway in ['lane', 'track', 'share_busway'] or cycleway_both in ['lane', 'track', 'share_busway'] or cycleway_right in ['lane', 'track', 'share_busway']:
if p.offset_distance == 'realistic':
offset_cycleway_right = width / 2
else:
offset_cycleway_right = d.getNumber(p.offset_distance)
layer.changeAttributeValue(feature.id(), id_offset_cycleway_right, offset_cycleway_right)
#offset for shared footways
#offset for left sidewalks
if sidewalk_bicycle in ['yes', 'designated', 'permissive'] or sidewalk_both_bicycle in ['yes', 'designated', 'permissive'] or sidewalk_left_bicycle in ['yes', 'designated', 'permissive']:
if p.offset_distance == 'realistic':
#use larger offset than for cycleways to get nearby, parallel lines in case both (cycleway and sidewalk) exist
offset_sidewalk_left = width / 2 + 2
else:
#TODO: double offset if cycleway exists on same side
offset_sidewalk_left = d.getNumber(p.offset_distance)
layer.changeAttributeValue(feature.id(), id_offset_sidewalk_left, offset_sidewalk_left)
#offset for right sidewalks
if sidewalk_bicycle in ['yes', 'designated', 'permissive'] or sidewalk_both_bicycle in ['yes', 'designated', 'permissive'] or sidewalk_right_bicycle in ['yes', 'designated', 'permissive']:
if p.offset_distance == 'realistic':
offset_sidewalk_right = width / 2 + 2
else:
offset_sidewalk_right = d.getNumber(p.offset_distance)
layer.changeAttributeValue(feature.id(), id_offset_sidewalk_right, offset_sidewalk_right)
processing.run('qgis:selectbyexpression', {'INPUT' : layer, 'EXPRESSION' : '\"offset_cycleway_left\" IS NOT NULL'})
offset_cycleway_left_layer = processing.run('native:offsetline', {'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True), 'DISTANCE': QgsProperty.fromExpression('"offset_cycleway_left"'), 'OUTPUT': 'memory:'})['OUTPUT']
processing.run('qgis:selectbyexpression', {'INPUT' : layer, 'EXPRESSION' : '\"offset_cycleway_right\" IS NOT NULL'})
offset_cycleway_right_layer = processing.run('native:offsetline', {'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True), 'DISTANCE': QgsProperty.fromExpression('-"offset_cycleway_right"'), 'OUTPUT': 'memory:'})['OUTPUT']
processing.run('qgis:selectbyexpression', {'INPUT' : layer, 'EXPRESSION' : '\"offset_sidewalk_left\" IS NOT NULL'})
offset_sidewalk_left_layer = processing.run('native:offsetline', {'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True), 'DISTANCE': QgsProperty.fromExpression('"offset_sidewalk_left"'), 'OUTPUT': 'memory:'})['OUTPUT']
processing.run('qgis:selectbyexpression', {'INPUT' : layer, 'EXPRESSION' : '\"offset_sidewalk_right\" IS NOT NULL'})
offset_sidewalk_right_layer = processing.run('native:offsetline', {'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True), 'DISTANCE': QgsProperty.fromExpression('-"offset_sidewalk_right"'), 'OUTPUT': 'memory:'})['OUTPUT']
#TODO: offset als Attribut überschreiben
#eigenständige Attribute ableiten
layer.updateFields()
#derive attributes for offset ways
for side in ['left', 'right']:
for type in ['cycleway', 'sidewalk']:
layer_name = 'offset_' + type + '_' + side + '_layer'
exec("%s = %s" % ('offset_layer', layer_name))
with edit(offset_layer):
for feature in offset_layer.getFeatures():
offset_layer.changeAttributeValue(feature.id(), id_offset, feature.attribute('offset_' + type + '_' + side))
offset_layer.changeAttributeValue(feature.id(), id_type, type)
offset_layer.changeAttributeValue(feature.id(), id_side, side)
#this offset geometries are sidepath
offset_layer.changeAttributeValue(feature.id(), id_proc_sidepath, 'yes')
offset_layer.changeAttributeValue(feature.id(), id_proc_highway, feature.attribute('highway'))
offset_layer.changeAttributeValue(feature.id(), id_proc_maxspeed, feature.attribute('maxspeed'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('width'), d.deriveAttribute(feature, 'width', type, side, 'float'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('oneway'), d.deriveAttribute(feature, 'oneway', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('oneway:bicycle'), d.deriveAttribute(feature, 'oneway:bicycle', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('traffic_sign'), d.deriveAttribute(feature, 'traffic_sign', type, side, 'str'))
#surface and smoothness of cycle lanes are usually the same as on the road (if not explicitely tagged)
if type != 'cycleway' or (type == 'cycleway' and ((feature.attribute('cycleway:' + side) == 'track' or feature.attribute('cycleway:both') == 'track' or feature.attribute('cycleway') == 'track') or feature.attribute(type + ':' + side + ':surface') != NULL or feature.attribute(type + ':both:surface') != NULL or feature.attribute(type + ':surface') != NULL)):
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('surface'), d.deriveAttribute(feature, 'surface', type, side, 'str'))
if type != 'cycleway' or (type == 'cycleway' and ((feature.attribute('cycleway:' + side) == 'track' or feature.attribute('cycleway:both') == 'track' or feature.attribute('cycleway') == 'track') or feature.attribute(type + ':' + side + ':smoothness') != NULL or feature.attribute(type + ':both:smoothness') != NULL or feature.attribute(type + ':smoothness') != NULL)):
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('smoothness'), d.deriveAttribute(feature, 'smoothness', type, side, 'str'))
if type == 'cycleway':
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('separation'), d.deriveAttribute(feature, 'separation', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('separation:both'), d.deriveAttribute(feature, 'separation:both', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('separation:left'), d.deriveAttribute(feature, 'separation:left', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('separation:right'), d.deriveAttribute(feature, 'separation:right', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('buffer'), d.deriveAttribute(feature, 'buffer', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('buffer:both'), d.deriveAttribute(feature, 'buffer:both', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('buffer:left'), d.deriveAttribute(feature, 'buffer:left', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('buffer:right'), d.deriveAttribute(feature, 'buffer:right', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('traffic_mode:both'), d.deriveAttribute(feature, 'traffic_mode:both', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('traffic_mode:left'), d.deriveAttribute(feature, 'traffic_mode:left', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('traffic_mode:right'), d.deriveAttribute(feature, 'traffic_mode:right', type, side, 'str'))
offset_layer.changeAttributeValue(feature.id(), offset_layer.fields().indexOf('surface:colour'), d.deriveAttribute(feature, 'surface:colour', type, side, 'str'))
#TODO: Attribute mit "both" auf left und right aufteilen?
#TODO: clean up offset layers
#merge vanilla and offset layers
layer = processing.run('native:mergevectorlayers', {'LAYERS' : [layer, offset_cycleway_left_layer, offset_cycleway_right_layer, offset_sidewalk_left_layer, offset_sidewalk_right_layer], 'OUTPUT': 'memory:'})['OUTPUT']
#--------------------------------------------#
#3: Determine way type for every way segment #
#--------------------------------------------#
print(time.strftime('%H:%M:%S', time.localtime()), 'Determine way type...')
with edit(layer):
for feature in layer.getFeatures():
#exclude segments with no public bicycle access
if d.getAccess(feature, 'bicycle') and d.getAccess(feature, 'bicycle') not in ['yes', 'permissive', 'designated', 'use_sidepath', 'optional_sidepath', 'discouraged']:
layer.deleteFeature(feature.id())
#exclude informal paths without explicit bicycle access
if feature.attribute('highway') == 'path' and feature.attribute('informal') == 'yes' and feature.attribute('bicycle') == NULL:
layer.deleteFeature(feature.id())
way_type = ''
highway = feature.attribute('highway')
segregated = feature.attribute('segregated')
bicycle = feature.attribute('bicycle')
foot = feature.attribute('foot')
vehicle = feature.attribute('vehicle')
is_sidepath = feature.attribute('is_sidepath')
#before determining the way type according to highway tagging, first check for some specific way types that are tagged independend from "highway":
if feature.attribute('bicycle_road') == 'yes':
#features with a "side" attribute are representing a cycleway or footway adjacent to the road with offset geometry - treat them as separate path, not as a bicycle road
side = feature.attribute('side')
if not side:
way_type = 'bicycle road'
if feature.attribute('footway') == 'link' or feature.attribute('cycleway') == 'link' or feature.attribute('path') == 'link' or feature.attribute('bridleway') == 'link':
way_type = 'link'
if feature.attribute('footway') == 'crossing' or feature.attribute('cycleway') == 'crossing' or feature.attribute('path') == 'crossing' or feature.attribute('bridleway') == 'crossing':
way_type = 'crossing'
#for all other cases: derive way type according to their primary "highway" tagging:
if way_type == '':
#for footways (with bicycle access):
if highway in ['footway', 'pedestrian', 'bridleway', 'steps']:
if bicycle in ['yes', 'designated', 'permissive']:
way_type = 'shared footway'
else:
layer.deleteFeature(feature.id()) #don't process ways with restricted bicycle access
#for path:
elif highway == 'path':
if foot == 'designated' and bicycle != 'designated':
way_type = 'shared footway'
else:
if segregated == 'yes':
way_type = 'segregated path'
else:
way_type = 'shared path'
#for cycleways:
elif highway == 'cycleway':
if foot in ['yes', 'designated', 'permissive']:
way_type = 'shared path'
else:
separation_foot = d.deriveSeparation(feature, 'foot')
if separation_foot == 'no':
way_type = 'segregated path'
else:
if not is_sidepath in ['yes', 'no']:
#Use the geometrically determined sidepath value, if is_sidepath isn't specified
if feature.attribute('proc_sidepath') == 'yes':
way_type = 'cycle track'
else:
way_type = 'cycle path'
elif is_sidepath == 'yes':
separation_motor_vehicle = d.deriveSeparation(feature, 'motor_vehicle')
if not separation_motor_vehicle in [NULL, 'no', 'none']:
if 'kerb' in separation_motor_vehicle or 'tree_row' in separation_motor_vehicle:
way_type = 'cycle track'
else:
way_type = 'cycle lane (protected)'
else:
way_type = 'cycle track'
else:
way_type = 'cycle path'
#for service roads/tracks:
elif highway == 'service' or highway == 'track':
way_type = 'track or service'
#for regular roads:
else:
cycleway = feature.attribute('cycleway')
cycleway_both = feature.attribute('cycleway:both')
cycleway_left = feature.attribute('cycleway:left')
cycleway_right = feature.attribute('cycleway:right')
bicycle = feature.attribute('bicycle')
side = feature.attribute('side') #features with a "side" attribute are representing a cycleway or footway adjacent to the road with offset geometry
#if this feature don't represent a cycle lane, it's a center line representing the shared road
if not side:
#distinguish shared roads (without lane markings) and shared traffic lanes (with lane markings)
#(assume that there are lane markings on primary and secondary roads, even if not tagged explicitely)
lane_markings = feature.attribute('lane_markings')
if lane_markings == 'yes' or (lane_markings != 'yes' and highway in ['motorway', 'trunk', 'primary', 'secondary']):
way_type = 'shared traffic lane'
else:
way_type = 'shared road'
else:
type = feature.attribute('type')
if type == 'sidewalk':
way_type = 'shared footway'
else:
#for cycle lanes
if cycleway == 'lane' or cycleway_both == 'lane' or (side == 'right' and cycleway_right == 'lane') or (side == 'left' and cycleway_left == 'lane'):
cycleway_lanes = feature.attribute('cycleway:lanes')
if cycleway_lanes and 'no|lane|no' in cycleway_lanes:
way_type = 'cycle lane (central)'
else:
separation_motor_vehicle = d.deriveSeparation(feature, 'motor_vehicle')
if not separation_motor_vehicle in [NULL, 'no', 'none']:
way_type = 'cycle lane (protected)'
else:
cycleway_lane = feature.attribute('cycleway:lane')
cycleway_both_lane = feature.attribute('cycleway:both:lane')
cycleway_left_lane = feature.attribute('cycleway:left:lane')
cycleway_right_lane = feature.attribute('cycleway:right:lane')
if cycleway_lane == 'exclusive' or cycleway_both_lane == 'exclusive' or (side == 'right' and cycleway_right_lane == 'exclusive') or (side == 'left' and cycleway_left_lane == 'exclusive'):
way_type = 'cycle lane (exclusive)'
else:
way_type = 'cycle lane (advisory)'
#for cycle tracks
elif cycleway == 'track' or cycleway_both == 'track' or (side == 'right' and cycleway_right == 'track') or (side == 'left' and cycleway_left == 'track'):
cycleway_foot = feature.attribute('cycleway:foot')
cycleway_both_foot = feature.attribute('cycleway:both:foot')
cycleway_left_foot = feature.attribute('cycleway:left:foot')
cycleway_right_foot = feature.attribute('cycleway:right:foot')
if cycleway_foot in ['yes', 'designated', 'permissive'] or cycleway_both_foot in ['yes', 'designated', 'permissive'] or (side == 'right' and cycleway_right_foot in ['yes', 'designated', 'permissive']) or (side == 'left' and cycleway_left_foot in ['yes', 'designated', 'permissive']):
way_type = 'shared path'
else:
cycleway_segregated = feature.attribute('cycleway:segregated')
cycleway_both_segregated = feature.attribute('cycleway:both:segregated')
cycleway_left_segregated = feature.attribute('cycleway:left:segregated')
cycleway_right_segregated = feature.attribute('cycleway:right:segregated')
if cycleway_segregated == 'yes' or cycleway_both_segregated == 'yes' or (side == 'right' and cycleway_right_segregated == 'yes') or (side == 'left' and cycleway_left_segregated == 'yes'):
way_type = 'segregated path'
elif cycleway_segregated == 'no' or cycleway_both_segregated == 'no' or (side == 'right' and cycleway_right_segregated == 'no') or (side == 'left' and cycleway_left_segregated == 'no'):
way_type = 'shared path'
else:
separation_foot = d.deriveSeparation(feature, 'foot')
if separation_foot == 'no':
way_type = 'segregated path'
else:
separation_motor_vehicle = d.deriveSeparation(feature, 'motor_vehicle')
if not separation_motor_vehicle in [NULL, 'no', 'none']:
if 'kerb' in separation_motor_vehicle or 'tree_row' in separation_motor_vehicle:
way_type = 'cycle track'
else:
way_type = 'cycle lane (protected)'
else:
way_type = 'cycle track'
#for shared bus lanes
elif cycleway == 'share_busway' or cycleway_both == 'share_busway' or (side == 'right' and cycleway_right == 'share_busway') or (side == 'left' and cycleway_left == 'share_busway'):
way_type = 'shared bus lane'
#for other vales - no cycle way
else:
sidewalk_bicycle = feature.attribute('sidewalk:bicycle')
sidewalk_both_bicycle = feature.attribute('sidewalk:both:bicycle')
sidewalk_left_bicycle = feature.attribute('sidewalk:left:bicycle')
sidewalk_right_bicycle = feature.attribute('sidewalk:right:bicycle')
if sidewalk_bicycle == 'yes' or sidewalk_both_bicycle == 'yes' or (side == 'right' and sidewalk_right_bicycle == 'yes') or (side == 'left' and sidewalk_left_bicycle == 'yes'):
way_type = 'shared footway'
else:
lane_markings = feature.attribute('lane_markings')
if lane_markings == 'yes' or (lane_markings != 'yes' and highway in ['primary', 'secondary']):
way_type = 'shared traffic lane'
else:
way_type = 'shared road'
if way_type == '':
way_type = NULL
else:
layer.changeAttributeValue(feature.id(), id_way_type, way_type)
layer.updateFields()
#----------------------------------------------------#
#4: Derive relevant attributes for index and factors #
#----------------------------------------------------#
print(time.strftime('%H:%M:%S', time.localtime()), 'Derive attributes/calculate index...')
with edit(layer):
for feature in layer.getFeatures():
way_type = feature.attribute('way_type')
side = feature.attribute('side')
is_sidepath = feature.attribute('proc_sidepath')
data_missing = ''
#-------------
#Derive oneway status. Can be one of the values in oneway_value_list (oneway applies to all vehicles, also for bicycles) or '*_motor_vehicles' (value applies to motor vehicles only)
#-------------
oneway_value_list = ['yes', 'no', '-1', 'alternating', 'reversible']
proc_oneway = NULL
oneway = feature.attribute('oneway')
oneway_bicycle = feature.attribute('oneway:bicycle')
cycleway_oneway = feature.attribute('cycleway:oneway')
if way_type in ['cycle path', 'cycle track', 'shared path', 'segregated path', 'shared footway', 'crossing', 'link', 'cycle lane (advisory)', 'cycle lane (exclusive)', 'cycle lane (protected)', 'cycle lane (central)']:
if oneway in oneway_value_list:
proc_oneway = oneway
elif cycleway_oneway in oneway_value_list:
proc_oneway = cycleway_oneway
else:
if way_type in ['cycle track', 'shared path', 'shared footway'] and side:
proc_oneway = p.default_oneway_cycle_track
elif 'cycle lane' in way_type:
proc_oneway = p.default_oneway_cycle_lane
else:
proc_oneway = 'no'
if oneway_bicycle in oneway_value_list: #usually not the case on cycle ways, but possible: overwrite oneway value with oneway:bicycle
proc_oneway = oneway_bicycle
if way_type == 'shared bus lane':
proc_oneway = 'yes' #shared bus lanes are represented by own geometry for the lane, and lanes are for oneway use only (usually)
if way_type in ['shared road', 'shared traffic lane', 'bicycle road', 'track or service']:
if not oneway_bicycle or oneway == oneway_bicycle:
if oneway in oneway_value_list:
proc_oneway = oneway
else:
proc_oneway = 'no'
else:
if oneway_bicycle and oneway_bicycle == 'no':
if oneway in oneway_value_list:
proc_oneway = oneway + '_motor_vehicles'
else:
proc_oneway = 'no'
else:
proc_oneway = 'yes'
if not proc_oneway:
proc_oneway = 'unknown'
layer.changeAttributeValue(feature.id(), id_proc_oneway, proc_oneway)
#-------------
#Derive width. Use explicitely tagged attributes, derive from other attributes or use default values.
#-------------
proc_width = NULL
if way_type in ['cycle path', 'cycle track', 'shared path', 'shared footway', 'crossing', 'link', 'cycle lane (advisory)', 'cycle lane (exclusive)', 'cycle lane (protected)', 'cycle lane (central)']:
#width for cycle lanes and sidewalks have already been derived from original tags when calculating way offsets
proc_width = d.getNumber(feature.attribute('cycleway:width')) #check for cycleway:width first for cases, where segregated isn't tagged correctly
if not proc_width:
proc_width = d.getNumber(feature.attribute('width'))
if not proc_width:
if way_type in ['cycle path', 'shared path', 'cycle lane (protected)']:
proc_width = p.default_highway_width_dict['path']
elif way_type == 'shared footway':
proc_width = p.default_highway_width_dict['footway']
else:
proc_width = p.default_highway_width_dict['cycleway']
if proc_width and proc_oneway == 'no':
proc_width *= 1.6 #default values are for oneways - if the way isn't a oneway, widen the default
data_missing = d.addDelimitedValue(data_missing, 'width')
layer.changeAttributeValue(feature.id(), id_data_missing_width, 1)
if way_type == 'segregated path':
highway = feature.attribute('highway')
if highway == 'path':
proc_width = d.getNumber(feature.attribute('cycleway:width'))
if not proc_width:
width = d.getNumber(feature.attribute('width'))
footway_width = d.getNumber(feature.attribute('footway:width'))
if width:
if footway_width:
proc_width = width - footway_width
else:
proc_width = width / 2
data_missing = d.addDelimitedValue(data_missing, 'width')
layer.changeAttributeValue(feature.id(), id_data_missing_width, 1)
else:
proc_width = d.getNumber(feature.attribute('width'))
if not proc_width:
proc_width = p.default_highway_width_dict['path']
if proc_oneway == 'no':
proc_width *= 1.6
data_missing = d.addDelimitedValue(data_missing, 'width')
layer.changeAttributeValue(feature.id(), id_data_missing_width, 1)
if way_type in ['shared road', 'shared traffic lane', 'shared bus lane', 'bicycle road', 'track or service']:
#on shared traffic or bus lanes, use a width value based on lane width, not on carriageway width
if way_type in ['shared traffic lane', 'shared bus lane']:
width_lanes = feature.attribute('width:lanes')
width_lanes_forward = feature.attribute('width:lanes:forward')
width_lanes_backward = feature.attribute('width:lanes:backward')
if ('yes' in proc_oneway or way_type != 'shared bus lane') and width_lanes and '|' in width_lanes:
#TODO: at the moment, forward/backward can only be processed for shared bus lanes, since there are no separate geometries for shared road lanes
#TODO: for bus lanes, currently only assuming that the right lane is the bus lane. Instead derive lane position from "psv:lanes" or "bus:lanes", if specified
proc_width = d.getNumber(width_lanes[width_lanes.rfind('|') + 1:])
elif (way_type == 'shared bus lane' and not 'yes' in proc_oneway) and side == 'right' and width_lanes_forward and '|' in width_lanes_forward:
proc_width = d.getNumber(width_lanes_forward[width_lanes_forward.rfind('|') + 1:])
elif (way_type == 'shared bus lane' and not 'yes' in proc_oneway) and side == 'left' and width_lanes_backward and '|' in width_lanes_backward:
proc_width = d.getNumber(width_lanes_backward[width_lanes_backward.rfind('|') + 1:])
else:
if way_type == 'shared bus lane':
proc_width = p.default_width_bus_lane
else:
proc_width = p.default_width_traffic_lane
data_missing = d.addDelimitedValue(data_missing, 'width:lanes')
if not proc_width:
#effective width (usable width of a road for flowing traffic) can be mapped explicitely
proc_width = d.getNumber(feature.attribute('width:effective'))
#try to use lane count and a default lane width if no width and no width:effective is mapped
#(usually, this means, there are lane markings (see above), but sometimes "lane" tag is misused or "lane_markings" isn't mapped)
if not proc_width:
width = d.getNumber(feature.attribute('width'))
if not width:
lanes = d.getNumber(feature.attribute('lanes'))
if lanes:
proc_width = lanes * p.default_width_traffic_lane
#TODO: take width:lanes into account, if mapped
#derive effective road width from road width, parking and cycle lane informations
#subtract parking and cycle lane width from carriageway width to get effective width (usable width for driving)
if not proc_width:
#derive parking lane width
parking_left = feature.attribute('parking:left')
parking_left_orientation = feature.attribute('parking:left:orientation')
parking_left_width = d.getNumber(feature.attribute('parking:left:width'))
parking_right = feature.attribute('parking:right')
parking_right_orientation = feature.attribute('parking:right:orientation')
parking_right_width = d.getNumber(feature.attribute('parking:right:width'))
parking_both = feature.attribute('parking:both')
parking_both_orientation = feature.attribute('parking:both:orientation')
parking_both_width = d.getNumber(feature.attribute('parking:both:width'))
#split parking:both-keys into left and right values
if parking_both:
if not parking_right:
parking_right = parking_both
if not parking_left:
parking_left = parking_both
if parking_both_orientation:
if not parking_right_orientation:
parking_right_orientation = parking_both_orientation
if not parking_left_orientation:
parking_left_orientation = parking_both_orientation
if parking_both_width:
if not parking_right_width:
parking_right_width = parking_both_width
if not parking_left_width:
parking_left_width = parking_both_width
if parking_right == 'lane' or parking_right == 'half_on_kerb':
if not parking_right_width:
if parking_right_orientation == 'diagonal':
parking_right_width = p.default_width_parking_diagonal
elif parking_right_orientation == 'perpendicular':
parking_right_width = p.default_width_parking_perpendicular
else:
parking_right_width = p.default_width_parking_parallel
if parking_right == 'half_on_kerb':
parking_right_width = float(parking_right_width) / 2
if parking_left == 'lane' or parking_left == 'half_on_kerb':
if not parking_left_width:
if parking_left_orientation == 'diagonal':
parking_left_width = p.default_width_parking_diagonal
elif parking_left_orientation == 'perpendicular':
parking_left_width = p.default_width_parking_perpendicular
else:
parking_left_width = p.default_width_parking_parallel
if parking_left == 'half_on_kerb':
parking_left_width = float(parking_left_width) / 2
if not parking_right_width:
parking_right_width = 0
if not parking_left_width:
parking_left_width = 0
#derive cycle lane width
cycleway = feature.attribute('cycleway')
cycleway_left = feature.attribute('cycleway:left')
cycleway_right = feature.attribute('cycleway:right')
cycleway_both = feature.attribute('cycleway:both')
cycleway_width = feature.attribute('cycleway:width')
cycleway_left_width = feature.attribute('cycleway:left:width')
cycleway_right_width = feature.attribute('cycleway:right:width')
cycleway_both_width = feature.attribute('cycleway:both:width')
buffer = 0
cycleway_right_buffer_left = NULL
cycleway_right_buffer_right = NULL
cycleway_left_buffer_left = NULL
cycleway_left_buffer_right = NULL
#split cycleway:both-keys into left and right values
if cycleway:
if not cycleway_right:
cycleway_right = cycleway
if not cycleway_left and (not oneway or oneway == 'no'):
cycleway_left = cycleway
if cycleway_both:
if not cycleway_right:
cycleway_right = cycleway_both
if not cycleway_left:
cycleway_left = cycleway_both
if cycleway_right == 'lane' or cycleway_left == 'lane':
if cycleway_width:
if not cycleway_right_width:
cycleway_right_width = cycleway_width
if not cycleway_left_width and (not oneway or oneway == 'no'):
cycleway_left_width = cycleway_width
if cycleway_both_width:
if not cycleway_right_width:
cycleway_right_width = cycleway_both_width
if not cycleway_left_width:
cycleway_left_width = cycleway_both_width
#cycleway buffers must also be subtracted from the road width
cycleway_buffer = feature.attribute('cycleway:buffer')
cycleway_left_buffer = feature.attribute('cycleway:left:buffer')
cycleway_right_buffer = feature.attribute('cycleway:right:buffer')
cycleway_both_buffer = feature.attribute('cycleway:both:buffer')
cycleway_buffer_left = feature.attribute('cycleway:buffer:left')
cycleway_left_buffer_left = feature.attribute('cycleway:left:buffer:left')
cycleway_right_buffer_left = feature.attribute('cycleway:right:buffer:left')
cycleway_both_buffer_left = feature.attribute('cycleway:both:buffer:left')
cycleway_buffer_right = feature.attribute('cycleway:buffer:right')
cycleway_left_buffer_right = feature.attribute('cycleway:left:buffer:right')
cycleway_right_buffer_right = feature.attribute('cycleway:right:buffer:right')
cycleway_both_buffer_right = feature.attribute('cycleway:both:buffer:right')
cycleway_buffer_both = feature.attribute('cycleway:buffer:both')
cycleway_left_buffer_both = feature.attribute('cycleway:left:buffer:both')
cycleway_right_buffer_both = feature.attribute('cycleway:right:buffer:both')
cycleway_both_buffer_both = feature.attribute('cycleway:both:buffer:both')
if cycleway_right == 'lane':
if not cycleway_right_width:
cycleway_right_width = p.default_width_cycle_lane
for buffer_tag in [cycleway_right_buffer_left, cycleway_right_buffer_both, cycleway_right_buffer, cycleway_both_buffer_left, cycleway_both_buffer_both, cycleway_both_buffer, cycleway_buffer_left, cycleway_buffer_both, cycleway_buffer]:
if not cycleway_right_buffer_left:
cycleway_right_buffer_left = buffer_tag
else:
break
for buffer_tag in [cycleway_right_buffer_right, cycleway_right_buffer_both, cycleway_right_buffer, cycleway_both_buffer_right, cycleway_both_buffer_both, cycleway_both_buffer, cycleway_buffer_right, cycleway_buffer_both, cycleway_buffer]:
if not cycleway_right_buffer_right:
cycleway_right_buffer_right = buffer_tag
else:
break
if cycleway_left == 'lane':
if not cycleway_left_width:
cycleway_left_width = p.default_width_cycle_lane
for buffer_tag in [cycleway_left_buffer_left, cycleway_left_buffer_both, cycleway_left_buffer, cycleway_both_buffer_left, cycleway_both_buffer_both, cycleway_both_buffer, cycleway_buffer_left, cycleway_buffer_both, cycleway_buffer]:
if not cycleway_left_buffer_left:
cycleway_left_buffer_left = buffer_tag
else:
break
for buffer_tag in [cycleway_left_buffer_right, cycleway_left_buffer_both, cycleway_left_buffer, cycleway_both_buffer_right, cycleway_both_buffer_both, cycleway_both_buffer, cycleway_buffer_right, cycleway_buffer_both, cycleway_buffer]:
if not cycleway_left_buffer_right:
cycleway_left_buffer_right = buffer_tag
else:
break
if not cycleway_right_width:
cycleway_right_width = 0
if not cycleway_left_width:
cycleway_left_width = 0
if not cycleway_right_buffer_left or cycleway_right_buffer_left == 'no' or cycleway_right_buffer_left == 'none':
cycleway_right_buffer_left = 0
if not cycleway_right_buffer_right or cycleway_right_buffer_right == 'no' or cycleway_right_buffer_right == 'none':
cycleway_right_buffer_right = 0
if not cycleway_left_buffer_left or cycleway_left_buffer_left == 'no' or cycleway_left_buffer_left == 'none':
cycleway_left_buffer_left = 0
if not cycleway_left_buffer_right or cycleway_left_buffer_right == 'no' or cycleway_left_buffer_right == 'none':
cycleway_left_buffer_right = 0
#carriageway width: use default road width if no width is specified
if not width:
highway = feature.attribute('highway')
if highway in p.default_highway_width_dict:
width = p.default_highway_width_dict[highway]
else:
width = p.default_highway_width_fallback
#assume that oneway roads are narrower
if 'yes' in proc_oneway:
width = round(width / 1.6, 1)
data_missing = d.addDelimitedValue(data_missing, 'width')
layer.changeAttributeValue(feature.id(), id_data_missing_width, 1)
buffer = d.getNumber(cycleway_right_buffer_left) + d.getNumber(cycleway_right_buffer_right) + d.getNumber(cycleway_left_buffer_left) + d.getNumber(cycleway_left_buffer_right)
proc_width = width - d.getNumber(cycleway_right_width) - d.getNumber(cycleway_left_width) - buffer
if parking_right or parking_left:
proc_width = proc_width - d.getNumber(parking_right_width) - d.getNumber(parking_left_width)
#if parking isn't mapped on regular shared roads, reduce width if it's above a threshold (assuming there might be unmapped parking)
else:
if way_type == 'shared road':
if not 'yes' in proc_oneway:
#assume that 5.5m of a regular unmarked carriageway are used for driving, other space for parking...
proc_width = min(proc_width, 5.5)
else:
#resp. 4m in oneway roads
proc_width = min(proc_width, 4)