-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patheditor-tools-view.component.ts
More file actions
834 lines (758 loc) · 31.3 KB
/
editor-tools-view.component.ts
File metadata and controls
834 lines (758 loc) · 31.3 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
import {parse, ParseResult} from "papaparse";
import {Component, ElementRef, ViewChild} from "@angular/core";
import * as svg from "save-svg-as-png";
import {DataService} from "../../services/data/data.service";
import {TrainrunService} from "../../services/data/trainrun.service";
import {NodeService} from "../../services/data/node.service";
import {FilterService} from "../../services/ui/filter.service";
import {TrainrunSectionService} from "../../services/data/trainrunsection.service";
import {UiInteractionService} from "../../services/ui/ui.interaction.service";
import {StammdatenService} from "../../services/data/stammdaten.service";
import {LogService} from "../../logger/log.service";
import {VersionControlService} from "../../services/data/version-control.service";
import {
Direction,
HaltezeitFachCategories,
NetzgrafikDto,
NodeDto,
TrainrunCategoryHaltezeit,
TrainrunSectionDto,
} from "../../data-structures/business.data.structures";
import {downloadBlob} from "../util/download-utils";
import {map} from "rxjs/operators";
import {LabelService} from "../../services/data/label.service";
import {NetzgrafikColoringService} from "../../services/data/netzgrafikColoring.service";
import {ViewportCullService} from "../../services/ui/viewport.cull.service";
import {LevelOfDetailService} from "../../services/ui/level.of.detail.service";
import {TrainrunSectionValidator} from "../../services/util/trainrunsection.validator";
import {OriginDestinationService} from "src/app/services/analytics/origin-destination/components/origin-destination.service";
import {EditorMode} from "../editor-menu/editor-mode";
import {NODE_TEXT_AREA_HEIGHT, RASTERING_BASIC_GRID_SIZE} from "../rastering/definitions";
import {ResourceService} from "../../services/data/resource.service";
interface ContainertoExportData {
documentToExport: HTMLElement;
exportParameter: any;
essentialProps: string[];
}
@Component({
selector: "sbb-editor-tools-view-component",
templateUrl: "./editor-tools-view.component.html",
styleUrls: ["./editor-tools-view.component.scss"],
})
export class EditorToolsViewComponent {
@ViewChild("stammdatenFileInput", {static: false})
stammdatenFileInput: ElementRef;
@ViewChild("netgrafikJsonFileInput", {static: false})
netgrafikJsonFileInput: ElementRef;
public isDeletable$ = this.versionControlService.variant$.pipe(map((v) => v?.isDeletable));
public isWritable$ = this.versionControlService.variant$.pipe(map((v) => v?.isWritable));
constructor(
private dataService: DataService,
private trainrunService: TrainrunService,
private nodeService: NodeService,
public filterService: FilterService,
private trainrunSectionService: TrainrunSectionService,
private uiInteractionService: UiInteractionService,
private stammdatenService: StammdatenService,
private labelService: LabelService,
private logger: LogService,
private versionControlService: VersionControlService,
private netzgrafikColoringService: NetzgrafikColoringService,
private viewportCullService: ViewportCullService,
private levelOfDetailService: LevelOfDetailService,
private originDestinationService: OriginDestinationService,
private resourceService: ResourceService,
) {}
onLoadButton() {
this.netgrafikJsonFileInput.nativeElement.click();
}
onLoad(param) {
const file = param.target.files[0];
const reader = new FileReader();
reader.onload = () => {
let netzgrafikDto: any;
try {
netzgrafikDto = JSON.parse(reader.result.toString());
} catch (err: any) {
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
return;
}
if (netzgrafikDto === undefined) {
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
return;
}
if (
"nodes" in netzgrafikDto &&
"trainrunSections" in netzgrafikDto &&
"trainruns" in netzgrafikDto &&
"resources" in netzgrafikDto &&
"metadata" in netzgrafikDto
) {
this.processNetzgrafikJSON(netzgrafikDto);
return;
}
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-error:JSON error`;
this.logger.error(msg);
};
reader.readAsText(file);
// set the event target value to null in order to be able to load the same file multiple times after one another
param.target.value = null;
}
onSave() {
const data: NetzgrafikDto = this.dataService.getNetzgrafikDto();
const blob = new Blob([JSON.stringify(data)], {type: "application/json"});
downloadBlob(
blob,
$localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafikFile:netzgrafik` +
".json",
);
}
onPrintContainer() {
this.uiInteractionService.closeFilter();
setTimeout(() => {
this.uiInteractionService.print();
}, 1500); // to allow cd-layout-filter to close
}
onExportContainerAsSVG() {
// option 2: save svg as svg
// https://www.npmjs.com/package/save-svg-as-png
this.levelOfDetailService.disableLevelOfDetailRendering();
this.viewportCullService.onViewportChangeUpdateRendering(false);
const containerInfo = this.getContainerToExport();
this.prepareStyleForExport(containerInfo);
// SVG scaling does not affect resolution since SVGs are rendered as vector graphics.
// To ensure a good initial scale, we define the target width as 2000 pixels.
const scaleToTargetWidth = 2000 / containerInfo.exportParameter.width;
containerInfo.exportParameter.scale = scaleToTargetWidth;
svg.svgAsDataUri(containerInfo.documentToExport, containerInfo.exportParameter).then((uri) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.href = uri;
a.download = this.getFilenameToExport() + ".svg";
a.click();
URL.revokeObjectURL(a.href);
a.remove();
this.levelOfDetailService.enableLevelOfDetailRendering();
});
}
onExportContainerAsPNG() {
// option 1: save svg as png
// https://www.npmjs.com/package/save-svg-as-png
this.levelOfDetailService.disableLevelOfDetailRendering();
this.viewportCullService.onViewportChangeUpdateRendering(false);
const containerInfo = this.getContainerToExport();
this.prepareStyleForExport(containerInfo);
svg.saveSvgAsPng(
containerInfo.documentToExport,
this.getFilenameToExport() + ".png",
containerInfo.exportParameter,
);
this.levelOfDetailService.enableLevelOfDetailRendering();
}
onLoadStammdatenButton() {
this.stammdatenFileInput.nativeElement.click();
}
onLoadStammdaten(param) {
const file = param.target.files[0];
const reader = new FileReader();
reader.onload = () => {
const finalResult: ParseResult = parse(reader.result.toString(), {
header: true,
});
this.stammdatenService.setStammdaten(finalResult.data);
};
reader.readAsText(file);
// set the event target value to null in order to be able to load the same file multiple times after one another
param.target.value = null;
}
onExportStammdaten() {
const filename =
$localize`:@@app.view.editor-side-view.editor-tools-view-component.baseDataFile:baseData` +
".csv";
const csvData = this.convertToStammdatenCSV();
this.onExport(filename, csvData);
}
onExportZuglauf() {
const filename =
$localize`:@@app.view.editor-side-view.editor-tools-view-component.trainrunFile:trainrun` +
".csv";
const csvData = this.convertToZuglaufCSV();
this.onExport(filename, csvData);
}
onExportOriginDestination() {
const filename =
$localize`:@@app.view.editor-side-view.editor-tools-view-component.originDestinationFile:originDestination` +
".csv";
const csvData = this.convertToOriginDestinationCSV();
this.onExport(filename, csvData);
}
onExport(filename: string, csvData: string) {
const blob = new Blob([csvData], {
type: "text/csv",
});
const url = window.URL.createObjectURL(blob);
const nav = window.navigator as any;
if (nav.msSaveOrOpenBlob) {
nav.msSaveBlob(blob, filename);
} else {
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
window.URL.revokeObjectURL(url);
}
getVariantIsWritable() {
return this.versionControlService.getVariantIsWritable();
}
getContainerName() {
const editorMode = this.uiInteractionService.getEditorMode();
switch (editorMode) {
case EditorMode.StreckengrafikEditing:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.spaceTimeChart:Space-time chart`;
case EditorMode.OriginDestination:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.originDestination:Origin-destination matrix`;
default:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafik:Netzgrafik`;
}
}
private buildCSVString(headers: string[], rows: string[][]): string {
const separator = ";";
const contentData: string[] = [];
contentData.push(headers.join(separator));
rows.forEach((row) => {
contentData.push(row.join(separator));
});
return contentData.join("\n");
}
private convertToStammdatenCSV(): string {
const comma = ",";
const headers: string[] = [];
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.bp:BP`);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.station:Station`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.category:category`,
);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.region:Region`);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeIPV:passengerConnectionTimeIPV`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeA:Passenger_connection_time_A`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeB:Passenger_connection_time_B`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeC:Passenger_connection_time_C`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.passengerConnectionTimeD:Passenger_connection_time_D`,
);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.ZAZ:ZAZ`);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.transferTime:Transfer_time`,
);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.labels:Labels`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.X:X`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.Y:Y`);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.create:Create`);
const rows: string[][] = [];
this.nodeService.getNodes().forEach((nodeElement) => {
const trainrunCategoryHaltezeit: TrainrunCategoryHaltezeit =
nodeElement.getTrainrunCategoryHaltezeit();
const stammdaten = this.stammdatenService.getBPStammdaten(nodeElement.getBetriebspunktName());
const zaz = stammdaten !== null ? stammdaten.getZAZ() : 0;
const erstellen = stammdaten !== null ? stammdaten.getErstellen() : "JA";
const kategorien = stammdaten !== null ? stammdaten.getKategorien() : [];
const regions = stammdaten !== null ? stammdaten.getRegions() : [];
const row: string[] = [];
row.push(nodeElement.getBetriebspunktName());
row.push(nodeElement.getFullName());
row.push(kategorien.map((kat) => "" + kat).join(comma));
row.push(regions.map((reg) => "" + reg).join(comma));
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.IPV].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.IPV].haltezeit - zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.A].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.A].haltezeit - zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.B].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.B].haltezeit - zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.C].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.C].haltezeit - zaz),
);
row.push(
"" +
(trainrunCategoryHaltezeit[HaltezeitFachCategories.D].no_halt
? 0
: trainrunCategoryHaltezeit[HaltezeitFachCategories.D].haltezeit - zaz),
);
row.push("" + zaz);
row.push("" + nodeElement.getConnectionTime());
row.push(
nodeElement
.getLabelIds()
.map((labelID) => {
const labelOfInterest = this.labelService.getLabelFromId(labelID);
if (labelOfInterest !== undefined) {
return labelOfInterest.getLabel();
}
return "";
})
.join(comma),
);
row.push("" + nodeElement.getPositionX());
row.push("" + nodeElement.getPositionY());
row.push(erstellen);
rows.push(row);
});
return this.buildCSVString(headers, rows);
}
private getStreckengrafikEditingContainerToExport(): ContainertoExportData {
const htmlElementToExport = document.getElementById("main-streckengrafik-container");
const param = {
encoderOptions: 1.0,
scale: 1.0,
left: 0,
top: 0,
width: htmlElementToExport.offsetWidth,
height: htmlElementToExport.offsetHeight,
backgroundColor: this.uiInteractionService.getActiveTheme().backgroundColor,
};
const essentialProps = [
"fill",
"stroke",
"stroke-width",
"stroke-dasharray",
"font-family",
"font-size",
"font-weight",
"opacity",
"text-anchor",
"dominant-baseline",
"width",
"min-width",
"max-width",
"height",
"min-height",
"max-height",
"overflow",
"margin-bottom",
"margin-top",
"margin-left",
"margin-right",
"margin",
"padding",
"display",
"grid-template-columns",
"grid-template-rows",
"grid-gap",
"background",
"background-color",
"border-right",
"border-left",
"border-top",
"border-bottom",
"border",
"box-sizing",
"paint-order",
];
return {
documentToExport: htmlElementToExport,
exportParameter: param,
essentialProps: essentialProps,
};
}
private getOriginDestinationContainerToExport(): ContainertoExportData {
const htmlElementToExport = document.getElementById("main-origin-destination-container");
if (htmlElementToExport === null) {
return undefined;
}
const bbox = (htmlElementToExport as unknown as SVGGElement).getBBox();
const padding = 10;
const param = {
encoderOptions: 1.0,
scale: 1.0,
left: bbox.x - padding,
top: bbox.y - padding,
width: bbox.width + 2 * padding,
height: bbox.height + 2 * padding,
backgroundColor: this.uiInteractionService.getActiveTheme().backgroundColor,
};
const essentialProps = [
"fill",
"stroke",
"stroke-width",
"stroke-dasharray",
"font-family",
"font-size",
"font-weight",
"opacity",
"text-anchor",
"dominant-baseline",
];
return {
documentToExport: htmlElementToExport,
exportParameter: param,
essentialProps: essentialProps,
};
}
private getNetzgrafikEditingContainerToExport(): ContainertoExportData {
const htmlElementToExport = document.getElementById("graphContainer");
if (htmlElementToExport === null) {
return undefined;
}
const boundingBox = this.nodeService.getNetzgrafikBoundingBox();
const param = {
encoderOptions: 1.0,
scale: 1.0,
left: boundingBox.minCoordX - 2.0 * RASTERING_BASIC_GRID_SIZE,
top: boundingBox.minCoordY - 2.0 * RASTERING_BASIC_GRID_SIZE,
width: boundingBox.maxCoordX - boundingBox.minCoordX + 4.0 * RASTERING_BASIC_GRID_SIZE,
height:
boundingBox.maxCoordY -
boundingBox.minCoordY +
4.0 * RASTERING_BASIC_GRID_SIZE +
NODE_TEXT_AREA_HEIGHT,
backgroundColor: this.uiInteractionService.getActiveTheme().backgroundColor,
};
const essentialProps = [
"fill",
"stroke",
"stroke-width",
"stroke-dasharray",
"font-family",
"font-size",
"font-weight",
"opacity",
"text-anchor",
"dominant-baseline",
];
return {
documentToExport: htmlElementToExport,
exportParameter: param,
essentialProps: essentialProps,
};
}
private prepareStyleForExport(containerInfo: ContainertoExportData) {
const element2export = containerInfo.documentToExport;
const elements = element2export.querySelectorAll("*");
elements.forEach((el) => {
const style = window.getComputedStyle(el);
const essentialPropsArray =
containerInfo.essentialProps !== undefined
? containerInfo.essentialProps
: Array.from(style);
const inlineStyle = essentialPropsArray
.map((key) => `${key}:${style.getPropertyValue(key)};`)
.join(" ");
el.setAttribute("style", inlineStyle);
});
}
private getContainerToExport(): ContainertoExportData {
const editorMode = this.uiInteractionService.getEditorMode();
switch (editorMode) {
case EditorMode.StreckengrafikEditing:
return this.getStreckengrafikEditingContainerToExport();
case EditorMode.OriginDestination:
return this.getOriginDestinationContainerToExport();
default:
return this.getNetzgrafikEditingContainerToExport();
}
}
private convertToZuglaufCSV(): string {
const comma = ",";
const headers: string[] = [];
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.trainCategory:Train category`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.trainName:Train name`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.startStation:Start station`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.destinationStation:Destination station`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.trafficPeriod:Traffic period`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.frequence:Frequence`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.departureMinuteAtStart:Minute of departure at start node`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTimeStartDestination:Travel time start-destination`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.arrivalMinuteAtDestination:Arrival minute at destination node`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTimeDestination:Turnaround time at destination station`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.departureMinuteDeparture:Departure minute at destination node`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTimeDestinationStart:Travel time destination-start`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.arrivalMinuteAtStart:Arrival minute at start node`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTimeStart:Turnaround time at start station`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.turnaroundTime:Turnaround time`,
);
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.labels:Labels`);
const rows: string[][] = [];
this.trainrunService
.getTrainruns()
.filter((trainrun) => this.filterService.filterTrainrun(trainrun))
.forEach((trainrun) => {
let startBetriebspunktName = "";
let endBetriebspunktName = "";
// Retrieve start -> end with:
// start {startNode, startTrainrunSection}
// end {iterator.current.node, iterator.current.trainrunSection}
const startNode = this.trainrunService.getLeftOrTopNodeWithTrainrunId(trainrun.getId());
const startTrainrunSection = startNode.getExtremityTrainrunSection(trainrun.getId());
const iterator = this.trainrunService.getIterator(startNode, startTrainrunSection);
while (iterator.hasNext()) {
iterator.next();
}
startBetriebspunktName = startNode.getBetriebspunktName();
endBetriebspunktName = iterator.current().node.getBetriebspunktName();
const departureTimeAtStart =
startTrainrunSection.getSourceNodeId() === startNode.getId()
? startTrainrunSection.getSourceDepartureConsecutiveTime()
: startTrainrunSection.getTargetDepartureConsecutiveTime();
const arrivalTimeAtEnd =
iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId()
? iterator.current().trainrunSection.getSourceArrivalConsecutiveTime()
: iterator.current().trainrunSection.getTargetArrivalConsecutiveTime();
const travelTime = arrivalTimeAtEnd - departureTimeAtStart;
const startNodeDeparture =
startTrainrunSection.getSourceNodeId() === startNode.getId()
? startTrainrunSection.getSourceDeparture()
: startTrainrunSection.getTargetDeparture();
const endNodeArrival =
iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId()
? iterator.current().trainrunSection.getSourceArrival()
: iterator.current().trainrunSection.getTargetArrival();
const endNodeDeparture =
iterator.current().trainrunSection.getSourceNodeId() === iterator.current().node.getId()
? iterator.current().trainrunSection.getSourceDeparture()
: iterator.current().trainrunSection.getTargetDeparture();
const startNodeArrival =
startTrainrunSection.getSourceNodeId() === startNode.getId()
? startTrainrunSection.getSourceArrival()
: startTrainrunSection.getTargetArrival();
let waitingTimeOnStartStation = startNodeDeparture - startNodeArrival;
let waitingTimeOnEndStation = endNodeDeparture - endNodeArrival;
if (trainrun.getFrequency() > 60) {
// special case - if the freq is bigger than 60min (1h) - then just mirror
waitingTimeOnStartStation = 2.0 * (trainrun.getFrequency() / 2.0 - startNodeArrival);
waitingTimeOnEndStation = 2.0 * (trainrun.getFrequency() / 2.0 - endNodeArrival);
} else {
// find next freq (departing)
while (waitingTimeOnStartStation < 0) {
waitingTimeOnStartStation += trainrun.getFrequency();
}
while (waitingTimeOnEndStation < 0) {
waitingTimeOnEndStation += trainrun.getFrequency();
}
}
if (trainrun.getFrequency() < 60) {
waitingTimeOnEndStation = waitingTimeOnEndStation % trainrun.getFrequency();
waitingTimeOnStartStation = waitingTimeOnStartStation % trainrun.getFrequency();
}
const timeOfCirculation =
travelTime + waitingTimeOnEndStation + travelTime + waitingTimeOnStartStation;
const row: string[] = [];
row.push(trainrun.getTrainrunCategory().shortName.trim());
row.push(trainrun.getTitle().trim());
row.push(startBetriebspunktName.trim());
row.push(endBetriebspunktName.trim());
row.push("Verkehrt: " + trainrun.getTrainrunTimeCategory().shortName.trim());
row.push("" + trainrun.getTrainrunFrequency().shortName.trim());
row.push("" + startNodeDeparture);
row.push("" + travelTime);
row.push("" + endNodeArrival);
row.push("" + waitingTimeOnEndStation);
row.push("" + endNodeDeparture);
row.push("" + travelTime);
row.push("" + startNodeArrival);
row.push("" + waitingTimeOnStartStation);
row.push("" + timeOfCirculation);
row.push(
trainrun
.getLabelIds()
.map((labelID) => {
const label = this.labelService.getLabelFromId(labelID);
if (label) {
return label.getLabel().trim();
}
return "";
})
.join(comma),
);
rows.push(row);
});
return this.buildCSVString(headers, rows);
}
private convertToOriginDestinationCSV(): string {
const headers: string[] = [];
headers.push($localize`:@@app.view.editor-side-view.editor-tools-view-component.origin:Origin`);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.destination:Destination`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.travelTime:Travel time`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.transfers:Transfers`,
);
headers.push(
$localize`:@@app.view.editor-side-view.editor-tools-view-component.totalCost:Total cost`,
);
const matrixData = this.originDestinationService.originDestinationData();
const rows = [];
matrixData.forEach((d) => {
if (!d.found) {
rows.push([d.origin, d.destination, "", "", ""]);
return;
}
const row = [
d.origin,
d.destination,
d.travelTime.toString(),
d.transfers.toString(),
d.totalCost.toString(),
];
rows.push(row);
});
return this.buildCSVString(headers, rows);
}
private detectNetzgrafikJSON3rdParty(netzgrafikDto: NetzgrafikDto): boolean {
return (
netzgrafikDto.nodes.find((n: NodeDto) => n.ports === undefined) !== undefined ||
netzgrafikDto.nodes.filter((n: NodeDto) => n.ports?.length === 0).length ===
netzgrafikDto.nodes.length ||
netzgrafikDto.trainrunSections.find(
(ts: TrainrunSectionDto) =>
ts.path === undefined || ts.path?.path === undefined || ts.path?.path?.length === 0,
) !== undefined
);
}
private processNetzgrafikJSON3rdParty(netzgrafikDto: NetzgrafikDto) {
// --------------------------------------------------------------------------------
// 3rd party generated JSON detected
// --------------------------------------------------------------------------------
const msg = $localize`:@@app.view.editor-side-view.editor-tools-view-component.import-netzgrafik-as-json-info-3rd-party:3rd party import`;
this.logger.info(msg);
// --------------------------------------------------------------------------------
// (Step 1) Import only nodes
const netzgrafikOnlyNodeDto: NetzgrafikDto = Object.assign({}, netzgrafikDto);
netzgrafikOnlyNodeDto.trainruns = [];
netzgrafikOnlyNodeDto.trainrunSections = [];
this.dataService.loadNetzgrafikDto(netzgrafikOnlyNodeDto, false, true);
// (Step 2) Import nodes and trainrunSectiosn by trainrun inseration (copy => create)
this.dataService.insertCopyNetzgrafikDto(netzgrafikDto, false);
// step(3) Check whether a transitions object was given when not
// departureTime - arrivatelTime == 0 => non-stop
this.nodeService.getNodes().forEach((n) => {
n.getTransitions().forEach((trans) => {
const p1 = n.getPort(trans.getPortId1());
const p2 = n.getPort(trans.getPortId2());
let arrivalTime = p1.getTrainrunSection().getTargetArrival();
if (p1.getTrainrunSection().getSourceNodeId() === n.getId()) {
arrivalTime = p1.getTrainrunSection().getSourceArrival();
}
let departureTime = p2.getTrainrunSection().getTargetDeparture();
if (p2.getTrainrunSection().getSourceNodeId() === n.getId()) {
departureTime = p2.getTrainrunSection().getSourceDeparture();
}
trans.setIsNonStopTransit(arrivalTime - departureTime === 0);
});
const res = this.resourceService.createAndGetResource(false);
n.setResourceId(res.getId());
});
// step(4) Migrate 3rd party imported trainruns/node/resource to ensure direction is set
this.dataService.ensureAllResourcesLinkedToNetzgrafikObjects();
this.trainrunService.getTrainruns().forEach((t) => {
const currentDirection = t.getDirection();
if (currentDirection === undefined) {
t.setDirection(Direction.ROUND_TRIP);
}
});
// step(5) Recalc/propagate consecutive times
this.trainrunService.propagateInitialConsecutiveTimes();
// step(6) Validate all trainrun sections
this.trainrunSectionService.getTrainrunSections().forEach((ts) => {
TrainrunSectionValidator.validateOneSection(ts);
TrainrunSectionValidator.validateTravelTime(ts, this.filterService.getTimeDisplayPrecision());
});
}
private processNetzgrafikJSON(netzgrafikDto: NetzgrafikDto) {
// prepare JSON import
this.uiInteractionService.showNetzgrafik();
this.uiInteractionService.closeNodeStammdaten();
this.uiInteractionService.closePerlenkette();
this.uiInteractionService.resetEditorMode();
this.nodeService.unselectAllNodes();
// import data
if (
netzgrafikDto.trainrunSections.length === 0 ||
!this.detectNetzgrafikJSON3rdParty(netzgrafikDto)
) {
// -----------------------------------------------
// Default: Netzgrafik-Editor exported JSON
// -----------------------------------------------
this.dataService.loadNetzgrafikDto(netzgrafikDto, false, true);
// -----------------------------------------------
} else {
// -----------------------------------------------
// 3rd Party: Netzgrafik-Editor exported JSON
// -----------------------------------------------
this.processNetzgrafikJSON3rdParty(netzgrafikDto);
}
// recompute viewport
this.uiInteractionService.viewportCenteringOnNodesBoundingBox();
}
private getFilenameToExport() {
const editorMode = this.uiInteractionService.getEditorMode();
switch (editorMode) {
case EditorMode.StreckengrafikEditing:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.spaceTimeChartFile:spaceTimeChart`;
case EditorMode.OriginDestination:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.originDestinationFile:originDestination`;
default:
return $localize`:@@app.view.editor-side-view.editor-tools-view-component.netzgrafikFile:netzgrafik`;
}
}
}