forked from cardstack/boxel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaflet-gtfs.gts
254 lines (217 loc) · 6 KB
/
leaflet-gtfs.gts
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
import { CardDef } from 'https://cardstack.com/base/card-api';
import NumberField from 'https://cardstack.com/base/number';
import StringField from 'https://cardstack.com/base/string';
import {
Component,
contains,
field,
} from 'https://cardstack.com/base/card-api';
import Modifier, { NamedArgs } from 'ember-modifier';
//@ts-expect-error no types available
import jszip from 'https://cdn.jsdelivr.net/npm/jszip@3.10.1/+esm';
//@ts-expect-error no types available
import Papa from 'https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { on } from '@ember/modifier';
import { LeafletModifier } from './leaflet-map';
import { optional } from '@cardstack/boxel-ui/helpers';
type Route = {
id: string;
number: string;
name: string;
};
export class LeafletGtfs extends CardDef {
static displayName = 'Leaflet GTFS';
@field lat = contains(NumberField);
@field lon = contains(NumberField);
@field tileserverUrl = contains(StringField);
@field gtfsUrl = contains(StringField);
map: any;
@tracked routes: Route[] = [];
@tracked activeRouteId: string | null = null;
@tracked shapes: any[] = [];
@tracked trips: any[] = [];
@tracked routesFeatureGroup: any = null;
@tracked routePolylines: any[] = [];
@action
setMap(map: any) {
this.map = map;
}
@action
setRoutes(routes: any) {
this.routes = routes.map((routeCsv: any) => ({
id: routeCsv.route_id,
name: `${routeCsv.route_short_name} ${routeCsv.route_long_name}`,
}));
}
@action
setShapes(shapes: any) {
this.shapes = shapes;
}
@action
setTrips(trips: any) {
this.trips = trips;
}
@action
handleRouteSelect(e: Event) {
if (e.target) {
let select = e.target as HTMLSelectElement;
this.setActiveRouteId(select.value);
}
}
@action
setActiveRouteId(activeRouteId: string) {
this.activeRouteId = activeRouteId;
this.routesFeatureGroup?.remove();
let activeRouteVariantPoints = this.activeRouteVariantPoints;
if (activeRouteVariantPoints) {
this.routesFeatureGroup = L.featureGroup(
activeRouteVariantPoints.map((points) => {
return L.polyline(points);
}),
);
this.routesFeatureGroup.addTo(this.map);
this.map.fitBounds(this.routesFeatureGroup.getBounds());
} else {
this.routesFeatureGroup = null;
}
}
get activeRouteShapeIds() {
if (this.trips) {
return this.trips.reduce((shapeIds, trip) => {
if (trip.route_id === this.activeRouteId) {
shapeIds.add(trip.shape_id);
}
return shapeIds;
}, new Set());
}
}
get activeRouteVariantPoints() {
if (this.shapes && this.activeRouteShapeIds) {
let activeRouteShapeIds = this.activeRouteShapeIds;
let shapeIdToPoints = new Map<string, [number, number][]>();
this.shapes.forEach((shape) => {
if (activeRouteShapeIds.has(shape.shape_id)) {
if (!shapeIdToPoints.has(shape.shape_id)) {
shapeIdToPoints.set(shape.shape_id, []);
}
shapeIdToPoints
?.get(shape.shape_id)
?.push([shape.shape_pt_lat, shape.shape_pt_lon]);
}
});
return Array.from(shapeIdToPoints.values());
}
return [];
}
static isolated = class Isolated extends Component<typeof this> {
<template>
<figure
{{LeafletModifier
lat=@model.lat
lon=@model.lon
tileserverUrl=@model.tileserverUrl
setMap=@model.setMap
}}
{{GtfsModifier
gtfsUrl=@model.gtfsUrl
setRoutes=@model.setRoutes
setShapes=@model.setShapes
setTrips=@model.setTrips
}}
class='map'
>
Map loading for
{{@model.lat}},
{{@model.lon}}
</figure>
<ul class='routes'>
{{#if @model.routes}}
Route to view:
<select
value={{@model.activeRouteId}}
{{on 'change' (optional @model.handleRouteSelect)}}
>
<option value=''>Select a route</option>
{{#each @model.routes as |route|}}
<option value={{route.id}}>{{route.name}}</option>
{{/each}}
</select>
{{/if}}
</ul>
<style>
figure.map {
margin: 0;
width: 100%;
height: 90%;
}
.routes {
height: 10%;
}
button.active {
font-weight: bold;
}
</style>
<link
href='https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.css'
rel='stylesheet'
/>
</template>
};
/*
static embedded = class Embedded extends Component<typeof this> {
<template></template>
}
static atom = class Atom extends Component<typeof this> {
<template></template>
}
static edit = class Edit extends Component<typeof this> {
<template></template>
}
*/
}
interface GtfsModifierSignature {
Args: {
Positional: [];
Named: {
gtfsUrl?: string;
setRoutes?: (routes: any) => void;
setShapes?: (shapes: any) => void;
setTrips?: (trips: any) => void;
};
};
}
class GtfsModifier extends Modifier<GtfsModifierSignature> {
modify(
_element: HTMLElement,
[],
{
gtfsUrl,
setRoutes,
setShapes,
setTrips,
}: NamedArgs<GtfsModifierSignature>,
) {
if (!gtfsUrl) {
return;
}
fetch(gtfsUrl)
.then((gtfsResponse) => {
return gtfsResponse.blob();
})
.then(jszip.loadAsync)
.then((zip: any) =>
Promise.all(
['routes.txt', 'shapes.txt', 'trips.txt'].map((filename) =>
zip.file(filename).async('string'),
),
),
)
.then(([routesCsv, shapesCsv, tripsCsv]) => {
setRoutes?.(Papa.parse(routesCsv, { header: true }).data);
setShapes?.(Papa.parse(shapesCsv, { header: true }).data);
setTrips?.(Papa.parse(tripsCsv, { header: true }).data);
});
}
}