113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
/*
|
|
* Copyright (c) 2020. Pascal Syma <pascal@syma.dev>.
|
|
* All rights reserved.
|
|
*/
|
|
|
|
import {LatLng, Map, Polyline} from "leaflet";
|
|
import Stop from "./Stop";
|
|
import Coordinate from "./Coordinate";
|
|
|
|
export default class Line {
|
|
readonly map: Map;
|
|
readonly name: string;
|
|
readonly stops: Stop[] = [];
|
|
readonly ends: string[] = [];
|
|
|
|
readonly breaks: number[] = [];
|
|
readonly stopBreaks: number[] = [];
|
|
readonly endBreaks: number[] = [];
|
|
readonly duration: number = 0;
|
|
readonly coords: Coordinate[];
|
|
readonly color: string = '#000';
|
|
|
|
constructor(map: Map, name: string, stops: string, color: string) {
|
|
this.map = map;
|
|
this.color = color
|
|
this.name = name;
|
|
let coords = stops.split("\n").map(e => {
|
|
let a = e.split(',');
|
|
return a.length > 2 ? new Stop(Number.parseFloat(a[0]), Number.parseFloat(a[1]), Number.parseFloat(a[2]), a[3], a.length === 5 ? a[4] : null) : new Coordinate(Number.parseFloat(a[0]), Number.parseFloat(a[1]));
|
|
});
|
|
this.coords = coords;
|
|
|
|
let lastStop = <Stop>coords[0];
|
|
let length = 0;
|
|
let breaks: number[] = [];
|
|
for (let i = 0; i < coords.length; i++) {
|
|
if (coords[i] instanceof Stop) {
|
|
let stop = <Stop>coords[i];
|
|
this.stops.push(stop)
|
|
this.stopBreaks.push(stop.minute)
|
|
if (stop.end) {
|
|
this.endBreaks.push(stop.minute)
|
|
this.ends.push(stop.end)
|
|
}
|
|
|
|
const td = stop.minute - lastStop.minute
|
|
if (coords[i - 1])
|
|
length += coords[i - 1].distance(stop)
|
|
breaks.map(e => lastStop.minute + (e / length) * td).forEach(e => this.breaks.push(e));
|
|
|
|
|
|
this.breaks.push(stop.minute)
|
|
|
|
length = 0;
|
|
breaks = [];
|
|
|
|
lastStop = stop
|
|
} else {
|
|
let len = coords[i - 1].distance(coords[i]);
|
|
length += len;
|
|
breaks.push(length)
|
|
}
|
|
}
|
|
this.duration = lastStop.minute
|
|
}
|
|
|
|
drawStops(show: boolean = true) {
|
|
if (show) {
|
|
this.stops.forEach(s => s.marker.addTo(this.map))
|
|
} else {
|
|
this.stops.forEach(s => s.marker.removeFrom(this.map))
|
|
}
|
|
}
|
|
|
|
draw() {
|
|
let pointList = this.coords.map(c => new LatLng(c.x, c.y));
|
|
new Polyline(pointList, {
|
|
color: this.color,
|
|
weight: 5,
|
|
// opacity: 0.7
|
|
}).addTo(this.map)
|
|
this.stops.forEach(s => {
|
|
// if (s.isEnd)
|
|
// new Popup().setLatLng([s.x, s.y]).setContent(this.name + ' - ' + s.name).openOn(this.map)
|
|
})
|
|
// let last: Coordinate = null;
|
|
// this.coords.forEach(c => {
|
|
// ctx.beginPath()
|
|
// ctx.textAlign = "center";
|
|
// if (c instanceof Stop) {
|
|
// let stop: Stop = c;
|
|
// ctx.arc(c.x, c.y, 5, 0, 2 * Math.PI);
|
|
// ctx.strokeStyle = this.color
|
|
// ctx.fillStyle = '#000'
|
|
// ctx.fillText(stop.name, c.x, c.y - 10);
|
|
// }
|
|
// ctx.stroke()
|
|
// ctx.closePath()
|
|
// if (last) {
|
|
// ctx.beginPath()
|
|
// ctx.moveTo(last.x, last.y)
|
|
// ctx.lineWidth = 3;
|
|
// ctx.lineTo(c.x, c.y)
|
|
// ctx.strokeStyle = this.color
|
|
// ctx.stroke()
|
|
// ctx.closePath()
|
|
// }
|
|
// last = c;
|
|
// })
|
|
}
|
|
|
|
|
|
} |