/
MainGUI.py
840 lines (742 loc) · 38.9 KB
/
MainGUI.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
import io
import os
import sys
from collections import OrderedDict
from haversine import haversine
import folium
import json
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QSize, pyqtSlot, Qt
from PyQt5.QtGui import QImage, QPalette, QBrush, QFont, QPixmap, QIcon
from PyQt5.QtWidgets import QApplication, QComboBox, QLabel
from folium.plugins import MarkerCluster
from ShortestPathGUI import ShortestPathGUI
from ShortestPath import ShortestPath
global mapView
#Initialise both starting and ending coordinates
startingCoordinates = None
endingCoordinates = None
class MainGUI(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.title = 'ICT1008 AY19/20 Project Group 2-3'
self.left = 10
self.top = 10
self.width = 800
self.height = 600
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowIcon(QIcon('Images/punggol_logo.jpg'))
bgImage = QImage("Images/punggol_background_1.png")
sBgImage = bgImage.scaled(QSize(1100, 800)) # resize Image to fit the windows size
palette = QPalette()
palette.setBrush(QPalette.Window, QBrush(sBgImage))
self.setPalette(palette)
self.initWidgets()
def initWidgets(self):
#Initialise the main layout
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.mapView = QtWebEngineWidgets.QWebEngineView()
self.initMap(self.m, self.marker_cluster)
# Array that contains the ending locations (Entire nodes that cover Punggol West Area)
startEndingLocation = self.importStartEnding('Combined/nodes.json')
#Array that contains all the bus services that ply around Punggol West (Area-Of-Interest)
busServices = ["Bus 3", "Bus 34", "Bus 43", "Bus 50", "Bus 62", "Bus 82", "Bus 83", "Bus 84", "Bus 85", "Bus 117", "Bus 118", "Bus 119", "Bus 136", "Bus 381", "Bus 382G", "Bus 382W", "Bus 386"]
#Initailises the main layout + grid layout to format the GUI
self.main = QtWidgets.QWidget()
self.setCentralWidget(self.main)
gridLayout = QtWidgets.QGridLayout(self.main)
#Combo Box Layout 1 for starting and ending location
comboLayout1 = QtWidgets.QGridLayout(self.main)
#Starting Location Label
lblStartLocation = QLabel(self)
lblStartLocation.setText('Choose starting location:')
lblStartLocation.setFont(QFont("Arial", 14, QFont.Bold))
lblStartLocation.setStyleSheet('QLabel { color : Green; }')
#Choose a start location (MRT station)
self.comboStart = QComboBox()
self.comboStart.setFont(QFont("Arial", 10))
self.comboStart.addItems(startEndingLocation)
self.comboStart.currentIndexChanged.connect(self.chooseStart)
#Ending location label
lblEndLocation = QLabel(self)
lblEndLocation.setText('Choose ending location:')
lblEndLocation.setFont(QFont("Arial", 14, QFont.Bold))
lblEndLocation.setStyleSheet('QLabel { color : Red; }')
#Choose an ending location (Residential Estates HDB + Condominiums)
self.comboEnd = QComboBox()
self.comboEnd.setFont(QFont("Arial", 10))
self.comboEnd.addItems(startEndingLocation)
self.comboEnd.currentIndexChanged.connect(self.chooseEnd)
#Adds widgets to the Combobox layout for starting and ending location
comboLayout1.addWidget(lblStartLocation, 0, 0)
comboLayout1.addWidget(self.comboStart, 1, 0)
comboLayout1.addWidget(lblEndLocation, 0, 1)
comboLayout1.addWidget(self.comboEnd, 1, 1)
gridLayout.addLayout(comboLayout1, 0, 0)
#Button to determine shortest walking bath
btnLayout = QtWidgets.QGridLayout()
btnWalkingPath = QtWidgets.QPushButton(self.tr("Compute shortest walking path"))
btnWalkingPath.setFont(QFont("Arial", 10, QFont.Bold))
btnWalkingPath.setStyleSheet('QPushButton { background-color: #008000; color: white; }')
btnLayout.addWidget(btnWalkingPath , 0, 0)
btnWalkingPath.clicked.connect(self.computeWalking)
#Button to determine fastest bus bath
btnFastestBusPath = QtWidgets.QPushButton(self.tr("Compute fastest bus path"))
btnFastestBusPath.setFont(QFont("Arial", 10, QFont.Bold))
btnFastestBusPath.setStyleSheet('QPushButton { background-color: #008B8B; color: white; }')
btnFastestBusPath.clicked.connect(self.computeFastestBus)
btnLayout.addWidget(btnFastestBusPath, 1, 0)
#Button to determine fastest MRT/train bath
btnFastestTrainPath = QtWidgets.QPushButton(self.tr("Compute fastest train path"))
btnFastestTrainPath.setFont(QFont("Arial", 10, QFont.Bold))
btnFastestTrainPath.setStyleSheet('QPushButton { background-color: #FF0000; color: white; }')
btnFastestTrainPath.clicked.connect(self.computeFastestTrain)
btnLayout.addWidget(btnFastestTrainPath, 2, 0)
gridLayout.addLayout(btnLayout, 1, 0)
#Checkbox to select whether the bus routes are displayed on the map
busLayout = QtWidgets.QGridLayout(self.main)
lblBusRoutes = QLabel(self)
lblBusRoutes.setText('Select a bus route to display on the map:')
lblBusRoutes.setAlignment(Qt.AlignHCenter)
lblBusRoutes.setFont(QFont("Arial", 14, QFont.Bold))
lblBusRoutes.setStyleSheet('QLabel { color : Blue; }')
busLayout.addWidget(lblBusRoutes, 0, 0)
#Checkbox to select a bus service route to display on the map
self.comboBusService = QComboBox(self)
self.comboBusService.setFont(QFont("Arial", 10))
self.comboBusService.addItems(busServices)
self.comboBusService.currentIndexChanged.connect(self.chooseBus)
busLayout.addWidget(self.comboBusService, 1, 0)
#Button to click the bus route to display on the map
btnSelectBus = QtWidgets.QPushButton(self.tr("Display Selected Bus Route"))
btnSelectBus.setFont(QFont("Arial", 10, QFont.Bold))
btnSelectBus.setStyleSheet('QPushButton { background-color: #800080; color: white; }')
btnSelectBus.clicked.connect(self.selectBusService)
busLayout.addWidget(btnSelectBus, 2, 0)
self.lblSelectedBusRoute = QLabel(self)
self.lblSelectedBusRoute.setText('Bus Route Displayed: ')
self.lblSelectedBusRoute.setFont(QFont("Arial", 14, QFont.Bold))
self.lblSelectedBusRoute.setStyleSheet('QLabel { color : Blue; }')
busLayout.addWidget(self.lblSelectedBusRoute, 3, 0)
gridLayout.addLayout(busLayout, 0, 2, 2, 1)
#Layout to display the different types of legends of the map
legendLayout = QtWidgets.QGridLayout()
lblLegend = QLabel(self)
lblLegend.setText('Legend:')
lblLegend.setFont(QFont("Arial", 14, QFont.Bold))
lblLegend.setStyleSheet('QLabel { color : #CC6600; }')
legendLayout.addWidget(lblLegend, 0, 0)
lrtLogo = QLabel(self)
pixmapLRT = QPixmap.fromImage(QImage('Images/lrt_logo.png').scaled(20, 25, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
lrtLogo.setPixmap(pixmapLRT.scaled(20, 25))
legendLayout.addWidget(lrtLogo, 1, 0)
lblLRT = QLabel(self)
lblLRT.setText('LRT Station')
lblLRT.setFont(QFont("Arial", 12, QFont.Bold))
lblLRT.setStyleSheet('QLabel { color : #5AB331; }')
legendLayout.addWidget(lblLRT, 1, 1, 1, 2)
busLogo = QLabel(self)
pixmapBus = QPixmap.fromImage(QImage('Images/bus_stop_logo.png').scaled(20, 20, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
busLogo.setPixmap(pixmapBus.scaled(20, 20))
legendLayout.addWidget(busLogo, 2, 0)
lblBus = QLabel(self)
lblBus.setText('Bus Stop')
lblBus.setFont(QFont("Arial", 12, QFont.Bold))
lblBus.setStyleSheet('QLabel { color : #1AA0E1; }')
legendLayout.addWidget(lblBus, 2, 1, 1, 2)
buildingLogo = QLabel(self)
pixmapBuilding = QPixmap.fromImage(QImage('Images/building_logo.png').scaled(20, 20, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
buildingLogo.setPixmap(pixmapBuilding.scaled(20, 20))
legendLayout.addWidget(buildingLogo, 3, 0)
lblBuilding = QLabel(self)
lblBuilding.setText('General Building')
lblBuilding.setFont(QFont("Arial", 12, QFont.Bold))
lblBuilding.setStyleSheet('QLabel { color : #FFA40B; }')
legendLayout.addWidget(lblBuilding, 3, 1, 1, 2)
houseLogo = QLabel(self)
pixmapBuilding = QPixmap.fromImage(QImage('Images/home_logo.png').scaled(20, 20, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
houseLogo.setPixmap(pixmapBuilding.scaled(20, 20))
legendLayout.addWidget(houseLogo, 4, 0)
lblHouse = QLabel(self)
lblHouse.setText('Residential Housing')
lblHouse.setFont(QFont("Arial", 12, QFont.Bold))
lblHouse.setStyleSheet('QLabel { color : #6782A4; }')
legendLayout.addWidget(lblHouse, 4, 1, 1, 2)
gridLayout.addLayout(legendLayout, 2, 2, 1, 1)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
gridLayout.addWidget(self.mapView, 2, 0, 2, 1)
def importStartEnding(self, file):
ending = []
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
#retrieve and populate the names of the nodes from json file
for feature_data in feature_access:
ending.append(feature_data['properties']['node-details'])
return ending
def initMap(self, map, markerCluster):
#This is to import the area of interest polyline from json file to display on folium map
self.importAreaOfInterest('Combined/area_of_interest.json', map)
#This is to import the train stations from json file to display on folium map (starting location)
self.importTrainStns('MRT/mrt.json', markerCluster)
#This is to import the bus stops from json file to display on folium map
self.importBusStops('Bus_Stops/bus_stops.json', map)
#This is to import the general buildings from json file to display on folium map
self.importBuildings('Buildings/general_buildings.json', map)
# This is to import the residential buildings from json file to display on folium map (ending location)
self.importResidential('Buildings/residential_buildings.json', map)
def importAreaOfInterest(self, file, map):
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
coordinates = [[]]
for feature_data in feature_access:
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="blue", weight=3).add_to(map)
def importTrainStns(self, file, markerCluster):
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
coordinates = feature_data['geometry']['coordinates']
coordinates[0], coordinates[1] = coordinates[1], coordinates[0]
icon = folium.features.CustomIcon('Images/lrt_logo.png', icon_size=(20, 25))
folium.Marker(coordinates, popup=prop['node-details'], icon=icon).add_to(markerCluster)
def importBusStops(self, file, map):
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
coordinates = feature_data['geometry']['coordinates']
coordinates[0], coordinates[1] = coordinates[1], coordinates[0]
icon = folium.features.CustomIcon('Images/bus_stop_logo.png', icon_size=(20, 20))
folium.Marker(coordinates, popup=prop['node-details'], icon=icon).add_to(map)
def importBuildings(self, file, map):
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
coordinates = feature_data['geometry']['coordinates']
coordinates[0], coordinates[1] = coordinates[1], coordinates[0]
icon = folium.features.CustomIcon('Images/building_logo.png', icon_size=(20, 20))
folium.Marker(coordinates, popup=prop['node-details'], icon=icon).add_to(map)
def importResidential(self, file, map):
with open(file) as f:
self.getJson = json.load(f)
feature_access = self.getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
coordinates = feature_data['geometry']['coordinates']
coordinates[0], coordinates[1] = coordinates[1], coordinates[0]
icon = folium.features.CustomIcon('Images/home_logo.png', icon_size=(20, 20))
folium.Marker(coordinates, popup=prop['node-details'], icon=icon).add_to(map)
#Function to choose different starting locations
def chooseStart(self, i):
print("Current index", i, "selection changed ", self.comboStart.currentText())
#Function to choose different ending locations
def chooseEnd(self, i):
print("Current index", i, "selection changed ", self.comboEnd.currentText())
def chooseBus(self, i):
print("Current index", i, "selection changed ", self.comboBusService.currentText())
#Function to choose different bus service to display on the map
def selectBusService(self):
self.lblSelectedBusRoute.setText('Bus Route Displayed: ' + self.comboBusService.currentText())
if self.comboBusService.currentText() == "Bus 3":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_3_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 34":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_34_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 43":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_43_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 50":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_50_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 62":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_62_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 82":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_82_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 83":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_83_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 84":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_84.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 85":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_85_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 117":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_117_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 118":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_118_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 119":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_119_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 136":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_136_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 381":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_381_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 382G":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_382G.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="green", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 382W":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_382W.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="red", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if self.comboBusService.currentText() == "Bus 386":
coordinates = [[]]
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
with open("Bus_Services/Bus_386_from.json") as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
type = feature_data['geometry']['type']
if type == 'LineString':
coordinates = feature_data['geometry']['coordinates']
for c in coordinates:
c[0], c[1] = c[1], c[0]
folium.PolyLine(coordinates, color="#800080", weight=3).add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
@pyqtSlot()
def computeWalking(self):
#initialise dictionary
nodes = {}
#opens up the combined nodes json file to retrieve the json keys
with open('Combined/nodes.json') as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
if 'node-details' in prop:
location_name = prop['node-details']
#add all the node details such as the name as key and coordinates as value into dictionary
nodes[location_name] = feature_data['geometry']['coordinates']
findPath = ShortestPath(nodes)
findPath.createEdges()
graph = findPath.buildAGraph()
path = findPath.findShortestPath(graph, self.comboStart.currentText(), self.comboEnd.currentText())
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
self.lblSelectedBusRoute.setText('Bus Route Displayed: ')
folium.PolyLine(path, opacity=1, color='green').add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
self.walkingPath = ShortestPathGUI(path)
self.walkingPath.show()
@pyqtSlot()
def computeFastestBus(self):
# Fastest Bus Route
nodes = OrderedDict()
edges = []
busPath = []
busRoutes = OrderedDict()
busNodes = {}
temp = {}
#Retrieve all the json files under Bus_Path directory
path_to_json = "Bus_Path/"
#Referenced from: https://stackoverflow.com/questions/30539679/python-read-several-json-files-from-a-folder
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.geojson')]
for (index, name) in enumerate(json_files):
with open(path_to_json + str(name)) as json_file:
data = json.load(json_file)
for feature in data['features']:
#Added all the coordinates that follow the bus path into busPath list
if feature['geometry']['type'] == 'MultiLineString':
for y in feature['geometry']['coordinates']:
busPath.append(y)
#Added all the nodes that are placed on the maop into busNodes list
else:
coordinates = feature['geometry']['coordinates']
nodes[feature['properties']['node-details']] = coordinates
print("Nodes: " + str(coordinates))
busNodes[tuple(coordinates)] = feature['properties']['node-details']
lowest = 1000
i = 0
while i < len(busPath):
distance = haversine(coordinates, busPath[i])
if distance < lowest:
lowest = distance
lowestIndex = i
i += 1
busPath.insert(lowestIndex, coordinates)
length = len(busPath)
for i in range(length):
c = tuple(busPath[i])
k = str(i)
busRoutes[k] = c
temp[c] = k
#Added for overall edges for the other node coordinates to find out the fastest path based on speed
for i in range(length):
if i + 1 != length:
distance = haversine(busPath[i], busPath[i + 1])
if tuple(busPath[i]) in busNodes:
edges.append((busNodes[tuple(busPath[i])], temp[tuple(busPath[i + 1])], distance / 60, "Bus"))
elif tuple(busPath[i + 1]) in busNodes:
edges.append((temp[tuple(busPath[i])], busNodes[tuple(busPath[i + 1])], distance / 60, "Bus"))
else:
edges.append((temp[tuple(busPath[i])], temp[tuple(busPath[i + 1])], distance / 60, "Bus"))
temp.clear()
busPath.clear()
with open('Combined/nodes.json') as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
if 'node-details' in prop:
location_name = prop['node-details']
nodes[location_name] = feature_data['geometry']['coordinates']
findPath = ShortestPath(nodes)
findPath.createEdges()
findPath.createBusEdgeNodes(edges, busNodes, busRoutes)
graph = findPath.buildAGraph()
print("Get graph: " + str(graph))
path = findPath.findShortestPath(graph, self.comboStart.currentText(), self.comboEnd.currentText())
print("Get Path: " + str(path))
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
self.lblSelectedBusRoute.setText('Bus Route Displayed: ')
folium.PolyLine(path, opacity=1, color='#800080').add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
@pyqtSlot()
def computeFastestTrain(self):
nodes = OrderedDict()
edges = []
mrtPath = []
mrtRoutes = OrderedDict()
mrtNodes = {}
temp = {}
# Retrieve all the json files under MRT directory
path_to_json = "MRT/"
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.geojson')]
for (index, name) in enumerate(json_files):
with open(path_to_json + str(name)) as json_file:
data = json.load(json_file)
for feature in data['features']:
#Added all the coordinates that follow the mrt path into mrtPath list
if feature['geometry']['type'] == 'MultiLineString':
for y in feature['geometry']['coordinates']:
mrtPath.append(y)
else:
coordinates = feature['geometry']['coordinates']
nodes[feature['properties']['node-details']] = coordinates
mrtNodes[tuple(coordinates)] = feature['properties']['node-details']
lowest = 1000
lowestIndex = 0
i = 0
while i < len(mrtPath):
distance = haversine(coordinates, mrtPath[i])
if distance < lowest:
lowest = distance
lowestIndex = i
i += 1
mrtPath.insert(lowestIndex, coordinates)
length = len(mrtPath)
for i in range(length):
c = tuple(mrtPath[i])
k = str(i)
mrtRoutes[k] = c
temp[c] = k
for i in range(length):
if i + 1 != length:
distance = haversine(mrtPath[i], mrtPath[i + 1])
if tuple(mrtPath[i]) in mrtNodes:
edges.append((mrtNodes[tuple(mrtPath[i])], temp[tuple(mrtPath[i + 1])], distance / 70, "LRT"))
elif tuple(mrtPath[i + 1]) in mrtNodes:
edges.append((temp[tuple(mrtPath[i])], mrtNodes[tuple(mrtPath[i + 1])], distance / 70, "LRT"))
else:
edges.append((temp[tuple(mrtPath[i])], temp[tuple(mrtPath[i + 1])], distance / 70, "LRT"))
temp.clear()
mrtPath.clear()
with open('Combined/nodes.json') as f:
getJson = json.load(f)
feature_access = getJson['features']
for feature_data in feature_access:
prop = feature_data['properties']
if 'node-details' in prop:
location_name = prop['node-details']
nodes[location_name] = feature_data['geometry']['coordinates']
findPath = ShortestPath(nodes)
findPath.createEdges()
findPath.createMrtEdgeNodes(edges, mrtNodes, mrtRoutes)
graph = findPath.buildAGraph()
print("Get graph: " + str(graph))
path = findPath.findShortestPath(graph, self.comboStart.currentText(), self.comboEnd.currentText())
print("Get Path: " + str(path))
self.m = folium.Map(location=[1.4053, 103.9021], zoom_start=16)
self.lblSelectedBusRoute.setText('Bus Route Displayed: ')
folium.PolyLine(path, opacity=1, color='red').add_to(self.m)
self.marker_cluster = MarkerCluster().add_to(self.m)
self.initMap(self.m, self.marker_cluster)
data = io.BytesIO()
self.m.save(data, close_file=False)
self.mapView.setHtml(data.getvalue().decode())
if __name__ == "__main__":
App = QApplication(sys.argv)
mainWindow = MainGUI()
sys.exit(App.exec_())