-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgrid2demand.py
More file actions
1627 lines (1400 loc) · 73.4 KB
/
grid2demand.py
File metadata and controls
1627 lines (1400 loc) · 73.4 KB
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
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Grid2Demand based on osm2gmns
Author: Anjun Li, Southwest Jiaotong University
Xuesong (Simon) Zhou, Arizona State University
Entai Wang, Beijing Jiaotong University
Taehooie Kim, Arizona State University
Email: anjun.li93@gmail.com
xzhou74@asu.edu
entaiwang@bjtu.edu.cn
taehooie.kim@asu.edu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
import os
import pandas as pd
import numpy as np
import math
import csv
import locale
import sys
from pprint import pprint
from collections import defaultdict
import logging
import random
from random import choice
class Node:
def __init__(self):
self.id = 0
self.zone_id = None
# commments: default is 0, or no value; only three conditions for a node to become an activity node
# and zone_id != 0: 1) POI node, 2) is_boundary node(freeway) 3) residential in activity_type
self.node_type = ''
self.x_coord = 0
self.y_coord = 0
self.production = 0 # commments: = 0 (current node is not poi node)
self.attraction = 0 # commments: = 0 (current node is not poi node)
self.boundary_flag = 0 # comments: = 1 (current node is boundry node)
self.poi_id = '' # commments: default = None; to be assigned to a POI ID after reading poi.csv
# if current node is poi ndoe
self.activity_type = '' # commments: provided from osm2gmns such as motoway, residential, ...
self.activity_location_tab = ''
self.osm_node_id = ''
class POI:
def __init__(self):
self.id = 0
self.zone_id = 0 # comments: mapping from zone
self.x_coord = 0
self.y_coord = 0
self.count = 1 # commments: total poi value for this poi node or poi zone
self.area = 0 # comments: total area of polygon for this poi zone
self.type = ''
class Zone: # comments: area of grid zone
def __init__(self):
self.id = 0
self.name = '' # comments: internal No., such as A1, A2,...
self.centroid_x = 0
self.centroid_y = 0
self.centroid = '' # comments: centroid coordinate (x, y) based on wkt format
self.x_max = 0 # comments: boundary coordinate for this zone
self.x_min = 0
self.y_max = 0
self.y_min = 0
self.poi_count = 0 # comments: total poi counts in this zone
self.residential_count = 0 # number of residential poi nodes in the zone
self.office_count = 0 # number of office poi nodes in the zone
self.shopping_count = 0 # number of shopping poi nodes in the zone
self.school_count = 0 # number of school poi nodes in the zone
self.parking_count = 0 # number of parking poi nodes in the zone
self.boundary_count = 0 # number of boundary nodes in the zone
self.node_id_list = [] # comments: nodes which belong to this zone
self.poi_id_list = [] # comments: poi points which belong to this zone
self.poi_node_list = []
self.polygon = '' # comments: study area boundary based on wkt format
self.connector_list = []
self.centroid_node = None
class Agent:
def __init__(self, agent_id, agent_type, o_zone_id,
d_zone_id):
""" the attribute of agent """
self.agent_id = agent_id
self.agent_type = agent_type # comments: vehicle default
self.o_zone_id = int(o_zone_id)
self.d_zone_id = int(d_zone_id)
self.o_node_id = 0 # comments: randomly selected from activity nodes of a zone
self.d_node_id = 0
self.path_node_seq_no_list = list() # comments: node id not node seq no
self.path_link_seq_no_list = list()
self.current_link_seq_no_in_path = 0 # comments: not used
self.path_cost = 0
self.b_generated = False
self.b_complete_trip = False
class Link:
def __init__(self, link_id, from_node_id, to_node_id, link_type_name, link_type_id):
""" the attribute of link """
self.link_id = link_id
self.from_node_id = from_node_id
self.to_node_id = to_node_id
self.link_type_name = link_type_name
self.link_type_id = link_type_id
self.geometry = None
# create a logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
"""PART 1 READ INPUT NETWORK FILES"""
g_node_list = []
g_boundary_node_list = []
g_outside_boundary_node_list = [] # comments: nodes outside the study area
g_poi_list = []
g_poi_id_type_dict = {}
g_poi_id_area_dict = {}
g_outside_boundary_node_id_index = {}
g_output_folder = ''
g_node_id_to_node = {}
g_node_id_to_index = {}
g_poi_map = {}
g_average_latitude = 0
def ReadNetworkFiles(input_folder=None):
global g_poi_id_type_dict
global g_poi_id_area_dict
global g_output_folder
global g_average_latitude
if input_folder:
node_filepath = os.path.join(input_folder, 'node.csv')
poi_filepath = os.path.join(input_folder, 'poi.csv')
g_output_folder = input_folder
logfile = os.path.join(g_output_folder, 'log.txt')
else:
node_filepath = 'node.csv'
poi_filepath = 'poi.csv'
logfile = 'log.txt'
# create a handler to write the log file
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)
# create a handler to print out in console
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)
# define output format
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add handler to logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.debug('Starting ReadNetworkFiles')
with open(node_filepath, errors='ignore') as fp:
reader = csv.DictReader(fp)
exclude_boundary_node_index = 0
poi_flag = 0
log_flag = 0
index = 0
for line in reader:
node = Node()
node_id = line['node_id']
if node_id:
node.id = int(node_id)
g_node_id_to_index[node.id] = index
else:
# print('Error: node_id is not defined in node.csv, please check it!')
logger.error("node_id is not defined in node.csv, please check it!")
sys.exit(0)
osm_node_id = line['osm_node_id']
if osm_node_id:
node.osm_node_id = str(osm_node_id)
else:
node.osm_node_id = None
activity_type = line['activity_type']
if activity_type:
node.activity_type = str(activity_type)
if node.activity_type == 'residential':
node.activity_location_tab = 'residential'
if node.activity_type == 'poi':
node.activity_location_tab = 'poi'
if node.activity_type == 'centroid node':
continue
x_coord = line['x_coord']
if x_coord:
node.x_coord = round(float(x_coord),7)
else:
# print('Error: x_coord is not defined in node.csv, please check it!')
logger.error("x_coord is not defined in node.csv, please check it!")
sys.exit(0)
y_coord = line['y_coord']
if y_coord:
node.y_coord = round(float(y_coord),7)
else:
# print('Error: y_coord is not defined in node.csv, please check it!')
logger.error("y_coord is not defined in node.csv, please check it!")
sys.exit(0)
poi_id = line['poi_id']
if poi_id:
node.poi_id = str(poi_id)
try:
# print(int(float(poi_id)))
poi_flag = int(float(poi_id)) # comments: = 1 if poi_id exists
except:
continue
boundary_flag = line['is_boundary']
if boundary_flag:
node.boundary_flag = int(float(boundary_flag))
if node.boundary_flag == 1:
node.activity_location_tab = 'boundary'
else:
logger.info("is_boundary is not defined in node.csv. Default value is 0.")
node.boundary_flag = 0
g_node_id_to_node[node.id] = node
g_node_list.append(node)
if node.boundary_flag == 1:
g_boundary_node_list.append(node)
else:
g_outside_boundary_node_list.append(node) # comments: outside boundary node
g_outside_boundary_node_id_index[node.id] = exclude_boundary_node_index
exclude_boundary_node_index += 1
index += 1
if poi_flag == 0:
if (log_flag == 0):
print('Field poi_id is not in node.csv. Please check it!')
logger.warning('Field poi_id is NOT defined in node.csv. Please check node.csv! \
It could lead to empty demand volume and zero agent. \
Please ensure to POIs=True in osm2gmns: \
net = og.getNetFromOSMFile')
log_flag = 1
try:
g_latitude_list = [node.y_coord for node in g_node_list]
g_average_latitude = sum(g_latitude_list) / len(g_latitude_list)
except:
g_average_latitude = 99
print('Please check y_coord in node.csv!')
logger.warning('Please check y_coord in node.csv!')
with open(poi_filepath, errors='ignore') as fp:
reader = csv.DictReader(fp)
for line in reader:
poi = POI()
poi_id = line['poi_id']
if poi_id:
poi.id = int(poi_id)
else:
logger.error("poi_id is not defined in poi.csv, please check it!")
sys.exit(0)
centroid = line['centroid']
if centroid:
temp_centroid = str(centroid)
else:
# print('Error: centroid is not defined in poi.csv, please check it!')
logger.error("centroid is not defined in poi.csv, please check it!")
sys.exit(0)
str_centroid = temp_centroid.replace('POINT (', '').replace(')', '').replace(' ', ';').strip().split(';')
x_coord = str_centroid[0]
if x_coord:
poi.x_coord = float(x_coord)
else:
# print('Error: x_coord is not defined in poi.csv, please check it!')
logger.error("x_coord is not defined in poi.csv, please check it!")
sys.exit(0)
y_coord = str_centroid[1]
if y_coord:
poi.y_coord = float(y_coord)
else:
# print('Error: y_coord is not defined in poi.csv, please check it!')
logger.error("y_coord is not defined in poi.csv, please check it!")
sys.exit(0)
area = line['area']
if area:
area_meter = float(area)
else:
# print('Error: area is not defined in poi.csv, please check it!')
logger.error('area is not defined for POI No.' + str(poi_id) + ' in poi.csv, please check it!')
sys.exit(0)
if area_meter > 90000: # comments: a simple benchmark to exclude extra large poi nodes
poi.area = 0
area_feet = 0
else:
poi.area = area_meter
area_feet = area_meter * 10.7639104 # comments: convert the area in square meters to square feet
# poi.area = area_feet # comments: convert the area unit of normal poi nodes to square feet
building = line['building']
if building:
poi.type = str(building)
g_poi_id_area_dict[poi.id] = area_feet
g_poi_id_type_dict[poi.id] = poi.type
g_poi_list.append(poi)
g_poi_map[poi.id] = g_poi_list.index(poi)
logger.debug('Ending ReadNetworkFiles')
"""PART 2 GRID GENERATION"""
g_zone_list = []
g_number_of_zones = 0
g_zone_id_list = []
g_zone_index_dict = {}
g_node_zone_dict = {}
g_poi_zone_dict = {}
g_used_latitude = 0
g_scale_list = [0.006, 0.005, 0.004, 0.003, 0.002, 0.001] # comments: default the scale for each grid zone
g_degree_length_dict = {60: 55.8, 51: 69.47, 45: 78.85, 30: 96.49,
0: 111.3}
# comments: default longitudinal length (km) equivalent at selected latitude
alphabet_list = []
for letter in range(65, 91):
alphabet_list.append(chr(letter))
def PartitionGrid(number_of_x_blocks=None,
number_of_y_blocks=None,
cell_width=None,
cell_height=None,
latitude=None,
connector=True):
logger.debug('Starting PartitionGrid')
# Error: Given grid scales and number of blocks simultaneously
if ((number_of_x_blocks is not None) and (number_of_y_blocks is not None) \
and (cell_width is not None) and (cell_height is not None)):
logger.error('Grid scales and number of blocks can only choose ONE to customize!')
sys.exit(0)
global g_number_of_zones
global g_zone_id_list
global g_zone_index_dict
global g_node_zone_dict
global g_used_latitude
global g_average_latitude
# initialize parameters
x_max = max(node.x_coord for node in g_outside_boundary_node_list)
x_min = min(node.x_coord for node in g_outside_boundary_node_list)
y_max = max(node.y_coord for node in g_outside_boundary_node_list)
y_min = min(node.y_coord for node in g_outside_boundary_node_list)
if latitude is None: # use the average latitude according to node.csv
if g_average_latitude == 99:
latitude = 30 # comments: default value if no given latitude value
flat_length_per_degree_km = g_degree_length_dict[latitude]
g_used_latitude = latitude
logger.warning('Please check y_coord in node.csv! Default latitude is 30 degree!')
else:
# match the closest latitude key according to the given latitude
dif = float('inf')
for i in g_degree_length_dict.keys():
if abs(abs(g_average_latitude) - i) < dif:
temp_latitude = i
dif = abs(abs(g_average_latitude) - i)
g_used_latitude = temp_latitude
flat_length_per_degree_km = g_degree_length_dict[temp_latitude]
else: # use the given latitude
# match the closest latitude key according to the given latitude
dif = float('inf')
for i in g_degree_length_dict.keys():
if abs(abs(latitude) - i) < dif:
temp_latitude = i
dif = abs(abs(latitude) - i)
g_used_latitude = temp_latitude
flat_length_per_degree_km = g_degree_length_dict[temp_latitude]
print('\nLatitude used for grid partition = ', g_used_latitude)
logger.info('Latitude used for grid partition = ' + str(g_used_latitude))
# Case 0: Default
if (number_of_x_blocks is None) and (number_of_y_blocks is None) \
and (cell_width is None) and (cell_height is None):
logger.warning('Default cell width and height are the length on a flat surface under a specific latitude '
'corresponding to the degree of 0.006!')
scale_x = g_scale_list[0]
scale_y = g_scale_list[0]
x_max = math.ceil(x_max / scale_x) * scale_x
x_min = math.floor(x_min / scale_x) * scale_x
y_max = math.ceil(y_max / scale_y) * scale_y
y_min = math.floor(y_min / scale_y) * scale_y
number_of_x_blocks = round((x_max - x_min) / scale_x)
number_of_y_blocks = round((y_max - y_min) / scale_y)
# Case 1: Given number_of_x_blocks and number_of_y_blocks
if (number_of_x_blocks is not None) and (number_of_y_blocks is not None) \
and (cell_width is None) and (cell_height is None):
scale_x = round((x_max - x_min) / number_of_x_blocks, 5) + 0.00001
scale_y = round((y_max - y_min) / number_of_y_blocks, 5) + 0.00001
x_max = round(x_min + scale_x * number_of_x_blocks, 5)
y_min = round(y_max - scale_y * number_of_y_blocks, 5)
# Case 2: Given scale_x and scale_y in meter
if (number_of_x_blocks is None) and (number_of_y_blocks is None) \
and (cell_width is not None) and (cell_height is not None):
scale_x = round(cell_width / (1000 * flat_length_per_degree_km), 5)
scale_y = round(cell_height / (1000 * flat_length_per_degree_km), 5)
x_max = round(math.ceil(x_max / scale_x) * scale_x, 5)
x_min = round(math.floor(x_min / scale_x) * scale_x, 5)
y_max = round(math.ceil(y_max / scale_y) * scale_y, 5)
y_min = round(math.floor(y_min / scale_y) * scale_y, 5)
number_of_x_blocks = round((x_max - x_min) / scale_x)
number_of_y_blocks = round((y_max - y_min) / scale_y)
block_numbers = number_of_x_blocks * number_of_y_blocks
x_temp = round(x_min, 5)
y_temp = round(y_max, 5)
for block_no in range(1, block_numbers + 1):
block = Zone()
block.id = block_no
block.x_min = x_temp
block.x_max = x_temp + scale_x
block.y_max = y_temp
block.y_min = y_temp - scale_y
for node in g_outside_boundary_node_list:
if ((node.x_coord <= block.x_max) & (node.x_coord >= block.x_min) \
& (node.y_coord <= block.y_max) & (node.y_coord >= block.y_min)) and \
(node.activity_type == 'poi' or node.activity_type == 'residential'):
node.zone_id = str(block.id)
g_node_zone_dict[node.id] = block.id
block.node_id_list.append(node.id)
for poi in g_poi_list:
if ((poi.x_coord <= block.x_max) & (poi.x_coord >= block.x_min) \
& (poi.y_coord <= block.y_max) & (poi.y_coord >= block.y_min)):
poi.zone_id = str(block.id)
g_poi_zone_dict[poi.id] = block.id
block.poi_id_list.append(poi.id)
block.poi_node_list.append(poi)
# get centroid coordinates of each zone with nodes by calculating average x_coord and y_coord
if len(block.node_id_list) != 0:
block.poi_count = len(block.poi_id_list) # number of all poi nodes in the zone
block.residential_count = 0 # number of residential poi nodes in the zone
block.office_count = 0 # number of office poi nodes in the zone
block.shopping_count = 0 # number of shopping poi nodes in the zone
block.school_count = 0 # number of school poi nodes in the zone
block.parking_count = 0 # number of parking poi nodes in the zone
for poi in block.poi_node_list:
if poi.type == 'apartments' or poi.type == 'dormitory' or poi.type == 'house' or poi.type == 'residential':
block.residential_count += 1 # number of residential poi nodes in the zone
elif poi.type == 'office' or poi.type == 'industrial':
block.office_count += 1 # number of office poi nodes in the zone
elif poi.type == 'commercial' or poi.type == 'retail' or poi.type == 'supermarket' or poi.type == 'warehouse':
block.shopping_count += 1
elif poi.type == 'school' or poi.type == 'kindergarten' or poi.type == 'university' \
or poi.type == 'college' or poi.type == 'university;yes':
block.school_count += 1
elif poi.type == 'parking' or poi.type == 'garage' or poi.type == 'garages' or poi.type == 'bicycle_parking':
block.parking_count += 1 # number of parking poi nodes in the zone
block.centroid_x = sum(g_outside_boundary_node_list[g_outside_boundary_node_id_index[node_id]].x_coord for
node_id in block.node_id_list) / len(block.node_id_list)
block.centroid_y = sum(g_outside_boundary_node_list[g_outside_boundary_node_id_index[node_id]].y_coord for
node_id in block.node_id_list) / len(block.node_id_list)
str_name_a = str(alphabet_list[math.ceil(block.id / number_of_x_blocks) - 1])
if int(block.id % number_of_x_blocks) != 0:
str_name_no = str(int(block.id % number_of_x_blocks))
else:
str_name_no = str(number_of_x_blocks)
block.name = str_name_a + str_name_no
str_polygon = 'POLYGON ((' + \
str(block.x_min) + ' ' + str(block.y_min) + ',' + \
str(block.x_min) + ' ' + str(block.y_max) + ',' + \
str(block.x_max) + ' ' + str(block.y_max) + ',' + \
str(block.x_max) + ' ' + str(block.y_min) + ',' + \
str(block.x_min) + ' ' + str(block.y_min) + '))'
block.polygon = str_polygon
str_centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.centroid = str_centroid
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
# centroid of each zone with zero node is the center point of the grid
if (len(block.node_id_list) == 0):
block.poi_count = 0 # number of all poi nodes in the zone
block.residential_count = 0 # number of residential poi nodes in the zone
block.office_count = 0 # number of office poi nodes in the zone
block.shopping_count = 0 # number of shopping poi nodes in the zone
block.school_count = 0 # number of school poi nodes in the zone
block.parking_count = 0 # number of parking poi nodes in the zone
block.centroid_x = (block.x_max + block.x_min) / 2
block.centroid_y = (block.y_max + block.y_min) / 2
str_name_a = str(alphabet_list[math.ceil(block.id / number_of_x_blocks) - 1])
if int(block.id % number_of_x_blocks) != 0:
str_name_no = str(int(block.id % number_of_x_blocks))
else:
str_name_no = str(number_of_x_blocks)
block.name = str_name_a + str_name_no
str_polygon = 'POLYGON ((' + \
str(block.x_min) + ' ' + str(block.y_min) + ',' + \
str(block.x_min) + ' ' + str(block.y_max) + ',' + \
str(block.x_max) + ' ' + str(block.y_max) + ',' + \
str(block.x_max) + ' ' + str(block.y_min) + ',' + \
str(block.x_min) + ' ' + str(block.y_min) + '))'
block.polygon = str_polygon
str_centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.centroid = str_centroid
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
if round(abs(x_temp + scale_x - x_max) / scale_x) >= 1:
x_temp = x_temp + scale_x
else:
x_temp = x_min
y_temp = y_temp - scale_y
# generate the grid address for boundary nodes and generate virtual zones around the boundary of the area
# left side virtual zones
i = 1
delta_y = 0
while i <= number_of_y_blocks:
block = Zone()
block.id = block_numbers + i
block.name = 'Gate' + str(i)
block.x_min = x_min - scale_x / 2
block.x_max = x_min
block.y_max = y_min + delta_y + scale_y
block.y_min = y_min + delta_y
block.centroid_x = block.x_min
block.centroid_y = (block.y_max + block.y_min) / 2
block.centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.polygon = ''
block.poi_id_list = []
block.boundary_count = 0
for node in g_boundary_node_list:
if abs(node.x_coord - x_min) == min(abs(node.x_coord - x_max), abs(node.x_coord - x_min),
abs(node.y_coord - y_max), abs(node.y_coord - y_min)) \
and block.y_max >= node.y_coord >= block.y_min and node.boundary_flag == 1:
node.zone_id = str(block.id)
g_node_zone_dict[node.id] = block.id
block.node_id_list.append(node.id)
block.boundary_count += 1
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
delta_y += scale_y
i += 1
# upper side virtual zones
i = number_of_y_blocks + 1
delta_x = 0
while i <= number_of_y_blocks + number_of_x_blocks:
block = Zone()
block.id = block_numbers + i
block.name = 'Gate' + str(i)
block.x_min = x_min + delta_x
block.x_max = x_min + delta_x + scale_x
block.y_max = y_max + scale_y / 2
block.y_min = y_max
block.centroid_x = (block.x_max + block.x_min) / 2
block.centroid_y = block.y_max
block.centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.polygon = ''
block.poi_id_list = []
block.boundary_count = 0
for node in g_boundary_node_list:
if abs(node.y_coord - y_max) == min(abs(node.x_coord - x_max), abs(node.x_coord - x_min),
abs(node.y_coord - y_max), abs(node.y_coord - y_min)) \
and block.x_max >= node.x_coord >= block.x_min and node.boundary_flag == 1:
node.zone_id = str(block.id)
g_node_zone_dict[node.id] = block.id
block.node_id_list.append(node.id)
block.boundary_count += 1
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
i += 1
delta_x += scale_x
# right side virtual zones
i = number_of_y_blocks + number_of_x_blocks + 1
delta_y = 0
while i <= 2 * number_of_y_blocks + number_of_x_blocks:
block = Zone()
block.id = block_numbers + i
block.name = 'Gate' + str(i)
block.x_min = x_max
block.x_max = x_max + scale_x / 2
block.y_max = y_max - delta_y
block.y_min = y_max - delta_y - scale_y
block.centroid_x = block.x_max
block.centroid_y = (block.y_max + block.y_min) / 2
block.centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.polygon = ''
block.poi_id_list = []
block.boundary_count = 0
for node in g_boundary_node_list:
if abs(node.x_coord - x_max) == min(abs(node.x_coord - x_max), abs(node.x_coord - x_min),
abs(node.y_coord - y_max), abs(node.y_coord - y_min)) \
and block.y_max >= node.y_coord >= block.y_min and node.boundary_flag == 1:
node.zone_id = str(block.id)
g_node_zone_dict[node.id] = block.id
block.node_id_list.append(node.id)
block.boundary_count += 1
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
i += 1
delta_y += scale_y
# lower side virtual zones
i = 2 * number_of_y_blocks + number_of_x_blocks + 1
delta_x = 0
while i <= 2 * (number_of_y_blocks + number_of_x_blocks):
block = Zone()
block.id = block_numbers + i
block.name = 'Gate' + str(i)
block.x_min = x_max - delta_x - scale_x
block.x_max = x_max - delta_x
block.y_max = y_min
block.y_min = y_min - scale_y / 2
block.centroid_x = (block.x_max + block.x_min) / 2
block.centroid_y = block.y_min
block.centroid = 'POINT (' + str(block.centroid_x) + ' ' + str(block.centroid_y) + ')'
block.polygon = ''
block.poi_id_list = []
block.boundary_count = 0
for node in g_boundary_node_list:
if abs(node.y_coord - y_min) == min(abs(node.x_coord - x_max), abs(node.x_coord - x_min),
abs(node.y_coord - y_max), abs(node.y_coord - y_min)) \
and block.x_max >= node.x_coord >= block.x_min and node.boundary_flag == 1:
node.zone_id = str(block.id)
g_node_zone_dict[node.id] = block.id
block.node_id_list.append(node.id)
block.boundary_count += 1
centroid_node = Node()
centroid_node.id = 100000 + block.id
centroid_node.zone_id = block.id
centroid_node.x_coord = block.centroid_x
centroid_node.y_coord = block.centroid_y
centroid_node.node_type = 'centroid node'
block.centroid_node = centroid_node
g_zone_list.append(block)
i += 1
delta_x += scale_x
g_number_of_zones = len(g_zone_list)
print('\nNumber of zones including virtual zones = ' + str(g_number_of_zones))
logger.info('Number of zones including virtual zones = ' + str(g_number_of_zones))
g_zone_id_list = [zone.id for zone in g_zone_list]
# get zone index
for i in range(g_number_of_zones):
g_zone_index_dict[g_zone_id_list[i]] = i
# generate the connector
for block in g_zone_list:
centroid_node = block.centroid_node
link_id_temp = 0
for node_id in block.node_id_list:
if (link_id_temp < 50):
if (g_node_list[g_node_id_to_index[node_id]].activity_location_tab in ['residential', 'boundary', 'poi']):
connector_link = Link(link_id = block.id * 100000 + link_id_temp,
from_node_id = node_id,
to_node_id = centroid_node.id,
link_type_name = 'connector with ' +
g_node_list[g_node_id_to_index[node_id]].activity_location_tab,
link_type_id = -1)
connector_link.geometry = 'LINESTRING (' + \
str(g_node_list[g_node_id_to_index[node_id]].x_coord) + ' ' + \
str(g_node_list[g_node_id_to_index[node_id]].y_coord) + ' ' + ',' + \
str(centroid_node.x_coord) + ' ' + \
str(centroid_node.y_coord) + ')'
block.connector_list.append(connector_link)
link_id_temp = link_id_temp + 1
else:
break
with open('connector.csv', 'w', newline='') as outfile:
writer = csv.writer(outfile)
line = ['name','link_id','osm_way_id','from_node_id','to_node_id','dir_flag','length','lanes','free_speed',\
'capacity','link_type_name','link_type','geometry','allowed_uses','from_biway']
writer.writerow(line)
for block in g_zone_list:
for connector_link in block.connector_list:
line = [ \
'', # name
connector_link.link_id, # link_id
'', # osm_way_id
connector_link.from_node_id, # from_node_id
connector_link.to_node_id, # to_node_id
'', # dir_flag
'', # length
'', # lanes
'', # free_speed
'', # capacity
connector_link.link_type_name, # link_type_name
connector_link.link_type_id, # link_type
connector_link.geometry # geometry
]
writer.writerow(line)
# update poi.csv with zone_id
local_encoding = locale.getdefaultlocale()
if g_output_folder is not None:
poi_filepath = os.path.join(g_output_folder, 'poi.csv')
try:
data = pd.read_csv(poi_filepath)
except UnicodeDecodeError:
data = pd.read_csv(poi_filepath, encoding=local_encoding[1])
data_list = [poi.zone_id for poi in g_poi_list]
data1 = pd.DataFrame(data_list)
data['activity_zone_id'] = data1
data_list = [poi.area for poi in g_poi_list]
data1 = pd.DataFrame(data_list)
data['area'] = data1
# print(data)
data.to_csv(poi_filepath, index=False, line_terminator='\n')
else:
try:
data = pd.read_csv('poi.csv')
except UnicodeDecodeError:
data = pd.read_csv('poi.csv', encoding=local_encoding[1])
data_list = [poi.zone_id for poi in g_poi_list]
data1 = pd.DataFrame(data_list)
data['activity_zone_id'] = data1
# print(data)
data.to_csv('poi.csv', index=False, line_terminator='\n')
# create zone.csv
data_list = [zone.id for zone in g_zone_list]
data_zone = pd.DataFrame(data_list)
data_zone.columns = ["activity_zone_id"]
data_list = [zone.name for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['name'] = data1
data_list = [zone.centroid_x for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['centroid_x'] = data1
data_list = [zone.centroid_y for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['centroid_y'] = data1
data_list = [zone.polygon for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['geometry'] = data1
data_list = [zone.centroid for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['centroid'] = data1
data_list = [zone.poi_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['total_poi_count'] = data1
data_list = [zone.residential_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['residential_poi_count'] = data1
data_list = [zone.office_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['office_poi_count'] = data1
data_list = [zone.shopping_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['shopping_poi_count'] = data1
data_list = [zone.school_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['school_poi_count'] = data1
data_list = [zone.parking_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['parking_poi_count'] = data1
data_list = [zone.boundary_count for zone in g_zone_list]
data1 = pd.DataFrame(data_list)
data_zone['boundary_node_count'] = data1
# print(data_zone)
if g_output_folder is not None:
zone_filepath = os.path.join(g_output_folder, 'zone.csv')
data_zone.to_csv(zone_filepath, index=False, line_terminator='\n')
else:
data_zone.to_csv('zone.csv', index=False, line_terminator='\n')
logger.debug("Ending Partition Grid")
"""PART 3 TRIP GENERATION"""
g_trip_purpose = 0 # comments: this set of global variables are used in the following part 3
g_poi_type_list = []
g_poi_prod_rate_list = []
g_poi_attr_rate_list = []
g_poi_prod_rate_notes_list = []
g_poi_attr_rate_notes_list = []
g_node_prod_list = []
g_node_attr_list = []
g_undefined_prod_rate_poi_name_list = []
g_undefined_attr_rate_poi_name_list = []
g_poi_type_prod_rate_dict = {}
g_poi_type_attr_rate_dict = {}
g_poi_prod_rate_flag = {}
g_poi_attr_rate_flag = {}
g_poi_purpose_prod_dict = defaultdict(defaultdict)
g_poi_purpose_attr_dict = defaultdict(defaultdict)
trip_purpose_list = [1, 2, 3]
g_number_of_unmatched_poi_production_rate = 0
g_number_of_unmatched_poi_attraction_rate = 0
def GetPoiTripRate(trip_rate_folder=None,
trip_purpose=None):
logger.debug("Starting GetPOITripRate")
global g_poi_purpose_prod_dict
global g_poi_purpose_attr_dict
global g_poi_type_list
global g_undefined_prod_rate_poi_name_list
global g_undefined_attr_rate_poi_name_list
global g_poi_type_prod_rate_dict
global g_poi_type_attr_rate_dict
global g_trip_purpose
global g_output_folder
global g_number_of_unmatched_poi_production_rate
global g_number_of_unmatched_poi_attraction_rate
if trip_rate_folder:
trip_rate_filepath = os.path.join(trip_rate_folder, 'poi_trip_rate.csv')
else:
trip_rate_filepath = 'poi_trip_rate.csv'
# define production/attraction rates of each land use type and trip purpose
try:
# user can customize poi_trip_rate.csv in advance
with open(trip_rate_filepath, errors='ignore') as fp:
reader = csv.DictReader(fp)
for line in reader:
poi_type = line['building']
for i in trip_purpose_list:
try:
g_poi_purpose_prod_dict[poi_type][i] = float(line['production_rate' + str(i)])
except:
g_poi_purpose_prod_dict[poi_type][i] = 0
try:
g_poi_purpose_attr_dict[poi_type][i] = float(line['attraction_rate' + str(i)])
except:
g_poi_purpose_attr_dict[poi_type][i] = 0
except:
# print('Warning: The folder of poi_trip_rate.csv is NOT defined! Default values will be used...')
logger.warning('poi_trip_rate.csv does not exist in the current folder. Default values will be used.')
# default trip generation rates refer to ITE Trip Generation Manual, 10t Edition
# https://www.troutdaleoregon.gov/sites/default/files/fileattachments/public_works/page/966/ite_land_use_list_10th_edition.pdf
# unit of measure for all poi nodes is 1,000 SF GFA in this version
g_poi_purpose_prod_dict = {'parking': {trip_purpose_list[0]: 0.43},
'bicycle_parking': {trip_purpose_list[0]: 0.43},
'digester': {trip_purpose_list[0]: 0.4},
'service': {trip_purpose_list[0]: 0.48}, 'college': {trip_purpose_list[0]: 1.17},
'university': {trip_purpose_list[0]: 1.17}, 'school': {trip_purpose_list[0]: 1.37},
'university;yes': {trip_purpose_list[0]: 1.17},
'kindergarten': {trip_purpose_list[0]: 11.12},
'transportation': {trip_purpose_list[0]: 1.72},
'train_station': {trip_purpose_list[0]: 1.72},
'public': {trip_purpose_list[0]: 0.11},
'public_building': {trip_purpose_list[0]: 0.11},
'hospital': {trip_purpose_list[0]: 0.97},
'government': {trip_purpose_list[0]: 1.71},
'administrative/auxiliary': {trip_purpose_list[0]: 1.71},
'fire_station': {trip_purpose_list[0]: 0.48},
'bakehouse': {trip_purpose_list[0]: 28}, 'temple': {trip_purpose_list[0]: 4.22},
'synagogue': {trip_purpose_list[0]: 0.49}, 'shrine': {trip_purpose_list[0]: 4.22},
'religious': {trip_purpose_list[0]: 0.49}, 'mosque': {trip_purpose_list[0]: 4.22},
'monastery': {trip_purpose_list[0]: 4.22}, 'church': {trip_purpose_list[0]: 0.49},
'chapel': {trip_purpose_list[0]: 0.49}, 'cathedral': {trip_purpose_list[0]: 0.49},
'warehouse': {trip_purpose_list[0]: 0.19}, 'retail': {trip_purpose_list[0]: 6.84},
'supermarket': {trip_purpose_list[0]: 9.24}, 'office': {trip_purpose_list[0]: 1.15},
'kiosk': {trip_purpose_list[0]: 7.42}, 'industrial': {trip_purpose_list[0]: 0.63},
'commercial': {trip_purpose_list[0]: 0.63}, 'library': {trip_purpose_list[0]: 1.17},
'childcare': {trip_purpose_list[0]: 11.12}, 'yes': {trip_purpose_list[0]: 1}}
g_poi_purpose_attr_dict = {'apartments': {trip_purpose_list[0]: 0.36}, 'bungalow': {trip_purpose_list[0]: 0.99},
'cabin': {trip_purpose_list[0]: 0.99}, 'detached': {trip_purpose_list[0]: 0.99},
'dormitory': {trip_purpose_list[0]: 0.36}, 'ger': {trip_purpose_list[0]: 0.99},
'hotel': {trip_purpose_list[0]: 0.6}, 'house': {trip_purpose_list[0]: 0.44},
'residential': {trip_purpose_list[0]: 0.36},
'semidetached_house': {trip_purpose_list[0]: 0.99},
'static_caravan': {trip_purpose_list[0]: 0.46},
'terrace': {trip_purpose_list[0]: 0.44}, 'public': {trip_purpose_list[0]: 0.11},
'grandstand': {trip_purpose_list[0]: 0.15}, 'pavilion': {trip_purpose_list[0]: 6.29},
'riding_hall': {trip_purpose_list[0]: 3.45},
'sports_hall': {trip_purpose_list[0]: 3.45}, 'stadium': {trip_purpose_list[0]: 0.15},
'yes': {trip_purpose_list[0]: 1}}
# Get POI production/attraction rates of each poi type with a specific trip purpose
if trip_purpose is None:
# print('Warning: Trip purpose is not defined! Default trip purpose is Purpose 1.')
logger.warning('Trip purpose is not defined! Default trip purpose is Purpose 1.')
trip_purpose = trip_purpose_list[0] # default trip purpose is Purpose 1
g_trip_purpose = trip_purpose
else:
g_trip_purpose = trip_purpose
poi_id = [poi.id for poi in g_poi_list]
poi_type = [poi.type for poi in g_poi_list]
for i in range(len(poi_id)):
# define production rate of each poi type
if (poi_type[i] in g_poi_purpose_prod_dict):
production_rate = g_poi_purpose_prod_dict[poi_type[i]][trip_purpose]
g_poi_type_prod_rate_dict[poi_type[i]] = production_rate