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
|
using System.Globalization;
using Enmarcha.Sources.OpenTripPlannerGql.Queries;
using Enmarcha.Backend.Configuration;
using Enmarcha.Backend.Helpers;
using Enmarcha.Backend.Types.Otp;
using Enmarcha.Backend.Types.Planner;
using Enmarcha.Backend.Types.Transit;
using Enmarcha.Sources.OpenTripPlannerGql;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace Enmarcha.Backend.Services;
public class OtpService
{
private readonly HttpClient _httpClient;
private readonly AppConfiguration _config;
private readonly IMemoryCache _cache;
private readonly ILogger<OtpService> _logger;
private readonly FareService _fareService;
private readonly FeedService _feedService;
public OtpService(HttpClient httpClient, IOptions<AppConfiguration> config, IMemoryCache cache, ILogger<OtpService> logger, FareService fareService, FeedService feedService)
{
_httpClient = httpClient;
_config = config.Value;
_cache = cache;
_logger = logger;
_fareService = fareService;
_feedService = feedService;
}
public RouteDto MapRoute(RoutesListResponse.RouteItem route)
{
var feedId = route.GtfsId.Split(':')[0];
var color = route.Color;
if (!string.IsNullOrWhiteSpace(color))
{
if (!color.StartsWith("#")) color = "#" + color;
}
if (string.IsNullOrWhiteSpace(color) || color.Length != 7)
{
(color, _) = _feedService.GetFallbackColourForFeed(feedId);
}
var textColor = ContrastHelper.GetBestTextColour(color.TrimStart('#'));
return new RouteDto
{
Id = route.GtfsId,
ShortName = _feedService.NormalizeRouteShortName(feedId, route.ShortName ?? string.Empty),
LongName = route.LongName,
Color = color,
TextColor = textColor,
SortOrder = route.SortOrder,
AgencyName = route.Agency?.Name,
TripCount = route.Patterns.Sum(p => p.TripsForDate.Count)
};
}
public RouteDetailsDto MapRouteDetails(RouteDetailsResponse.RouteItem route)
{
var feedId = route.GtfsId?.Split(':')[0] ?? "unknown";
var color = route.Color;
if (!string.IsNullOrWhiteSpace(color))
{
if (!color.StartsWith("#")) color = "#" + color;
}
if (string.IsNullOrWhiteSpace(color) || color.Length != 7)
{
(color, _) = _feedService.GetFallbackColourForFeed(feedId);
}
var textColor = ContrastHelper.GetBestTextColour(color.TrimStart('#'));
return new RouteDetailsDto
{
ShortName = _feedService.NormalizeRouteShortName(feedId, route.ShortName ?? string.Empty),
LongName = route.LongName,
Color = color,
TextColor = textColor,
AgencyName = route.Agency?.Name,
Patterns = route.Patterns.Select(p => MapPattern(p, feedId)).ToList()
};
}
private PatternDto MapPattern(RouteDetailsResponse.PatternItem pattern, string feedId)
{
return new PatternDto
{
Id = pattern.Id,
Name = pattern.Name,
Headsign = pattern.Headsign,
DirectionId = pattern.DirectionId,
Code = pattern.Code,
SemanticHash = pattern.SemanticHash,
TripCount = pattern.TripsForDate.Count,
Geometry = DecodePolyline(pattern.PatternGeometry?.Points)?.Coordinates,
Stops = pattern.Stops.Select((s, i) => new PatternStopDto
{
Id = s.GtfsId,
Code = _feedService.NormalizeStopCode(feedId, s.Code ?? string.Empty),
Name = _feedService.NormalizeStopName(feedId, s.Name),
Lat = s.Lat,
Lon = s.Lon,
ScheduledDepartures = pattern.TripsForDate
.Select(t => t.Stoptimes.ElementAtOrDefault(i)?.ScheduledDeparture ?? -1)
.Where(d => d != -1)
.OrderBy(d => d)
.ToList()
}).ToList()
};
}
public async Task<List<StopTileResponse.Stop>> GetStopsByBboxAsync(double minLon, double minLat, double maxLon, double maxLat)
{
const string cacheKey = "otp_all_stops_detailed";
if (_cache.TryGetValue(cacheKey, out List<StopTileResponse.Stop>? cachedStops) && cachedStops != null)
{
return cachedStops;
}
try
{
var bbox = new StopTileRequestContent.Bbox(minLon, minLat, maxLon, maxLat);
var query = StopTileRequestContent.Query(bbox);
var request = new HttpRequestMessage(HttpMethod.Post, $"{_config.OpenTripPlannerBaseUrl}/gtfs/v1");
request.Content = JsonContent.Create(new GraphClientRequest { Query = query });
var response = await _httpClient.SendAsync(request);
var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<StopTileResponse>>();
if (responseBody is not { IsSuccess: true } || responseBody.Data?.StopsByBbox == null)
{
_logger.LogError("Error fetching stops from OTP for caching");
return new List<StopTileResponse.Stop>();
}
var stops = responseBody.Data.StopsByBbox;
_cache.Set(cacheKey, stops, TimeSpan.FromHours(18));
return stops;
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception fetching stops from OTP for caching");
return new List<StopTileResponse.Stop>();
}
}
private Leg MapLeg(OtpLeg otpLeg)
{
return new Leg
{
Mode = otpLeg.Mode,
RouteName = otpLeg.Route,
RouteShortName = otpLeg.RouteShortName,
RouteLongName = otpLeg.RouteLongName,
Headsign = otpLeg.Headsign,
AgencyName = otpLeg.AgencyName,
RouteColor = otpLeg.RouteColor,
RouteTextColor = otpLeg.RouteTextColor,
From = MapPlace(otpLeg.From),
To = MapPlace(otpLeg.To),
StartTime = DateTimeOffset.FromUnixTimeMilliseconds(otpLeg.StartTime).UtcDateTime,
EndTime = DateTimeOffset.FromUnixTimeMilliseconds(otpLeg.EndTime).UtcDateTime,
DistanceMeters = otpLeg.Distance,
Geometry = DecodePolyline(otpLeg.LegGeometry?.Points),
Steps = otpLeg.Steps.Select(MapStep).ToList(),
IntermediateStops = otpLeg.IntermediateStops.Select(MapPlace).Where(p => p != null).Cast<PlannerPlace>().ToList()
};
}
private PlannerPlace? MapPlace(OtpPlace? otpPlace)
{
if (otpPlace == null) return null;
var feedId = otpPlace.StopId?.Split(':')[0] ?? "unknown";
return new PlannerPlace
{
Name = _feedService.NormalizeStopName(feedId, otpPlace.Name!),
Lat = otpPlace.Lat,
Lon = otpPlace.Lon,
StopId = otpPlace.StopId, // Use string directly
StopCode = _feedService.NormalizeStopCode(feedId, otpPlace.StopCode ?? string.Empty)
};
}
private Step MapStep(OtpWalkStep otpStep)
{
return new Step
{
DistanceMeters = otpStep.Distance,
RelativeDirection = otpStep.RelativeDirection,
AbsoluteDirection = otpStep.AbsoluteDirection,
StreetName = otpStep.StreetName,
Lat = otpStep.Lat,
Lon = otpStep.Lon
};
}
private PlannerGeometry? DecodePolyline(string? encodedPoints)
{
if (string.IsNullOrEmpty(encodedPoints)) return null;
var coordinates = Decode(encodedPoints);
return new PlannerGeometry
{
Coordinates = coordinates.Select(c => new List<double> { c.Lon, c.Lat }).ToList()
};
}
// Polyline decoding algorithm
private static List<(double Lat, double Lon)> Decode(string encodedPoints)
{
if (string.IsNullOrEmpty(encodedPoints))
return new List<(double, double)>();
var poly = new List<(double, double)>();
char[] polylineChars = encodedPoints.ToCharArray();
int index = 0;
int currentLat = 0;
int currentLng = 0;
int next5bits;
int sum;
int shifter;
while (index < polylineChars.Length)
{
// calculate next latitude
sum = 0;
shifter = 0;
do
{
next5bits = (int)polylineChars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylineChars.Length);
if (index >= polylineChars.Length)
break;
currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
// calculate next longitude
sum = 0;
shifter = 0;
do
{
next5bits = (int)polylineChars[index++] - 63;
sum |= (next5bits & 31) << shifter;
shifter += 5;
} while (next5bits >= 32 && index < polylineChars.Length);
currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
poly.Add((Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0));
}
return poly;
}
public RoutePlan MapPlanResponse(PlanConnectionResponse response)
{
var itineraries = response.PlanConnection.Edges
.Select(e => MapItinerary(e.Node))
.ToList();
return new RoutePlan
{
Itineraries = itineraries
};
}
private Itinerary MapItinerary(PlanConnectionResponse.Node node)
{
var legs = node.Legs.Select(MapLeg).ToList();
var fares = _fareService.CalculateFare(legs);
return new Itinerary
{
DurationSeconds = node.DurationSeconds,
StartTime = DateTime.Parse(node.Start8601, null, DateTimeStyles.RoundtripKind),
EndTime = DateTime.Parse(node.End8601, null, DateTimeStyles.RoundtripKind),
WalkDistanceMeters = node.WalkDistance,
WalkTimeSeconds = node.WalkSeconds,
TransitTimeSeconds = node.DurationSeconds - node.WalkSeconds - node.WaitingSeconds,
WaitingTimeSeconds = node.WaitingSeconds,
Legs = legs,
CashFare = fares.CashFareEuro,
CashFareIsTotal = fares.CashFareIsTotal,
CardFare = fares.CardFareEuro,
CardFareIsTotal = fares.CardFareIsTotal
};
}
private Leg MapLeg(PlanConnectionResponse.Leg leg)
{
var feedId = leg.From.Stop?.GtfsId?.Split(':')[0] ?? "unknown";
var shortName = _feedService.NormalizeRouteShortName(feedId, leg.Route?.ShortName ?? string.Empty);
var headsign = leg.Headsign;
var headsignTrimmed = headsign?.Trim();
if (feedId == "vitrasa")
{
headsign = headsign?.Replace("*", "");
if (headsign == "FORA DE SERVIZO.G.B.")
{
headsign = "García Barbón, 7 (fora de servizo)";
}
switch (shortName)
{
case "A" when headsignTrimmed != null &&
(headsignTrimmed.StartsWith("\"1\"", StringComparison.Ordinal) ||
(headsignTrimmed.Length >= 1 && headsignTrimmed[0] == '1' &&
(headsignTrimmed.Length == 1 || !char.IsDigit(headsignTrimmed[1])))):
shortName = "A1";
if (headsignTrimmed.StartsWith("\"1\"", StringComparison.Ordinal))
{
headsign = headsignTrimmed[3..];
}
else
{
headsign = headsignTrimmed[1..];
}
headsign = headsign.TrimStart(' ', '-', '.', ':');
break;
case "6":
headsign = headsign?.Replace("\"", "");
break;
}
}
var color = leg.Route?.Color;
var textColor = leg.Route?.TextColor;
if (string.IsNullOrEmpty(color) || color == "FFFFFF")
{
var (fallbackColor, fallbackTextColor) = _feedService.GetFallbackColourForFeed(feedId);
color = fallbackColor.Replace("#", "");
textColor = fallbackTextColor.Replace("#", "");
}
else if (string.IsNullOrEmpty(textColor) || textColor == "000000")
{
textColor = ContrastHelper.GetBestTextColour(color).Replace("#", "");
}
return new Leg
{
Mode = leg.Mode,
FeedId = feedId,
RouteId = leg.Route?.GtfsId,
RouteName = leg.Route?.LongName,
RouteShortName = shortName,
RouteLongName = leg.Route?.LongName,
Headsign = headsign,
AgencyName = leg.Route?.Agency?.Name,
RouteColor = color,
RouteTextColor = textColor,
From = MapPlace(leg.From),
To = MapPlace(leg.To),
StartTime = DateTime.Parse(leg.Start.ScheduledTime8601, null, DateTimeStyles.RoundtripKind),
EndTime = DateTime.Parse(leg.End.ScheduledTime8601, null, DateTimeStyles.RoundtripKind),
DistanceMeters = leg.Distance,
Geometry = DecodePolyline(leg.LegGeometry?.Points),
Steps = leg.Steps.Select(MapStep).ToList(),
IntermediateStops = leg.StopCalls.Select(sc => MapPlace(sc.StopLocation)).ToList()
};
}
private PlannerPlace MapPlace(PlanConnectionResponse.LegPosition pos)
{
var feedId = pos.Stop?.GtfsId?.Split(':')[0] ?? "unknown";
return new PlannerPlace
{
Name = _feedService.NormalizeStopName(feedId, pos.Name),
Lat = pos.Latitude,
Lon = pos.Longitude,
StopId = pos.Stop?.GtfsId,
StopCode = _feedService.NormalizeStopCode(feedId, pos.Stop?.Code ?? string.Empty),
ZoneId = pos.Stop?.ZoneId
};
}
private PlannerPlace MapPlace(PlanConnectionResponse.StopLocation stop)
{
var feedId = stop.GtfsId?.Split(':')[0] ?? "unknown";
return new PlannerPlace
{
Name = _feedService.NormalizeStopName(feedId, stop.Name),
Lat = stop.Latitude,
Lon = stop.Longitude,
StopId = stop.GtfsId,
StopCode = _feedService.NormalizeStopCode(feedId, stop.Code ?? string.Empty)
};
}
private Step MapStep(PlanConnectionResponse.Step step)
{
return new Step
{
DistanceMeters = step.Distance,
RelativeDirection = step.RelativeDirection,
AbsoluteDirection = step.AbsoluteDirection,
StreetName = step.StreetName,
Lat = step.Latitude,
Lon = step.Longitude
};
}
}
|